Backup

Introduction

Let's assume you have a Windows PC or NAS that you want to use to backup your PI

Also you want a mechanism that won't blindly image a 64GB sd card and take up 64GB on the backup.
The following notes describe one way of using image-utils to drive rsync.

image-utils can create an initial backup image (without storing empty partition space) and then do incremental backups appending only the files that have changed.

I'm also assuming that you already use ssh via PuTTY to connect to your Pi and know how to execute basic commands in a shell

We need to create a share that is writeable by remote clients - I'll call it \\NAS\share but it might simply be \\YOUR-PC\share

For now, download and unzip the image-utils attachment from RonR's post at https://www.raspberrypi.org/forums/viewtopic.php?p=1528736 into the share

The folder is also a useful place to hold copies of files ssl and wpa_supplicant.conf (see Restore)

Quick Start to Backup

Create a prepare-share.sh script for the pi user to prepare the mounted share, substituting appropriate values below for NAS and username and password and DownPeaks (the latter is my choice of name for my backup)

#!/usr/bin/env bash

sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /boot

sudo touch /boot/ssl

sudo umount -a -t cifs -l

[ -d /mnt/windowshare ] && sudo rmdir /mnt/windowshare

sudo mkdir /mnt/windowshare

sudo mount.cifs //NAS/share /mnt/windowshare -o username=username,password=password

n.b.(1) The bold highlighted statement lines above are just a precaution to ensure a best effort to mount the windowshare cleanly.
Anecdotal observation suggests that if /mnt/windowshare is offline (especially if it is a CIFS resource over WiFi) then wrongly assuming the /mnt/windowshare is available and try to use it can sometimes hang the linux file system.

n.b.(2) Alternatively username=username,password=password can be replaced by credentials=/etc/win-credentials (or any filename) containing username=username password=password (on seperate lines)
Prudent to set the permissions on /etc/win-credentials so that only root can read!


Execute the script and verify that you have full access to the //NAS/share.

Copy the image-utils folder from the remote share to ~/image-utils on the pi

Add these to the pi user's crontab using crontab -e

@reboot sleep 15 && sudo ~/prepare-share.sh

30 1 * * * sudo ~/image-utils/image-backup -o --progress /mnt/windowshare/DownPeaks.img | sudo tee -a /mnt/windowshare/DownPeaks.log

The @reboot sleep 15 will cause a short delay before the prepare-share.sh script is executed.

Now is a good time to reboot the Pi !

Check that you can still access the //NAS/share after the Pi comes back up and you have reconnected successfully with PuTTY

Doing the Initial Backup

Decide on your backup image name, best to make it related to the name of your Pi especially important when you have more than one and you want to keep multiple backups in the same \\NAS\share

Obviously here replace my choice of DownPeaks by your own ...

sudo ~/image-utils/image-backup --initial /mnt/windowshare/DownPeaks.img

The first time you use image-utils it may prompt for a dependency gdisk if you don't already have it on your Pi...

The backup for now proceed, as you can see my Pi image is called DownPeaks.img

Incremental Backup

You'll need to keep the image file as-is if you want to do incremental updates

The command to do an incremental update is similar, but does not have the -i flag

sudo ./image-backup /mnt/windowshare/DownPeaks.img

This command can be run at any time but it is recommended that it is run from crontab as per the example above in Quick Start to Backup

Adjust the scheduled time to suit your requirements - I find around 1:30 am best for me

If you want to see filenames when image-backup executes use these additional flags (but not worth it on an initial image creation - it's too verbose)

-o --progress

Fortunately, the img file can be significantly compressed (From time to time I use 7Zip and create a timestamped compressed version, which I move elsewhere for safety) You may get as much as 30-40% compression.

I also found it useful to do a minor modification to the image-utils/image-backup script, to show a timestamp in the output

#!/bin/bash

echo $(date -u) "image-backup started"

trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM