Skip to content

Instantly share code, notes, and snippets.

@ToMe25
Last active April 4, 2022 17:09
Show Gist options
  • Save ToMe25/c872aea6571f048c9b15bf3c6c17fa02 to your computer and use it in GitHub Desktop.
Save ToMe25/c872aea6571f048c9b15bf3c6c17fa02 to your computer and use it in GitHub Desktop.
A simple rsync based bash script to create backups on a ftp server.
#!/bin/bash
# Change to where you want your backup to be.
# This script will create two backups in there, called "Backup1" and "Backup2".
# Note that the directory you set here has to exists, while Backup1 and Backup2 do not matter.
BACKUP_DIR="ftp_user@ftp_server:/some/path/for/backups/"
# The directory in which the directories to be backed up(specified in backup.txt) are.
BACKUP_ROOT_DIR=..
# The mac address to wake up using Wake on Lan.
# Comment out to disable Wake on Lan.
#WAKE_MAC="01:23:45:67:89:AB"
# The time to wait for the target device to wake up.
# Can be skipped by running the script with --skip-wait as the first arg.
WAKE_TIME="10m"
if [ ! -z ${WAKE_MAC+x} ]; then
echo "Waking device $WAKE_MAC."
wakeonlan $WAKE_MAC
if [ ${1-x} != "--skip-wait" ]; then
echo "Waiting $WAKE_TIME for the device to boot."
sleep $WAKE_TIME
fi
fi
SCRIPT_DIR=$(dirname "$0")
echo "Moving to script location($SCRIPT_DIR)."
cd $SCRIPT_DIR
MISSING=false
if [ ! -f backup.txt ]; then
echo "Missing backup.txt file!"
echo "Add one folder to copy per line."
MISSING=true
fi
if [ ! -f blacklist.txt ]; then
echo "Missing blacklist.txt file!"
echo "Look at 'man rsync' for the format for this file, or just add one file to exclude per line."
echo "This file can be empty if you don't want to exclude any files from your backup."
MISSING=true
fi
if $MISSING; then
exit 1
fi
if (( 10#$(date +%j) % 2 == 0 )); then
BACKUP_DIR="$BACKUP_DIR/Backup2/"
else
BACKUP_DIR="$BACKUP_DIR/Backup1/"
fi
echo "Starting backup."
rsync -avPEhtlrum --copy-unsafe-links --delete --stats --exclude-from=blacklist.txt --files-from=backup.txt $BACKUP_ROOT_DIR $BACKUP_DIR
echo "Backup done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment