Skip to content

Instantly share code, notes, and snippets.

@gtrabanco
Last active February 20, 2023 09:50
Show Gist options
  • Save gtrabanco/ca5128e2e559a9e80adcedb406281950 to your computer and use it in GitHub Desktop.
Save gtrabanco/ca5128e2e559a9e80adcedb406281950 to your computer and use it in GitHub Desktop.
Script to do a backup of unifi or protect files with udm
#!/usr/bin/env sh
{ # Ensure all script is loaded
#
# Backup unifi or protect configuration files to a remote server
# You should install first the public key remotely.
#
# To do ssh authentication if you have a Cloud Key Gen2, it is like any other
# debian. Just use "ssh-keygen" and "ssh-copy-id"
#
# To do ssh authentication (with UDM) you need to generate new keys
# due to dropbear keys are not compatible
# $ dropbearkey -t rsa -f /mnt/data/ssh/id_rsa.backup.private
#
# Copy your public key of the generated key, to print the key just:
# $ dropbearkey -t rsa -f /mnt/data/ssh/id_rsa.backup.private -y | grep ^ssh-rsa
#
# Paste the public key in a new line of your "$HOME/.ssh/authorized_keys" of your remote server
#
# After that you should configure this script and copy in your desired path, but remember to
# change the params. By default is configured to be in: /mnt/data/on_boot.d/S99-backnifi.sh
#
# If you do not want protect backups, you should comment line 68. If you do not want unifi
# backups, you should comment line 65.
#
# 1st time exec the script manually
#
# This script do not provide a way to rotate backups, compressing or whatever but it should be
# added easily.
#
# Can't wait to see your customized or improved version of this script!
#
# Author: Gabriel Trabanco Llano <gtrabanco@fwok.org>
# Website: http://gabi.io | https://git.io/JUEgZ
#
#
# Configuration with defaults
#
REMOTE_USER="backnifi"
REMOTE_SERVER="my.remote"
REMOTE_UNIFI_DIR="~/backups/unifi/" # Must exists first
REMOTE_PROTECT_DIR="~/backups/protect/" # Must exists first
# minute hour dayOfMonth month dayOfTheWeek command
CRON_COMMAND="35 07 * * * /mnt/data/on_boot.d/S00-backnifi.sh"
# With CKG2 change to /etc/init.d/S99-backnifi.sh
IDENTITY_FILE="/mnt/data/ssh/id_rsa.backup.private" # With CKG2
# use /root/.ssh/id_rsa
CRON_FILE="/etc/cron.d/backnifi"
COMMAND="scp -pr" # command to copy
LOCAL_UNIFI_DIR="/mnt/data/unifi-os/unifi/data/backup/autobackup/*" # autobackup*.conf
# With CKG2 use: /data/unifi/*
LOCAL_PROTECT_DIR="/mnt/data_ext/unifi-os/unifi-protect/backups/*" # unifi_protect_backup<version><datetime>.zip files here
# datetime with format 4 digit Year 2 digit Month 2 Digit Day 7 Digit Number
# Sample namefile: unifi_protect_backup.v1.14.12-beta.4.202009190009000.zip
#
# With CKG2 use: /data/unifi-protect/backups/*
#
# Program
#
# Copy known hosts if present in $HOME dir
if [[ -f "$HOME/.ssh/known_hosts" ]]; then
cp -f "$HOME/.ssh/known_hosts" "/mnt/data/ssh/"
fi
if [[ ! -f "$HOME/.ssh/known_hosts" ]]; then
cp -f "/mnt/data/ssh/known_hosts" "$HOME/.ssh/known_hosts"
fi
# Unifi Backups
${COMMAND} -i "${IDENTITY_FILE}" ${LOCAL_UNIFI_DIR} ${REMOTE_USER}@${REMOTE_SERVER}:"${REMOTE_UNIFI_DIR}" > /dev/null
# Protect Backups
${COMMAND} -i "${IDENTITY_FILE}" ${LOCAL_PROTECT_DIR} ${REMOTE_USER}@${REMOTE_SERVER}:"${REMOTE_PROTECT_DIR}" > /dev/null
# Setup a crontab
if [ ! -f "${CRON_FILE}" ]; then
sleep 300
echo "${CRON_COMMAND}" > "/etc/cron.d/backnifi"
fi
# End
} # End of ensure all script is loaded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment