Skip to content

Instantly share code, notes, and snippets.

@walthowd
Created June 20, 2018 17:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walthowd/014215f86d5695889b785494926bf7db to your computer and use it in GitHub Desktop.
Save walthowd/014215f86d5695889b785494926bf7db to your computer and use it in GitHub Desktop.
Home Assistant Rsync Backup with Dropbox
#!/bin/sh
#############################################################
# Home Assistant Rsync Backup Script
# 1.0 - 6/20/2018 - Walt Howd
#
# This script will backup via rsync
#
# Backups are created in a snapshot style with the latest
# backup being stored in backup.0, the previous backup in
# backup.1, etc.
#
#############################################################
##########################
# Configuration Variables
##########################
BACKUPDIR="/home/homeassistant/.homeassistant"
DESTINATION="/home/homeassistant/Dropbox/Backups/homeautomation/homeassistant"
EXCLUDES=" \
--exclude home-assistant.log \
--exclude home-assistant_v2.db \
--exclude deps/ \
"
#########################
# Setup enviroment
#########################
umask 077
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
export HOME=/root
#######################################
# Double check rsync-back.lock file
#######################################
if [ -f $DESTINATION/rsync-backup.lock ]; then
echo "Rsync Backup via rsync: FAILED"
echo ""
echo "Reason:"
echo "Lock file exists from previous run of rsync-backup.sh"
echo ""
echo "The lockfile in the backups location was present when"
echo "the backup script started. This could be from a"
echo "previous backup session that ended prematurely or"
echo "from another error. Please login to the system and"
echo "double check the lock file and clear if needed"
exit 1
fi
#######################################
# Make sure we can write our lock file
#######################################
touch $DESTINATION/rsync-backup.lock
if [ $? != 0 ]; then
echo "Rsync Backup via rsync: FAILED"
echo ""
echo "Reason:"
echo "Could not write lock file."
echo ""
echo "Could not write the lock file to the $DESTINATION"
echo "partition. Please check to make sure the partition "
echo "is not full."
exit 1
fi
#######################################
# Change the destination directory
#######################################
cd $DESTINATION
if [ $? != 0 ]; then
echo "Rsync Backup via rsync: FAILED"
echo ""
echo "Reason:"
echo "Could not change directory to $DESTINATION."
echo ""
exit 1
fi
#######################################
# Delete the oldest backup snapshot
#######################################
rm -rf ./backup.6
if [ $? != 0 ]; then
echo "Rsync Backup via rsync: FAILED"
echo ""
echo "Reason:"
echo "Could not delete the oldest backup snapshot."
exit 1
fi
#######################################
# Rotate the backups back a number
#######################################
for i in {5,4,3,2,1,0} ; do
test -d ./backup.$(($i)) && mv ./backup.$(($i)) ./backup.$(($i+1))
done
#######################################
# Run rsync to copy data to the latest
# snapshot direcotry.
########################################
rsync -avz --delete --link-dest=$DESTINATION/backup.1 --delete-excluded $EXCLUDES $BACKUPDIR $DESTINATION/backup.0 1>/tmp/rsync_output 2>&1
if [ $? = 0 ]; then
echo "$HOST Backup via rsync: SUCCEEDED"
echo ""
echo "The backup rsync process completed "
echo "without any errors."
echo ""
elif [ $? = 23 ]; then
echo "$HOST Backup via rsync: SUCCEEDED"
echo ""
echo "The backup rsync process completed "
echo "but only partially transferred files"
echo "to the backup host. This is normal with"
echo "temporary files and can be ignored."
echo ""
echo "You can examine the rsync log at "
echo "/tmp/rsync_output"
echo ""
elif [ $? = 24 ]; then
echo "$HOST Backup via rsync: SUCCEEDED"
echo ""
echo "The backup rsync process completed "
echo "but some files vanished before they"
echo "could be transferred. This is normal"
echo "with system in use can can be ignored."
echo ""
echo "You can examine the rsync log at "
echo "/tmp/rsync_output"
echo ""
else
echo "$HOST Backup via rsync: FAILED"
echo ""
echo "The backup rsync process did NOT "
echo "return a normal exit code. The exit "
echo "code returned was $?"
echo ""
echo "You can examine the rsync log at "
echo "/tmp/rsync_output"
echo ""
fi
#######################################
# Remove the lock file
#######################################
rm -f $DESTINATION/rsync-backup.lock
@SpartanWithin
Copy link

SpartanWithin commented Sep 24, 2020

Hello, the section

#######################################
# Rotate the backups back a number
#######################################
for i in {5,4,3,2,1,0} ; do
  test -d ./backup.$(($i)) && mv ./backup.$(($i)) ./backup.$(($i+1))
done

doesn't seem to work, I get the error arithmetic expression: expecting primary: "{5,4,3,2,1,0}".

The solution seems to be for i in 5 4 3 2 1; do.

Thank you for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment