{"id":98,"date":"2014-04-16T22:27:54","date_gmt":"2014-04-16T21:27:54","guid":{"rendered":"http:\/\/192.168.1.84\/?p=98"},"modified":"2019-06-01T22:54:31","modified_gmt":"2019-06-01T21:54:31","slug":"backing-up-my-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.benningtons.net\/index.php\/backing-up-my-raspberry-pi\/","title":{"rendered":"Backup my Raspberry Pi"},"content":{"rendered":"<p>After setting up my <a href=\"http:\/\/www.benningtons.net\/index.php\/raspberry-pi-web-server\/\" title=\"Raspberry Pi Web Server\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi Web Server<\/a> and <a href=\"http:\/\/www.benningtons.net\/index.php\/configuring-wordpress\/\" title=\"Configuring WordPress\" target=\"_blank\" rel=\"noopener noreferrer\">Configuring WordPress<\/a> I should really start making some backup copies before something goes wrong. SD cards are fickle things and it doesn&#8217;t seem to take much for them to corrupt&#8230;<\/p>\n<p><b>Backing up the whole SD card:<\/b><\/p>\n<p style=\"padding-left: 30px;\">\nI&#8217;ll start by taking a complete image copy of the whole SD card. Not a process I would want to regularly do, because it can&#8217;t be automated and involves taking the website offline, but it will capture absolutely everything. The operating system changes, WordPress config, website content and <u>importantly<\/u> any minor tweaks that I may have forgotten to note down.<br \/>\nSo firstly I shutdown the web server:<\/p>\n<div id=\"code\">\nsudo shutdown -h 0\n<\/div>\n<p style=\"padding-left: 30px;\">\nOnce all the Pi&#8217;s leds have stopped flickering and only the red power led remains, the Pi can be unplugged and the SD card removed.<br \/>\nI then use a card reader on another computer and a shell script to backup, compress and maintain a library of card images but, for this post, the actual card copying command is:<\/p>\n<div id=\"code\">\ndd if=\/dev\/sdc of=~\/webserver.img bs=2M conv=sync,noerror\n<\/div>\n<p style=\"padding-left: 30px;\">\nReplace the input file (\/dev\/sdc) with the device name of the SD card. Taking care to make sure this is the right device (tip: plug in the SD card and use the dmesg command to see which device name has just been added).<br \/>\nThe output file could also be changed, here I&#8217;ve used webserver.img in my home directory.<\/p>\n<p style=\"padding-left: 30px;\">\nThis backup takes a while (about 10 minutes on my old computer). When it has finished the card reader can be disconnected and the SD card returned to the Pi.<\/p>\n<p><b>Recovering the whole SD card:<\/b><\/p>\n<p style=\"padding-left: 30px;\">\nBy reversing the previous backup command I can now write the same image to a new SD card:<\/p>\n<div id=\"code\">\ndd if=~\/webserver.img of=\/dev\/sdc bs=2M\n<\/div>\n<p><b>Accessing files within an SD card backup:<\/b><\/p>\n<p style=\"padding-left: 30px;\">\nFor a more &#8216;granular&#8217; recovery from an SD card backup I can access (and even modify) individual files within the webserver.img backup.<br \/>\nFirst by listing the partitions within the image file:<\/p>\n<div id=\"code\">\nfdisk -l webserver.img\n<\/div>\n<pre>\r\nDevice          Boot  Start        End       Blocks   Id     System\r\nwebserver.img1         8192      122879       57344    c     W95 FAT32 (LBA)\r\nwebserver.img2       122880    15415295     7646208   83     Linux\r\n<\/pre>\n<p style=\"padding-left: 30px;\">\nEither of the two partitions in this example could then be mounted:<\/p>\n<div id=\"code\">\nmkdir \/mnt\/img1<br \/>\nmount -o loop,offset=$[8192*512] -t vfat webserver.img \/mnt\/img1<br \/>\nmkdir \/mnt\/img2<br \/>\nmount -o loop,offset=$[122880*512] -t ext4 webserver.img \/mnt\/img2\n<\/div>\n<p style=\"padding-left: 30px;\">\nThe backup of the \/var\/www directory from my webserver can then be accessed as follows. Files could even be altered before using the image to write a new SD card.<\/p>\n<div id=\"code\">\ncd \/mnt\/img2\/var\/www\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>After setting up my Raspberry Pi Web Server and Configuring WordPress I should really start making some backup copies before something goes wrong. SD cards are fickle things and it doesn&#8217;t seem to take much for them to corrupt&#8230; Backing up the whole SD card: I&#8217;ll start by taking a complete image copy of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[10,2],"_links":{"self":[{"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/posts\/98"}],"collection":[{"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":29,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/posts\/98\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benningtons.net\/index.php\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}