Skip to content

Instantly share code, notes, and snippets.

@solusipse
Created January 9, 2016 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solusipse/bd14b3af8648ca433b50 to your computer and use it in GitHub Desktop.
Save solusipse/bd14b3af8648ca433b50 to your computer and use it in GitHub Desktop.
# This is a very simple script for performing whole disk backups. Rotation scheme: 3 daily backups, 2 weekly and 2 monthly.
# Edit to suit your needs. The only non-standard requirement is sshfs.
TARGET="1.1.1.1"
MAIL="my@mail.com"
sshfs root@$TARGET:/backup/disk /root/mnt
if [ $# -eq 0 ]; then
echo "Run with argument: daily, weekly or monthly."
exit
fi
if [ $1 == "daily" ]; then
if [ -f /root/mnt/disk.2.backup ]; then
mv /root/mnt/disk.2.backup /root/mnt/disk.3.backup
fi
if [ -f /root/mnt/disk.1.backup ]; then
mv /root/mnt/disk.1.backup /root/mnt/disk.2.backup
fi
if [ -f /root/mnt/disk.0.backup ]; then
mv /root/mnt/disk.0.backup /root/mnt/disk.1.backup
fi
OUTPUT="disk.0.backup"
elif [ $1 == "weekly" ]; then
if [ -f /root/mnt/disk.week.1.backup ]; then
mv /root/mnt/disk.week.1.backup /root/mnt/disk.week.2.backup
fi
if [ -f /root/mnt/disk.week.0.backup ]; then
mv /root/mnt/disk.week.0.backup /root/mnt/disk.week.1.backup
fi
OUTPUT="disk.week.0.backup"
elif [ $1 == "monthly" ]; then
if [ -f /root/mnt/disk.month.1.backup ]; then
mv /root/mnt/disk.month.1.backup /root/mnt/disk.month.2.backup
fi
if [ -f /root/mnt/disk.month.0.backup ]; then
mv /root/mnt/disk.month.0.backup /root/mnt/disk.month.1.backup
fi
OUTPUT="disk.month.0.backup"
else
echo "Wrong argument!"
exit
fi
dd if=/dev/sda bs=16M | gzip -3 | ssh -p 22 root@$TARGET "dd of=/backup/disk/$OUTPUT bs=16M"
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo "Whole disk backup failed at domain.com!" | mail -s "Backup error" $MAIL
else
if [ -f /root/mnt/disk.3.backup ]; then
rm -rf /root/mnt/disk.3.backup
fi
if [ -f /root/mnt/disk.week.2.backup ]; then
rm -rf /root/mnt/disk.week.2.backup
fi
if [ -f /root/mnt/disk.month.2.backup ]; then
rm -rf /root/mnt/disk.month.bacup
fi
fi
umount /root/mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment