Automate homebridge backups on RPI

This guide assumes you have a server you can backup to that’s running Linux (i.e. FreeNAS / TrueNAS / Synology / etc.)

I won’t go into the details of how to mount NFS/CIFS shares on the RPI as it’s really pre-requisite knowledge, and if you don’t know how to mount the backup location, then the rest of this guide isn’t going to make sense anyway.

The following script will recreate the internal homebridge backup script without the need to trigger it via the UI. It assumes you’ve mounted your backup location at /mnt/backups

Replace <SOURCE> <DEST> below with the source of your homebridge data, and the destination of the backup.

rsync -avhr --delete --exclude={'homebridge.log','package.json','node_modules','backups/instance-backups'} <SOURCE> <DEST>

A standard usage example for a Raspberry Pi running Homebridge using the standard install method:

rsync -avhr --delete --exclude={'homebridge.log','package.json','node_modules','backups/instance-backups'} /var/lib/homebridge /mnt/backups

More complex options are available, i.e. if you don’t have a Linux NAS you could .tgz the backup and send it to an Amazon S3 bucket or similar. There are other guides out there that tell you how to mount an s3 bucket, you could then combine it with auto pruning and backup rotation scripts to keep GFS backups.

Putting it into a cron job (the following cron job would run at 00:01 every day):

Open the cron editor:

sudo crontab -e

Paste in the following and update to match the changes you made earlier (note the need for the shell path above the corn job otherwise it wont expand the braces and the excludes will fail):

SHELL=/bin/bash
1 0 * * * rsync -avhr --delete --exclude={'homebridge.log','package.json','node_modules','backups/instance-backups'} /var/lib/homebridge /mnt/backups

Automatically pruning and rotating logs Code here for your benefit edit as you see fit. Was unable to locate the true author of this script as it’s been posted all over the web and modified a lot. If you know who wrote it let me know in the comments. Assumptions: -It assumes you’ve set the earlier scripts to run somewhere like /my/backups/latest -Your source below would then be /backups/homebridge/latest -Destination would be /backups/homebridge

#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################

# What to backup. 
backup_files="/your/source/location"

# Where to backup to.
dest="/your/backup/location"

# Setup variables for the archive filename.
day=$(date +%A)
hostname=homebridge

# Find which week of the month 1-4 it is.
day_num=$(date +%-d)
if (( $day_num <= 7 )); then
        week_file="$hostname-week1.tgz"
elif (( $day_num > 7 && $day_num <= 14 )); then
        week_file="$hostname-week2.tgz"
elif (( $day_num > 14 && $day_num <= 21 )); then
        week_file="$hostname-week3.tgz"
elif (( $day_num > 21 && $day_num < 32 )); then
        week_file="$hostname-week4.tgz"
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
        month_file="$hostname-month2.tgz"
else
        month_file="$hostname-month1.tgz"
fi

# Create archive filename.
if [ $day_num == 1 ]; then
	archive_file=$month_file
elif [ $day != "Saturday" ]; then
        archive_file="$hostname-$day.tgz"
else 
	archive_file=$week_file
fi

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar Pczf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.