If you consider these 2 facts it will open you up to other options:
- You can mount the same device in multiple directories simultaneously.
- You can create
tmpfs
mounts anywhere you want to mask a directory.
So, yes there are many paths that you don't want to backup. You avoid most of them by mounting your primary partition at a 2nd directory. Others will still have data you don't want to backup like /tmp
, /dev
, and /var/log
.
My process went like this...
- SSH into the Raspberry Pi
Mount exactly want I wanted to backup
sudo mount /dev/mmcblk0p2 /tmp/root/
sudo mount -t tmpfs -o size=1m tmpfs /tmp/root/var/log/
sudo mount -t tmpfs -o size=1m tmpfs /tmp/root/dev/
sudo mount -t tmpfs -o size=1m tmpfs /tmp/root/tmp/
sudo mount /dev/mmcblk0p1 /tmp/root/boot/
- Exit the machine
Copy the data over via ssh+tar
ssh pi@raspberry.local 'cd /tmp/root; sudo tar cf - * | gzip;' | pv > rpi.tgz
# NOTE: The `pv` command gives you a progress meter but can be left out.
Remove the temporary mounts
for m in /var/log/ /dev/ /boot/ /; do sudo umount /tmp/root${m}; done
Once you are happy with the results, you can put it all in a single file like ~/backup.sh
#!/bin/bash -eu
dir=$(mktemp -d)
cleanup(){
cd /tmp/ # You can't umount or rm a directory while you are in it.
for m in /dev/ /tmp/ /var/log/ /boot/ /; do
sudo umount ${dir}${m}
done
rm -rf ${dir}
}
do_mounts(){
sudo mount /dev/mmcblk0p2 ${dir}/
sudo mount -t tmpfs -o size=1m tmpfs ${dir}/dev/
sudo mount -t tmpfs -o size=1m tmpfs ${dir}/tmp/
sudo mount -t tmpfs -o size=1m tmpfs ${dir}/var/log/
sudo mount /dev/mmcblk0p1 ${dir}/boot/
}
send_data(){
cd ${dir}; sudo tar cf - * | gzip | tee >(md5sum > /tmp/backup.md5);
}
give_feedback(){
awk '{print "MD5:", $1}' < /tmp/backup.md5 >&2
}
trap cleanup EXIT INT TERM
do_mounts
send_data
give_feedback
And calling goes like this...
$ ssh pi@raspberry.local ./backup.sh | pv | tee rpi.tgz | md5sum | awk '{print "MD5:", $1}'
MD5: d3d9181374f3ec8e4e721c786eca9f71
348MB 0:04:50 [ 1.2MB/s] [ <=> ]
MD5: d3d9181374f3ec8e4e721c786eca9f71
HINT: While you are experimenting, change tar cf - *
to tar cf - etc
to save yourself a bunch of time on each test run.