{"id":28,"date":"2012-09-24T00:55:00","date_gmt":"2012-09-24T00:55:00","guid":{"rendered":"https:\/\/robertrath.com\/gadgetzone\/archives\/28-backing-up-the-raspberry-pi-blog.html"},"modified":"2012-09-24T00:55:00","modified_gmt":"2012-09-24T00:55:00","slug":"backing-up-the-raspberry-pi-blog","status":"publish","type":"post","link":"https:\/\/robertrath.com\/gadgetzone\/archives\/28-backing-up-the-raspberry-pi-blog.html","title":{"rendered":"Backing Up the Raspberry Pi Blog"},"content":{"rendered":"It would be a shame to lose everything I have put into the Raspberry Pi blog so it&#8217;s time to put in a backup regime.<br \/>\n<br \/>\n<!-- s9ymdb:5 --><img loading=\"lazy\" decoding=\"async\" class=\"serendipity_image_left\" width=\"440\" height=\"300\" src=\"\/gadgetzone\/uploads\/Images\/backup2.gif\" alt=\"\" \/><br \/>\n<br \/>\n<strong>Strategy<\/strong> <br \/>\n<br \/>\nThe Raspberry Pi blog is located both both &#8216;\/var\/www\/raspberrypi\/&#8217; and in the serendipity-db catalog of the local MySQL database. We will need to backup up both to completely cover ourselves.<br \/>\n<br \/>\nThe strategy for backup will be to make archives of both the filesystem and the mysql database, archive them together and then copy to the network drive under a unique filename. The backup will be run from a script which which will live in the &#8216;\/var\/www&#8217; directory and be regularly run from an entry in the &#8216;\/etc\/crontab&#8217; file.<br \/>\n<br \/>\nDuring the backup process I will create temporary files on the network drive to avoid wear on the local SD card.<br \/>\n<br \/>\nWe first setup NFS or SAMBA networking and ensure we have mounted our network storage device at a suitable mount point. I have chosen NFS in this example. To use Windows networking adapt the instructions below accordingly.<br \/>\n<br \/>\n1. Install NFS client (checked already installed as part of the Weezy distribution).<br \/>\n2. Make a mount point for backups at &#8216;\/var\/www\/backup&#8217; and add edit &#8216;\/etc\/rc.local&#8217; to mount our external resource on boot by adding the following line &#8216;mount -t nfs -o nolock [IP address or hostname of your NAS server]:\/var\/local\/www  \/var\/www\/backup&#8217;<br \/>\n<br \/>\n<br \/>\n<strong>Backup Script<\/strong><br \/>\n<br \/>\nHere is my script. Note the use of the file &#8216;\/var\/www\/backup\/sync_www&#8217; to ensure we have mounted the network drive and are not just working with our local mount point.<br \/>\n<br \/>\n<blockquote>#!\/bin\/sh<br \/>\n<br \/>\nif [ -e \/var\/www\/backup\/sync_www ]; then<br \/>\n<br \/>\n  mkdir -p \/var\/www\/backup\/raspberrypi\/<br \/>\n  cd \/var\/www\/<br \/>\n  tar &#8211;exclude-vcs -czf .\/backup\/raspberrypi\/raspberrypi-www_`date +%y_%m_%d`.gz raspberrypi &amp;&amp; \\<br \/>\n  mysqldump -u YOUR_MYSQL_USERNAME_HERE -pYOUR_MYSQL_PASSWORD_HERE serendipity-db | gzip &gt; .\/backup\/raspberrypi\/raspberrypi-mysqldump_`date +%y_%m_%d`.gz &amp;&amp; \\<br \/>\n  tar &#8211;remove -cf .\/backup\/raspberrypi\/raspberrypi-blog_`date +%y_%m_%d`.tar .\/backup\/raspberrypi\/raspberrypi-www_`date +%y_%m_%d`.gz .\/backup\/raspberrypi\/raspberrypi-mysqldump_`date +%y_%m_%d`.gz<br \/>\n<br \/>\n  exit 0<br \/>\n<br \/>\nelse<br \/>\n<br \/>\n  exit 1<br \/>\n<br \/>\nfi<br \/>\n<\/blockquote><br \/>\n<br \/>\nA brief description pf the steps of the script are as follows<br \/>\n<br \/>\n1. Check to see we have mounted the network drive by looking for a file called &#8216;sync-www&#8217; which we have alread place on the network drive.<br \/>\n2. Create our target backup directory if it does not already exist.<br \/>\n3. Change to the &#8216;\/var\/www\/&#8217; as our working directory.<br \/>\n4. Create a dated temporary archive of the files in &#8216;\/var\/www\/raspberypi&#8217;<br \/>\n5. Create a dated temporary archive of the mysql data dumped from the catalog &#8216;serendipity-db&#8217;<br \/>\n6. Create the final backup archive removing the temporary working files.<br \/>\n<br \/>\n<br \/>\n<strong>Handling Erorrs<\/strong><br \/>\n <br \/>\nNote the use of &#8216;&amp;&amp; \\&#8217; after each line ensures that the next line only executes if the previous command was successful otherwise the script exits with error code 1.<br \/>\n<br \/>\nNote this script could be further enhanced with email notifications of success or failure as needed.<br \/>\n<br \/>\n<br \/>\n<strong>Scheduling<\/strong><br \/>\n<br \/>\nFinally the following entry in &#8216;\/etc\/crontab&#8217; will ensure this script runs daily at 2:30 am.<br \/>\n<br \/>\n<blockquote>&#8217;30 2    <strong> <\/strong> *   root    \/var\/www\/backup-www.sh &amp;&#8217;<\/blockquote><br \/>\n<br \/>\n<br \/>\n   &#8230; Robert<br \/>\n<br \/>\n<br \/>\n","protected":false},"excerpt":{"rendered":"<p>It would be a shame to lose everything I have put into the Raspberry Pi blog so it&#8217;s time to put in a backup regime. Strategy The Raspberry Pi blog is located both both &#8216;\/var\/www\/raspberrypi\/&#8217; and in the serendipity-db catalog of the local MySQL database. We will need to backup up both to completely cover &#8230; <a title=\"Backing Up the Raspberry Pi Blog\" class=\"read-more\" href=\"https:\/\/robertrath.com\/gadgetzone\/archives\/28-backing-up-the-raspberry-pi-blog.html\" aria-label=\"Read more about Backing Up the Raspberry Pi Blog\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":0,"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"wp:attachment":[{"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robertrath.com\/gadgetzone\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}