From 17baa237f992948214c552a3d74d313f5cfe79c1 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 5 Apr 2020 15:43:32 +0200 Subject: [PATCH] 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. --- rsync-backup/rsync-backup.conf.example | 14 +++++ rsync-backup/rsync-backup.sh | 78 ++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 rsync-backup/rsync-backup.conf.example create mode 100755 rsync-backup/rsync-backup.sh diff --git a/rsync-backup/rsync-backup.conf.example b/rsync-backup/rsync-backup.conf.example new file mode 100644 index 0000000..46d3172 --- /dev/null +++ b/rsync-backup/rsync-backup.conf.example @@ -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='' diff --git a/rsync-backup/rsync-backup.sh b/rsync-backup/rsync-backup.sh new file mode 100755 index 0000000..341e7b5 --- /dev/null +++ b/rsync-backup/rsync-backup.sh @@ -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 " + 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