Stuff what I did

Backup my Raspberry Pi —

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’t seem to take much for them to corrupt…

Backing up the whole SD card:

I’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’t be automated and involves taking the website offline, but it will capture absolutely everything. The operating system changes, WordPress config, website content and importantly any minor tweaks that I may have forgotten to note down.
So firstly I shutdown the web server:

sudo shutdown -h 0

Once all the Pi’s leds have stopped flickering and only the red power led remains, the Pi can be unplugged and the SD card removed.
I 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:

dd if=/dev/sdc of=~/webserver.img bs=2M conv=sync,noerror

Replace 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).
The output file could also be changed, here I’ve used webserver.img in my home directory.

This 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.

Recovering the whole SD card:

By reversing the previous backup command I can now write the same image to a new SD card:

dd if=~/webserver.img of=/dev/sdc bs=2M

Accessing files within an SD card backup:

For a more ‘granular’ recovery from an SD card backup I can access (and even modify) individual files within the webserver.img backup.
First by listing the partitions within the image file:

fdisk -l webserver.img
Device          Boot  Start        End       Blocks   Id     System
webserver.img1         8192      122879       57344    c     W95 FAT32 (LBA)
webserver.img2       122880    15415295     7646208   83     Linux

Either of the two partitions in this example could then be mounted:

mkdir /mnt/img1
mount -o loop,offset=$[8192*512] -t vfat webserver.img /mnt/img1
mkdir /mnt/img2
mount -o loop,offset=$[122880*512] -t ext4 webserver.img /mnt/img2

The 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.

cd /mnt/img2/var/www

Categorised as: Raspberry Pi Web Server

Comments are disabled on this post

Comments are closed.