Skip to content

Instantly share code, notes, and snippets.

@eikenb
Created January 30, 2021 20:26
Show Gist options
  • Save eikenb/7d9e1b8cd2aa763eae89f86fdc8c7f37 to your computer and use it in GitHub Desktop.
Save eikenb/7d9e1b8cd2aa763eae89f86fdc8c7f37 to your computer and use it in GitHub Desktop.
create live backup on external usb drive
#!/bin/sh
set -e
if [ "$(id -u)" -ne 0 ]
then
echo "Rerunning with sudo..."
fullpath="$(readlink -f $0)"
exec sudo $fullpath
fi
# the usb drive's filesystem is labelled with 'mirror' so I can grep for it
while true; do
cat /proc/mounts | grep mirror && break
echo "waiting for mirror drive mount..."
sleep 1
done
# mount location of usb/backup drive
ROOT=/media/data/mirror
set +e
rsync_success=0
/usr/bin/rsync -vPaxSHAX --delete --numeric-ids \
--exclude /home/jae/Private/ \
/ $ROOT
rsync_success="$?"
set -e
# NOTE for disk move
# Mount from rescue disk
# Make filesystem
#mkfs.ext4 /mnt/new
# Sync data
#/usr/bin/rsync -PaxSHAX --delete --numeric-ids /mnt/old/ /mnt/new
# Then do the below, except without the fstab change.
### Make backup bootable
# change to use the mirror disk
sed -i -e 's/LABEL=root/LABEL=mirror/' ${ROOT}/etc/fstab
# required mounts
for fs in dev dev/pts sys proc ; do
mount -o bind /${fs} ${ROOT}/${fs}
done
# dpkg-reconfigure is only needed if you need to run grub-install
#sudo chroot ${ROOT} dpkg-reconfigure grub-pc
chroot ${ROOT} update-grub
chroot ${ROOT} update-initramfs -t -u -k all
for fs in sys proc dev/pts dev ; do
umount ${ROOT}/${fs}
done
# done
if [ $rsync_success -gt 0 ]
then
echo "Rsync failed. Keep mount to re-run? [y/n]"; read reply
case $reply in Y*|y*) exit 1 ;; *) ;; esac
fi
umount -v $ROOT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment