Added rsync-backup.sh script and example config

This script can be executed in a cronjob and to regularly backup certain predefined files. AFter backing up, the script will send an email to inform the admin about the backup.
This commit is contained in:
Dennis Potter 2020-04-05 15:43:32 +02:00
commit 17baa237f9
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,14 @@
#################################################################
# Config
#################################################################
EMAIL='' # Send confirmation email to this address
SSH_HOST='' # Target host
SSH_USER='' # User on target host
SSH_KEY='' # location of SSH key
SSH_PORT='22'
declare -a SRC_DIRS=(
#"/home"
)
DST_DIR=''

78
rsync-backup/rsync-backup.sh Executable file
View File

@ -0,0 +1,78 @@
#!/bin/bash
#################################################################
# Check availability of software
#################################################################
function availability {
if [[ ! $(command -v $1) ]]; then
echo Error: '$1' is not available but required. Please install it!
exit 1
fi
}
availability mail
availability rsync
#################################################################
# Get arguments
#################################################################
missingArg()
{
echo "Error: Please define the configuration to be used!"
echo " Usage: $0 -c <configuration_file>"
exit 1
}
while getopts "c:" opt
do
case "$opt" in
c ) CONFIG_FILE="$OPTARG" ;;
? ) missingArg ;;
esac
done
# Check if config was empty
if [ -z "$CONFIG_FILE" ]
then
missingArg
fi
# Check if config file exists
if [ ! -f $CONFIG_FILE ]
then
echo "$CONFIG_FILE does not exist!"
exit 1
fi
source $CONFIG_FILE
#################################################################
# Loop through array and invoke rsync
#################################################################
date_styled=$(date -d "today" +"%Y%m%d_%H%M")
for i in "${SRC_DIRS[@]}"
do
echo "" >> /tmp/rsync_log_$date_styled
echo "======================================================" >> /tmp/rsync_log_$date_styled
echo "${i^^}" >> /tmp/rsync_log_$date_styled
echo "======================================================" >> /tmp/rsync_log_$date_styled
rsync -av --delete --rsh="ssh -p $SSH_PORT -i $SSH_KEY" $i $SSH_USER@$SSH_HOST:$DST_DIR >> /tmp/rsync_log_$date_styled
done
#################################################################
# Send email with information and hashes
#################################################################
sed -i '1s/^/Hey there!\n\nI created a new backup. See the log below.\n/' /tmp/rsync_log_$date_styled
mail -s "Backup created ($date_styled)" $EMAIL < /tmp/rsync_log_$date_styled
#################################################################
# Delete log
#################################################################
rm -f /tmp/rsync_log_$date_styled
exit 0