Code: Select all
Filesystem Size Used Avail Use% Mounted on
/dev/sdb4 434G 113G 318G 26.0 [#....] /
/dev/sdb4 434G 113G 318G 26.0 [#....] /home
/dev/sdb4 434G 113G 318G 26.0 [#....] /var
Whilst I really should store one of these 3 SATA drives elsewhere, all 3 currently live on my window sill, next to my computer desk.
Then, I have a script, shown below which I execute, by hand, because it relies on my external drive to be mounted.
Code: Select all
./SystemBacukp.sh /mnt/usb/Data_01I was wondering if there are any suggestions as to a better way to do it.
Code: Select all
#!/bin/bash
backupFilesystem="${1:-/mnt/usb/Backup}"
if [ ! -d "${backupFilesystem}" ]
then
echo "$0: ${backupFilesystem} is not mounted."
exit 1
fi
echo "Starting backup at $(date) to ${backupFilesystem}"
backupDir="${backupFilesystem}/Backups"
SNAPSHOT="$(date +'%Y-%m-%d')"
echo "Snapshot = ${SNAPSHOT}"
snapshotsDir="/.snapshots/${SNAPSHOT}"
function CreateSnapshots
{
if [ -d /.snapshots/* ]
then
echo "Removing previous snapshots..."
# remove any snapshots left over from an aborted previous run
for s in /.snapshots/*/*
do
echo /sbin/btrfs subvolume delete "${s}"
/sbin/btrfs subvolume delete "${s}"
done
echo rm -fr /.snapshots/*
rm -fr /.snapshots/*
fi
# Want to backup boot, which is not normally mounted
if ! mountpoint -q /boot
then
/bin/mount /boot
fi
mkdir -p "${snapshotsDir}"
# create a read only snapshot
/sbin/btrfs subvolume snapshot -r / ${snapshotsDir}/root
/sbin/btrfs subvolume snapshot -r /home ${snapshotsDir}/home
/sbin/btrfs subvolume snapshot -r /var ${snapshotsDir}/var
}
function RemoveSnapshots
{
/sbin/btrfs subvolume delete ${snapshotsDir}/root
/sbin/btrfs subvolume delete ${snapshotsDir}/home
/sbin/btrfs subvolume delete ${snapshotsDir}/var
rmdir "${snapshotsDir}"
if mountpoint -q /boot
then
# no longer need /boot mounted
/bin/umount /boot
fi
}
CreateSnapshots
# Remove snapshots regardless of exit status
trap "RemoveSnapshots" EXIT
snapshots="/.snapshots/${SNAPSHOT}"
echo "Starting backup at $(date)"
for fileSystemName in root home var boot
do
case "${fileSystemName}" in
( "root" ) fileSystem="${snapshotsDir}/root" ;;
( "home" ) fileSystem="${snapshotsDir}/home" ;;
( "var" ) fileSystem="${snapshotsDir}/var" ;;
( "boot" ) fileSystem="/boot" ;;
( * ) echo "$0:Invalid Filesystem '${fileSystemName}'" >&2
exit 1
;;
esac
echo "Starting backup of ${fileSystem} at $(date)"
backupFile="${backupDir}/${fileSystemName}.tar.bz2"
previousBackupDir="${backupDir}/${fileSystemName}.prev"
#
## #########################################################################
## Backup previous backups in a directory - we don't really know how many
## files there are.
## #########################################################################
#
/bin/mkdir --parents "${previousBackupDir}"
#
## #########################################################################
## Remove any previous backups
## #########################################################################
#
/bin/rm --force "${previousBackupDir}"/*
#
## #########################################################################
## Save the current ones away
## #########################################################################
#
/bin/mv --force "${backupDir}"/${fileSystemName}.tar.bz2* "${previousBackupDir}"
#
## #########################################################################
## Backup the filesystem into 1gb chunks
## #########################################################################
#
/bin/tar --one-file-system --create --file - "${fileSystem}" | /bin/bzip2 --stdout | /usr/bin/split --bytes=1000m - "${backupFile}."
echo "Verifying backup of ${fileSystem} at $(date)"
/bin/cat "${backupFile}".* | /bin/tar jtf - > /dev/null 2>&1
rc=$?
if [ "$rc" -eq 0 ]
then
echo "Completed backup of ${fileSystem} at $(date)"
else
echo "**FAILED* backup of ${fileSystem} at $(date) - Did not verify!"
fi
done
# unmount usb disks as they seem to freeze after a period of disuse
/bin/mount -l | /bin/grep '/mnt/usb' | while read device on dir desc ; do /bin/umount -l ${dir} ; done
echo "Completed backup at $(date)"



