Categories :

Complete Backup of MaxAir on a running system + Incremental Backups

Last Updated on 17/12/2023 by Kevin Agar

This has now been superseded by the install program on MaxAir, but it is still relevant for backing up other pi projects.

Rather than remove the sd card, copy it, then reinstall the card to restart the system, it would be nice if you could backup the running system to a NAS and do incremental backups. This is a description of how this can be accomplished using the set of utilities found in image-utils.zip

Thanks go to RonR for the original scripts written in image-utils.zip
If you want any further information about the scripts then visit https://forums.raspberrypi.com/viewtopic.php?t=332000
To download the scripts then go to
https://forums.raspberrypi.com/viewtopic.php?t=332000

Download image-utils.zip which is at the bottom of the 1st post

Copy zip file to /home/maxair on the pi and unzip them to /usr/local/bin

If /usr/local/bin doesn’t exist create it

maxair@maxair:~ $ sudo mkdir /usr/local

maxair@maxair:~ $ sudo mkdir /usr/local/bin


maxair@maxair:~ $ sudo unzip /home/maxair/image-utils.zip -d /usr/local/bin/
Make the files executable
maxair@maxair:~ $ sudo chmod +x /usr/local/bin/image-*

Map a folder on the pi to a folder on the NAS

maxair@maxair:~ $ sudo mkdir /mnt/pibackups

This folder needs to be in /mnt or /media as these aren’t included in the backup.

Create a directory/folder on the NAS that you want mapped to the folder on the pi

In my case it’s //192.168.1.254/Volume_1/MaxAir_Backups

To enable auto connection to the mapped folder, add a line to /etc/fstab and save

maxair@maxair:~ $ sudo nano /etc/fstab

Enter into the file and save.

//192.168.1.254/Volume_1/MaxAir_Backups /mnt/pibackups cifs nofail,noatime,vers=1.0,username=****,password=****,uid=www-data,gid=www-data 0 0

  • //192.168.1.254/Volume_1/MaxAir_Backups – NAS address and directory to map to
  • /mnt/pibackups – MaxAir folder to map from. This folder needs to be in /mnt or /media as these aren’t included in the backup.
  • nofail – System will still boot up even if connecting to the NAS fails
  • Noatime – Stop changing the file read time. (Improves performance)
  • vers=1.0 – Used to specify which version of SAMBA is used. Due to my NAS being old, and working on SAMBA v1.0 I was getting lots of errors, unable to connect etc. Adding this removed the problems
  • username=****,password=**** – The credentials for logging into the NAS
  • 0 0 – dump – A number indicating whether and how often the file system should be backed up by the dump program; a zero indicates the file system will never be automatically backed up. –pass – A number indicating the order in which the fsck program will check the devices for errors at boot time: 0=do not check
  • uid=www-data,gid=www-data – set permissions for user www-data

Ctrl x, Y, return to save the file.
Reboot the pi.
To test, add any small file to the folder /Volume_1/MaxAir_Backups on the NAS
log into the pi via ssh and go to the folder /mnt/pibackups
maxair@maxair:~ $ cd /mnt/pibackups
maxair@maxair:~ $ ls

You should see the file you added to the NAS at /Volume_1/MaxAir_Backups

To do an initial backup of the complete raspberry pi (excluding the /mnt and /media folders)

maxair@maxair:~ $ sudo /usr/local/bin/image-backup

Image file to create? /mnt/pibackups/maxair_backup_15_10_2022.img

Initial image file ROOT filesystem size (MB) [5127]? – if you’re adding space for incremental backups then you should also increase this. I set it to 6000

Added space for incremental updates after shrinking (MB) [0]? 100 – 100Mb should be enough for incremental backups

Create /mnt/pibackups/maxair_backup_15_10_2022.img (y/n)? y

Starting full backup (for incremental backups, run: /etc/local/bin/image-backup /mnt/pibackups/maxair_backup_15_10_2022.img)

maxair@maxair:~ $

Backup finishes without any errors

2022-10-16_122942.jpg

As it states above, to do an incremental backup just include the original file name in the command

maxair@maxair:~ $ sudo /usr/local/bin/image-backup /mnt/pibackups/maxair_backup_15_10_2022.img

To restore the system use either Win32DiskImager, balenaEtcher, or Raspberry Pi Imager and add ssh file to the boot partition to enable logging in with ssh.

A couple of caveats

Because maxair is dynamic and adds data to the database continually, any backup will miss some of the latest data added to the image. This is unavoidable but a small price to pay. Subsequent incremental backups, which will be a lot quicker, should capture the missing data.

All mapped folders should be in either /mnt or /media. Anywhere else will cause the backup to fail

If you run the backup on a different pi, MQTT will not work. This is because the MQTT password is encrypted using the mac address of the pi. Deleting and reinstalling MQTT should get it working again.

I think a complete backup should be done on a fairly regular basis, perhaps once a month, or just before an update, and incremental backups between this time, maybe weekly.

To try and get this working as a scheduled task I created 2 files, maxairimage.sh and maxairincremental.sh which can be run via a cron job.

maxairimage.sh

#! /bin/bash
#create the variable
var=`date +"%FORMAT_STRING"`
#set the format of the variable
now=`date +"%d-%m-%Y-%H:%M"`
#Print out the variable
#echo "${now}"
#Create the initial backup with the date added to the end of the file name
/usr/local/bin/image-backup -i /mnt/pibackups/maxair_backup_${now},,100.img

maxairincremental.sh

#! /bin/bash
#find the latest image file
FILES=`find /mnt/pibackups/maxair_back* -type f | sort -n | cut -d' ' -f 2- | tail -n 1`
#echo "${FILES}"
# find /dir/path -type f finds all the files in the directory
# | is a shell pipe (see man bash section on Pipelines)
# sort -n means to sort on the first column and to treat the token as numerical instead of lexicographic (see man sor># cut -d' ' -f 2- means to split each line using the   character and then to print all tokens starting at the second ># NOTE: -f 2 would print only the second token
# tail -n 1 means to print the last line (see man tail)
#run the incremental backup on the latest image file
/usr/local/bin/image-backup ${FILES}

Leave a Reply

Your email address will not be published. Required fields are marked *