Raspberry Pi Backup

I have been experimenting with creating backups for the raspberry pi. My backup philosophy is to backup everything on these little machines as I have a tendency to install packages and files everywhere. I have not been satisfied with traditional backup methods, such as rsync, rsnapshot, etc. I wanted a full image copy of the memory card in the raspberry pi.

So, I wrote a script to do this for you. I make some assumptions, but you can change the variables in the script. More on that in a bit. Basically, my script uses the “dd” command to make a bit by bit copy of your LIVE memory card and store it into a compressed image file on a USB flash drive. This is a tricky process, mainly because the Pi is powered on and utilizing the very memory card you are copying. If you have files that are constantly changing (databases, etc), this may not turn out so well. However, it works for my purposes and has saved my bacon a few times, especially when my Pi with Pi-Hole and my Ubiquity Cloud Key controller crashed in a fiery dumpster fire, you know, similar to 2020.

To recover, all I had to do was:

  1. Gracefully shutdown the pi: sudo shutdown -now
  2. Remove the flash drive & microSD from the Pi
  3. Insert Flash drive into my Windows 10 machine
  4. Unzip the last known good backup on the flash drive using 7-Zip all while saying a prayer
    1. TIP: Copy the image to your local hard drive as the image file is highly compressed (in my case a 2GB file expanded to 32GB)
  5. Use Rufus to rewrite the image file to microSD card
  6. Insert both USB and microSD back into Pi
  7. Power on and pray again.
  8. Test – TADA! I was back up and running within a short time.

This backup process is CPU and media intensive. It took about 2 hours and some change on a Raspberry Pi 3 to backup a 64GB memory card. Below is the script. I have it heavily commented and it creates logs of EVERYTHING. As I say: Logs or it didn’t happen. Save the below file to /home/pi/backup.sh

Assumptions

  • Assumes your memory card is called mmcblk0. If this is not the case, change it on line 8. You will need to dig through the /dev/ directory to locate it. On my Pi, with Raspian, it was mmcblk0.
  • Your USB flash drive will need to be mounted. Mine was called /dev/sda1 and I mounted it to /mnt/usb1. You can change the mount point to whatever you want by editing line 13.
  • Change line 24 to append something meaningful to you about the image file. Since my pi is running Pi-Hole and UniFi Cloud key services, I used _piholeunifi.img. Make it whatever you want.

Test It

You should test the script out before scheduling a cronjob. Execute the following command:

Watch for any errors. Screen output is also captured to a log file on the flashdrive, assuming it is mounted correctly and you updated the variables above. If the script produces errors, check the variables for the microSD card (MEMCARD) and the USB flashdrive mount point (BKLOCATE). Once you have resolved any errors, move on.

Automate It

Before you can execute it, you will need to give it execution permissions:

Copy the script, I called it backup.sh, to:

This script needs to execute as root, otherwise, it will fail. You will need to schedule a cron job as root:

At the bottom of your crontab file, enter:

Conclusion

This is a simple way to have a complete backup of your memory card as an image file that can be restored using a memory card writing program. This is not a fast process. If you require file level backup rather than an image file, you will want something else.

I should note that this script is not limited to Raspberry Pis. In theory, it will work on any Linux distribution.

BONUS: Digital Forensics

Because I am a digital forensics investigator, I try to preserve everything I can using the most complete method available. I have not tested this as a forensics method for collecting information off of a Raspberry Pi, but I assume it is pretty close. Of course, you know what they say about assuming. I will need to do some additional testing. I believe that since this is a live forensic capture, you will not be able to have consistent hash values of the microSD card and the resulting image. I will test that when I am able.

Future Work

I have a few ideas for some future work: Error handling (hey, I wrote this in about 30 minutes and have little bash scripting experience), Hashing, stop/start of services during the image creation process to minimize microSD card changes, encryption of backups, cloud upload.

2020-10-19 UPDATE:

After posting the original script, I realized I had an error in my code that only affected time stamping in the logs. I forgot that setting the time stamp variable LOGENTRY at one particular moment in time does not cause it to update, therefore, the time stamps in the log will be wrong. In the corrected version above, I remove the LOGENTRY variable in favor of calling the date command as log entries are created. The version has been incremented to 1.1.

Print Friendly, PDF & Email