Backing up the Pi


This is a detailed listing of how I back up my Raspberry Pi’s in case of disk failure. Hopefully it well help someone.

I use a 4 gig USB thumb drive (a 1 gig will work if you can find one) that stays plugged into the Pi all the time. Format the stick for FAT32 (works best for me). Create a directory /mnt/usb as the mount point for the USB stick. Most likely the USB stick will be assigned /dev/sda1, unless you already have another USB drive plugged into the Pi.

This script is run by root’s cron at 2:30 am locally, although you could run it any time you like and, of course, change what files and directories are best for you. Edit a file: ‘sudo nano /usr/bin/backup’. Then make it executable ‘sudo chmod 755 /usr/bin/backup’. The backup script only copies important files and directories. It does not make an image of the entire Pi. See below for that.


/bin/mount /dev/sda1 /mnt/usb
/bin/tar czf /mnt/usb/home_pi.tar.gz /home/pi/dump-tools && \
/bin/tar czf /mnt/usb/var_www_collectd.tar.gz /var/www/collectd && \
/bin/tar czf /mnt/usb/var_lib_collectd.tar.gz /var/lib/collectd && \
/bin/tar czf /mnt/usb/usr_share_dump1090-mutability_html.tar.gz /usr/share/dump1090-mutability/html && \
/usr/bin/crontab -l > /mnt/usb/root.crontab && \
/bin/cp /usr/bin/backup /mnt/usb
/bin/cp /usr/bin/restore /mnt/usb
/bin/cp /etc/modprobe.d/8192cu.conf /mnt/usb
/bin/cp /etc/wpa_supplicant/wpa_supplicant.conf /mnt/usb
/bin/cp /etc/network/interfaces /mnt/usb
/bin/cp /etc/fr24feed.ini /mnt/usb
/bin/cp /etc/pfclient-config.json /mnt/usb
/bin/cp /etc/collectd/collectd.conf /mnt/usb/
/bin/cp /etc/default/dump1090-mutability /mnt/usb
/bin/cp /etc/ssh/sshd_config /mnt/usb/
/bin/cp /etc/lighttpd/lighttpd.conf /mnt/usb/
/bin/umount /dev/sda1

I leave the USB stick unmounted except when I need to get something off of it and then I unmount it again. The script mounts and unmounts the drive.

This script is run when I need to restore the data - usually only after I install a new SD card from scratch. This will restore your data so you can maintain your system stats for graphing. It also restores selected config files. Edit a file: ‘sudo nano /usr/bin/restore’. Then make it executable ‘sudo chmod 755 /usr/bin/restore’.


/bin/mount /dev/sda1 /mnt/usb
/bin/tar xvzf /mnt/usb/home_pi.tar.gz -C / && \
/bin/tar xvzf /mnt/usb/var_www_collectd.tar.gz -C / && \
/bin/tar xvzf /mnt/usb/var_lib_collectd.tar.gz -C / && \
/bin/tar xvzf /mnt/usb/usr_share_dump1090-mutability_html.tar.gz -C /
/bin/cp /mnt/usb/dump1090-mutability /etc/default/dump1090-mutability
/bin/cp /mnt/usb/collectd.conf /etc/collectd/collectd.conf
/bin/cp /mnt/usb/sshd_config /etc/ssh/sshd_config
/bin/cp /mnt/usb/lighttpd.conf /etc/lighttpd/lighttpd.conf
/bin/cp /mnt/usb/backup /usr/bin/backup
/bin/cp /mnt/usb/restore /usr/bin/restore
/bin/cp /mnt/usb/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
/bin/cp /mnt/usb/interfaces /etc/network/interfaces
/bin/cp /mnt/usb/fr24feed.ini /etc/fr24feed.ini
/bin/cp /mnt/usb/pfclient-config.json /etc/pfclient-config.json 
/bin/cp /mnt/usb/dump1090-mutability /etc/default/dump1090-mutability
/bin/umount /dev/sda1

Finally, after I have an SD card configured as I want it, I make an image of the SD card on my Windows PC using Win32DiskImager. If I need to do a quick restore, if the SD card fails, I restore this image to an SD card of the same size (I use 8 gig SD cards). Then, after I boot the Pi with this image, I run ‘/usr/bin/restore’ and refresh the data and config files on the freshly made SD card.

Reboot the Pi after running the restore script.

what files/directories do you recommend to backup
Backing up an SD card on a regular basis?

Thats cool Joe. I wonder is this can be done using a NAS. I have my Pi on LAN connection. I have a NAS on my network. Hmmm…


Rsync over ssh…


Can this be made to work still, I can’t seem to get to write to a USB stick on a PI. When inserted, it gets owned by root root, and then set to read only.

Two of us spent 4.5 hours last night trying to make it so we could use the Kingston 16gb data traveler 2.0. And to no avail I might add!

We had mixed results, but I’m wondering how it can be that the default position for a raspberry pi (and some other Linux boxes) is to make it not writable. Surely this is a bit daft given the nature of a thumb drive?



Take a look to rpi-clone which is a wonderfull python script able to backup the entire SD-card.


Piaware has its own udev rules which is what causes the USB stick to be mounted. If you don’t need those rules, just override them with:
Sudo ln -s /dev/null /etc/udev/rules.d/60-piaware-support.rules

Rpi-clone is great BTW.