This website requires Javascript for some parts to function propertly. Your experience may vary.

Create a Backup Image from a running Tinker Board :: Portfolio

Quick overview how to create a backup Image of a Tinker Board, like a raspberry pi while its running.

Common ways to create backups of your tinker boards are to rsync specific files over to an external device like a NAS. Or to make a full copy of their SD card via an tool like Win32DiskImager. However both have their drawbacks, while the rsync approach is a good practise to have a backup of important files at the ready, e.g. when you have to revert a miss configuration, it will require you to re-configure a device from scratch in the case your sd card fails on you.

The full SD-card image on the other hand requires you to shutdown your device and block based backups like the ones of Win32DiskImager are also quite space consuming.

A good alternative to that is to perform the backup from your tinker board itself via Linux board tool dd & gzip. With it you can make a full compressed copy of your SD-card, meaning the backup wont have always the full size of your SD-card, but rather only the size of the files it contains.

You can execute this on a frequent basis from your device itself, which allows you to have a recent backup of your entire system at hand in the case you need it.

Preparation

For this kind of backup we should have access to some kind of external media to store our backup upon. Since its purpose is (at least for my use-case) to have an image snapshot of the device at hand in the case of an hardware failure. You could probably use an external USB stick, or hard drive that you connect to your tinker board, however I’m using a mounted network drive.

Configure NFS on the Synology

In this case I opted for a Synology based NFS mount, to configure this, login to the DSM Panel &:

  1. Go to Control Panel > File Services > NFS and check Enable NFS.
  2. Control Panel > Shared Folder and click on Edit at the Shared Folder, which you want to use for your Backups.
  3. On the panel that opens, go to the tab called NFS Rule and add a new rule with the IP address of your

Setup mount point on tinker board

So lets create our mount location & mount the drive:

sudo mkdir -p ${localFolder}
sudo mount -t nfs ${NASIP}:${NASFolder} ${localFolder}

Afterwards we need to update our fstab config to ensure this persists through a reboot. So open the file with:

sudo nano /etc/fstab

and enter the following text (adjusted to your environment)

${NASIP}:${NASFolder} ${localFolder} nfs defaults 0 0

Note: When you get an mount issue like: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program. You are porbably still missing nfs-common on your system. You can install this by running:

sudo apt install nfs-common -y

Actual Backup

Once we have our storage location mounted we can perform a backup by running the following command:

dd if=/dev/mmcblk0 bs=1MB | gzip -c > ${localFolder}/$(date +%Y%m%d-%H%M%S).img.gz

This will create a copy of your SD-card on the defined mountpoint. We use gzip to compress the empty parts of our card to receive a smaller image file. otherwise the resulting image would have the same size as our above mentioned Win32DiskImage clone.

However its a bit unpractical to do this manually. So lets do this via an cronjob instead:

sudo crontab -e
0 0 * * SUN (dd if=/dev/mmcblk0 bs=1MB | gzip -c > ${localFolder}/$(date +%Y%m%d-%H%M%S).img.gz) 2>&1	

That’s it already!

Note: Depending on your tinkerboard/os you might need to adjust the device name /dev/mmcblk0 to the ones of your system. You can see a list of all devices of your system by running:

lsblk

Clean-up old backups

If we don’t want to pill up an endless stack of old backup files it advisable to clean-up older backups after a while.

Depending on the importance and frequency of changes you are performing on them you might want to consider a higher, or lower amount of snapshots to go back to.

(cd ${localFolder} && ls -1t | tail -n +${AmountOfBackupsToKeep} | xargs -d '\n' rm -f)

Note: Amount of backups should be a integer +1. So 11, if you want to keep 10 files.

#tldr show me the code

You can obviously compress this into a shorter snippet:

localFolder=''
NASIP=''
NASFolder=''
AmountOfBackupsToKeep=4
mkdir -p ${localFolder}
mount -t nfs ${NASIP}:${NASFolder} ${localFolder}
echo "${NASIP}:${NASFolder} ${localFolder} nfs defaults 0 0" >> /etc/fstab
(crontab -u $(whoami) -l; echo "0 0 * * SUN (dd if=/dev/mmcblk0 bs=1MB | gzip -c > ${localFolder}/\$(date +%Y%m%d-%H%M%S).img.gz) 2>&1" ) | crontab -u $(whoami) -
(crontab -u $(whoami) -l; echo "0 1 * * SUN (cd ${localFolder} && ls -1t | tail -n +${AmountOfBackupsToKeep} | xargs -d '\n' rm -f) 2>&1" ) | crontab -u $(whoami) -

Hope thats helpful to someone 👍