How to make a live backup of your Raspberry Pi Ubuntu/Raspberry Pi OS Server to create live bootable ISO Images on an external drive

Over the course of a few years, I have been teaching myself how to build secure and functional web servers on the Raspberry Pi. While building these systems, I have run into a problem. I have been unable to figure out how to fully backup my running Ubuntu 20.04 server system (including root files), into a bootable ISO file, in case of severe disaster or hackage. Until now, most tried solutions were using backup software that was either too complicated, or didn’t accomplish full ISO/disk image backups to an external disk. After some browsing of the Raspberry Pi Forums, I finally found the solution. A developer by the name of RonR on the Raspberry Pi Forums, has developed a simple set of scripts to accomplish exactly this… A FULL (Or SEVERAL FULL) bootable ISO Backups of a live running Ubuntu/Raspberry Pi OS linux system. You can find the original post with the downloadable scripts here in the raspberry pi forums.

What we are going to accomplish in this tutorial is the following:

  1. Create full daily bootable backups of your running Ubuntu or Raspberry Pi OS server in bootable ISO/IMG format, that will update themselves (using incremental updates) to the current live running server every 7 days (i.e. mondaybackup.img, is updated to reflect changes in the running OS since last Monday).
  2. Create full monthly bootable backups of your Raspberry Pi running Ubuntu Server or Raspberry Pi OS in bootable ISO format, that will update themselves to the current live running server every month (i.e. Monthy.iso, is update to reflect changes in the running OS since last Month).

Things required for this tutorial are a Raspberry Pi 3 or 4, an external USB Drive as big as possible & no smaller than 300GB in data capacity, and a machine (preferably a Raspberry Pi 4) running either Ubuntu Server 20.04 (untested on other versions of ubuntu) or Raspberry Pi OS. All of this tutorial will be done in the terminal on the command line. Now, LETS BEGIN!

1) SSH Into your Ubuntu/Raspberry Pi OS Server, and download “
Image File Utilities” (image-utils.zip) from https://forums.raspberrypi.com/viewtopic.php?t=332000#p1511694 the type the following command while inside your user folder (/home/yourusername/):

cd ~/ && wget -O image-files.zip "https://forums.raspberrypi.com/download/file.php?id=54873"

2) Create a separate folder, move the downloaded zip file to it, and decompress/extract the zipped files/scripts to that folder. Finally, delete the original zipped file.

mkdir -p ~/image-files && mv ~/image-files.zip ~/image-files && cd ~/image-files && sudo apt install unzip && unzip ~/image-files/image-files.zip && rm ~/image-files/image-files.zip

3) Change owner of scripts to root, and change permissions of scripts to be executable.

sudo chown root:root ~/image-files/image* && sudo chmod +x ~/image-files/image*

4) Create directory /usr/local/bin if it doesn’t exist, and move all of the unzipped files to that directory. Change to the newly created directory and rename README.txt to image-readme.txt.

sudo mkdir -p /usr/local/bin/ && mv ~/image-files/* /usr/local/bin/ && sudo mv /usr/local/bin/README.txt /usr/local/bin/image-readme.txt

5) Add /usr/local/bin to your $PATH if it is not already in your $PATH. If it is in your $PATH, skip this step. To check if it’s in your $PATH, run the following command…

echo $PATH

To add it to your path run the following command. This will add PATH=/usr/local/bin:$PATH to the bottom of your ~/.bashrc file, and then source it to enable it in your $PATH permanently.

echo 'PATH=/usr/local/bin:$PATH' >> ~/.bashrc && source ~/.bashrc

6) Now you have all of your scripts in the correct location with the correct permissions, it’s time to plug in your hard drive into either a mac, linux, or windows computer and format it as “exFAT” with the sceme being “Master Boot Record”. If you format in your linux desktop using gparted or gnome-disks, you may also format it as ext2 or ext4 filesystem. We prefer ext4. Now go ahead and format your external usb drive for backups, and then plug it into your Ubuntu/Raspian/debian server. Once it’s plugged into your server, continue to step enter the following command:

sudo blkid

My Output:

/dev/sda: LABEL="USBDRIVE" UUID="20737ag0-52b8-5d38-831b-9d570f0ffec4" TYPE="ext4" /dev/sdb1: LABEL_FATBOOT="system-boot" LABEL="system-boot" UUID="1CB3-D69A" TYPE="vfat" PARTUUID="4ad3ea62-01" /dev/sdb2: LABEL="writable" UUID="58ccb32d-ef91-4182-930d-e423439cf786" TYPE="ext4" PARTUUID="3ac8ed52-03" /dev/loop1: TYPE="squashfs" /dev/loop2: TYPE="squashfs" /dev/loop3: TYPE="squashfs" /dev/loop4: TYPE="squashfs" /dev/loop5: TYPE="squashfs" /dev/loop6: TYPE="squashfs" /dev/loop7: TYPE="squashfs" /dev/loop8: TYPE="squashfs" /dev/sdc1: LABEL="BACKUPDISK" UUID="521B-2C71" TYPE="exfat"

As you can see, my exFAT “BACKUPDISK” is device /dev/sdc1, and it has a UUID of 521B-2C71. Make note of this UUID.

7) Create the directory for your “BACKUPDISK“.

sudo mkdir -p /mnt/BACKUPDISK

8) Gain root privileges and backup your /etc/fstab file.

sudo su && cp /etc/fstab /etc/fstab.bak

Then add your BACKUPDISK to it’s own /etc/fstab entry to force the disk to auto-mount upon every boot. Do this by adding the UUID for /dev/sdc1 (or whatever your drive is) to the UUID Section in the below code, and then pressing enter.

echo 'UUID=YOUR-UUID-GOES-HERE-CHANGETHIS       /mnt/BACKUPDISK       ext4    defaults,noatime       0       1' >> /etc/fstab

In my case, the commmand looks like this:

echo 'UUID=521B-2C71       /mnt/BACKUPDISK       ext4    defaults,noatime       0       1' >> /etc/fstab

9) Next Mount your new USB Drive.

mount -a

Check to see that it mounted correctly.

lsblk
sdc 8:32 1 16M 0 disk
└─sdc1 8:33 1 16M 0 part /mnt/BACKUPDISK

Note: You may want to try rebooting at this point to be sure that your drive mounts automatically upon boot.

10) Create your initial backup image on your BACKUPDISK.

If running Ubuntu Server on a Raspberry Pi, run the following command to create your initial backup image:

sudo image-backup -u --initial /mnt/BACKUPDISK/00-sundaybackup.img,,8000

If running Raspberry Pi OS on a Raspberry Pi, run the following command to create your initial backup image:

sudo image-backup --initial /mnt/BACKUPDISK/00-sundaybackup.img,,8000

Wait for the backup to complete, then move on to the next step.

11) After the backup is complete, you will need to make a copy of the backup for each day of the week. You can run this single line command to do so. This may take a while, like a half hour to a few hours.

sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/01-mondaybackup.img && sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/02-tuesdaybackup.img && sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/03-wednesdaybackup.img && sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/04-thursdaybackup.img && sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/05-fridaybackup.img && sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/06-saturdaybackup.img

12) Make a copy of the initial image for your monthly backups. This may take a while.

sudo cp /mnt/BACKUPDISK/00-sundaybackup.img /mnt/BACKUPDISK/07-monthlybackup.img

13) Create a crontab as the sudo user to create incremental updates to each image for every day of the week, as well as the first of every month. This will keep your .img files updated with a clone of your OS that you were using prior to the current day of the week, as well as a clone of your OS that you were using prior to the current month. So you will have backups for every day of last week, and one single backup for the first of last month. Feel free to modify the crontab and disk images to suit your needs with backup times.

sudo crontab -e

For Ubuntu Server Users, copy and paste the following text into the bottom of your crontab window.

########################################
########## Full ISO Backups ############
########################################
# Incremental ISO Backup for every Sunday at 4am
0 4 * * 0 image-backup -u /mnt/BACKUPDISK/00-sundaybackup.img
# Incremental ISO Backup for every Monday at 4am
0 4 * * 1 image-backup -u /mnt/BACKUPDISK/01-mondaybackup.img
# Incremental ISO Backup for every Tuesay at 4am
0 4 * * 2 image-backup -u /mnt/BACKUPDISK/02-tuesdaybackup.img
# Incremental ISO Backup for every Wednesday at 4am
0 4 * * 3 image-backup -u /mnt/BACKUPDISK/03-wednesdaybackup.img
# Incremental ISO Backup for every Thursday at 4am
0 4 * * 4 image-backup -u /mnt/BACKUPDISK/04-thursdaybackup.img
# Incremental ISO Backup for every Friday at 4am
0 4 * * 5 image-backup -u /mnt/BACKUPDISK/05-fridaybackup.img
# Incremental ISO Backup for every Saturday at 4am
0 4 * * 6 image-backup -u /mnt/BACKUPDISK/06-saturdaybackup.img
# Incremental ISO Backup for every first of the month
@monthly image-backup -u /mnt/BACKUPDISK/07-monthlybackup.img

For Raspberry Pi OS Users, copy and past the following text into the bottom of your crontab window.

########################################
########## Full ISO Backups ############
########################################
# Incremental ISO Backup for every Sunday at 4am
0 4 * * 0 image-backup /mnt/BACKUPDISK/00-sundaybackup.img
# Incremental ISO Backup for every Monday at 4am
0 4 * * 1 image-backup /mnt/BACKUPDISK/01-mondaybackup.img
# Incremental ISO Backup for every Tuesay at 4am
0 4 * * 2 image-backup /mnt/BACKUPDISK/02-tuesdaybackup.img
# Incremental ISO Backup for every Wednesday at 4am
0 4 * * 3 image-backup /mnt/BACKUPDISK/03-wednesdaybackup.img
# Incremental ISO Backup for every Thursday at 4am
0 4 * * 4 image-backup /mnt/BACKUPDISK/04-thursdaybackup.img
# Incremental ISO Backup for every Friday at 4am
0 4 * * 5 image-backup /mnt/BACKUPDISK/05-fridaybackup.img
# Incremental ISO Backup for every Saturday at 4am
0 4 * * 6 image-backup /mnt/BACKUPDISK/06-saturdaybackup.img
# Incremental ISO Backup for every first of the month
@monthly image-backup /mnt/BACKUPDISK/07-monthlybackup.img

Exit and save crontab with “ctrl-x”, then press “y”, then hit “enter”.

FINALLY, your Raspberry Pi should be doing full backups of itself and converting them to the .img files in your external hard drive that we set up. Every day of the week, incremental backups will happen to each IMG File the backup corresponds to, leaving your backups no less than a week old at all times. Finally, you will also have an incremental backup corresponding to it’s img file for the first of every month. This will always leave you with a backup from the first of last month. I hope this tutorial helped and thanks to RonR for his backup scripts.

14) This is the end of the tutorial, and if it worked for you (which it should have), you could spread the love back and donate some change to my bitcoin address. Please send any BTC donations to:

bc1q3klg86zej8y852hp04qv8569k4fe45jpjfj763

Sources:https://forums.raspberrypi.com/viewtopic.php?t=332000