Raspberry Pi – Backup
Currently, you can read a lot about Raspberry Pi in blogs and forums. There are a lot of topics about ideas and projects but I often miss one very important topic: backup strategies! However which operating system you have Linux, Mac or Windows, you have to make regularly backups otherwise you have to risk to loose data. On your Raspberry Pi you may not have such sensible data like family fotos or business data that are really important for you, but what if you use your Raspberry Pi with a database where you store sensible data from measurements?
If you do not have sensible data you may only need to reinstall your operating system…but if so, you also have an effort to setup and configure. So if you make a regularly backup, a reset is done within some minutes. You can distinguish between two backup strategies:
- manual backup of your SD card as image
- automatic backup, while your Raspberry Pi is running
The simplest backup you can think of is to make a copy of your SD card. For this you have to stop your Raspberry Pi and put the SD card into another computer. There you can copy the whole contents of your SD card as image onto your laptop or computer. This is slightly different if you have another operating system.
Linux / Mac OSX
if you have an Unix like operating system, it is very simple. You only have to do the following commands in your console/terminal:
Here you should find the path to your SD card (normally it is mounted like /dev/disk1s1).
diskutil umount /dev/disk1s1
Now you have to unmount your SD card. The command above is from MacOSX, on linux you only need the unmount command.
dd if=/dev/disk1s1 of=/home/backup/backup.img bs=1M
With the command dd you can copy the whole contents of your SD card to an image file. Here you should use the correct mount directory from your SD card and then you need to set a directory and filename where the image has to be created. Attention: the image can get big! You can restore your backup (if your SD card has a failure) with the same command! You only have to change if and of. For this it is a good idea to use a formatted SD card with same size.
on Windows you can use the tool win32Diskimager.exe to store data to SD card and to backup data from SD card. For a backup you only need to put a path and filename into ‚Image File‘ and then click ‚Read‘.
You can also do an automatic backup directly from your Raspberry Pi with a script. For this you need a place where you can store your backup. So you need to connect a USB stick or an external hard disk with appropriate size. Another possibility is to use a network share or a FTP server.
First: you should find out how your SD card is mounted from our Raspberry Pi. You can do this with this commend:
sudo cat /etc/fstab
Normally this should be /dev/mmcblk0p2. You can run a backup during runtime with this command:
sudo dd if=/dev/mmcblk0p2 of=/home/pi/networkdrive/my.img bs=1M
For the output path ‚of‘ you have to set the path to your USB stick, external hard disk or network share. For your daily backup, you can call this command every day with cron.
I found a very useful script which can be used without changes:
#!/bin/bash # Setting up directories SUBDIR=raspberrypi_backups DIR=/hdd/$SUBDIR echo "Starting RaspberryPI backup process!" # First check if pv package is installed, if not, install it first PACKAGESTATUS=`dpkg -s pv | grep Status`; if [[ $PACKAGESTATUS == S* ]] then echo "Package 'pv' is installed." else echo "Package 'pv' is NOT installed." echo "Installing package 'pv'. Please wait..." apt-get -y install pv fi # Check if backup directory exists if [ ! -d "$DIR" ]; then echo "Backup directory $DIR doesn't exist, creating it now!" mkdir $DIR fi # Create a filename with datestamp for our current backup (without .img suffix) OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)" # Create final filename, with suffix OFILEFINAL=$OFILE.img # First sync disks sync; sync # Shut down some services before starting backup process echo "Stopping some services before backup." service apache2 stop service mysql stop service cron stop # Begin the backup process, should take about 1 hour from 8Gb SD card to HDD echo "Backing up SD card to USB HDD." echo "This will take some time depending on your SD card size and read performance. Please wait..." SDSIZE=`blockdev --getsize64 /dev/mmcblk0`; pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock # Wait for DD to finish and catch result RESULT=$? # Start services again that where shutdown before backup process echo "Start the stopped services again." service apache2 start service mysql start service cron start # If command has completed successfully, delete previous backups and exit if [ $RESULT = 0 ]; then echo "Successful backup, previous backup files will be deleted." rm -f $DIR/backup_*.tar.gz mv $OFILE $OFILEFINAL echo "Backup is being tarred. Please wait..." tar zcf $OFILEFINAL.tar.gz $OFILEFINAL rm -rf $OFILEFINAL echo "RaspberryPI backup process completed! FILE: $OFILEFINAL.tar.gz" exit 0 # Else remove attempted backup file else echo "Backup failed! Previous backup files untouched." echo "Please check there is sufficient space on the HDD." rm -f $OFILE echo "RaspberryPI backup process failed!" exit 1 fi
I hope you are now backuping your data regularly!