Skip to content

Instantly share code, notes, and snippets.

@Sol1du2
Created December 13, 2021 14:26
Show Gist options
  • Save Sol1du2/964c0dbb413049b8b1c03851097aedf1 to your computer and use it in GitHub Desktop.
Save Sol1du2/964c0dbb413049b8b1c03851097aedf1 to your computer and use it in GitHub Desktop.
A backup script for my IOTstack services. I call the system EDI hence the name of the file :)
#!/bin/sh
# Note: If a password exists for the ssh connection this will be queried every
# time we connect via rsync which can be annoying. Consider a public/private
# key solution without a passphrase if you want to automate it.
set -e
if [ $# -lt 2 ]; then
echo "Usage: backup-edi.sh <host> <destination>"
exit 1
fi
HOST=$1
HA_DEST="$2/home-assistant"
IOTSTACK_DEST="$2/iotstack"
check_and_create_dir() {
if [ ! -e $1 ]; then
mkdir $1
elif [ ! -d $1 ]; then
echo "$1 already exists but is not a directory" 1>&2
exit 1
fi
}
check_and_create_dir $HA_DEST
check_and_create_dir $IOTSTACK_DEST
echo "starting backups..."
# Backup the sqlite database first via the sqlite backup command so that we
# don't get a corrupt database due to it being used by HA itself.
echo "creating HA sqlite backup..."
ssh $HOST << EOF
sqlite3 ./volumes/home_assistant/home-assistant_v2.db ".backup '/home/pi/home-assistant-backup.sqlite'"
EOF
echo "HA sqlite backup created"
echo "downloading HA data..."
# Use sudo rsync so we can copy root only files.
# Exclude .db files so that we don't copy the database twice since we are
# already downloading the backup we created earlier.
rsync -a -e ssh --rsync-path="sudo rsync" --exclude="*.db*" "$HOST:/home/pi/volumes/home_assistant /home/pi/home-assistant-backup.sqlite" $HA_DEST
echo "HA data downloaded"
echo "downloading IOTStack data..."
rsync -a -e ssh "$HOST:/home/pi/IOTstack/compose-override.yml /home/pi/IOTstack/docker-compose.yml" $IOTSTACK_DEST
echo "IOTStack data downloaded"
echo "all backups done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment