Browse Source

Complete rewrite. README still has to be updated.

master
Christian Baer 1 year ago
parent
commit
7a51c28bb2
6 changed files with 416 additions and 171 deletions
  1. +12
    -7
      README.md
  2. +1
    -0
      newsyslog.conf.d/rmbackup.conf
  3. +53
    -0
      rmbackup.conf.dist
  4. +1
    -0
      rmbackup.d/general.exclude.dist
  5. +19
    -24
      rmbackup.d/sample.conf.dist
  6. +330
    -140
      rmbackup.sh

+ 12
- 7
README.md View File

@@ -19,7 +19,7 @@ There has to be a user on the remote server that is able to run rsync with root
$ visudo

# /etc/sudoers
Cmnd_Alias RSYNC = /usr/bin/rsync
%rmbackup ALL=(ALL) NOPASSWD: RSYNC

@@ -62,12 +62,12 @@ There has to be a user on the remote server that is able to run rsync with root
# Additional command line parameters for ssh (verbose mode, exclude patterns...
# see man rsync for further information)
# RSYNC_CONF=(-v)
- Change the path to _rmbackup.d_ in rmbackup.sh:

# rmbackup.sh
...
# Where to look for config files
CONFLOCATION=/etc/rmbackup.d/*.conf
@@ -82,7 +82,7 @@ There has to be a user on the remote server that is able to run rsync with root
#
SHELL=/usr/local/bin/bash
PATH=/etc:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root
#minute hour mday month wday command
#
15 */1 * * * /home/rmbackup/bin/rmbackup.sh --backup-files
@@ -98,13 +98,13 @@ File backups will now be taken hourly. The backup of the MySQL databases will be
- Create a new user that is only able to read and lock the databases.

$ mysql
> grant select, lock tables on *.* to 'rmbackup'@'localhost' identified by 'password';
> grant select, lock tables on *.* to 'rmbackup'@'localhost' identified by 'password';

- We don't wan't to type the password every time the backup runs. So we create a file called .my.cnf in the home directory of the backup user on the remote server. MySQL takes the login credentials from this file


$ vim ~.my.cnf
# ~/.my.cnf
[client]
@@ -116,6 +116,11 @@ File backups will now be taken hourly. The backup of the MySQL databases will be

## Changelog

**2019-04-03**

- Complete rewrite, no backwards compatibility.
- Removed all unused stuff and made this thing full POSIX compliant.

**2018-01-04**

- Made shebang more portable


+ 1
- 0
newsyslog.conf.d/rmbackup.conf View File

@@ -0,0 +1 @@
/var/log/rmbackup.log 644 7 1024 * J

+ 53
- 0
rmbackup.conf.dist View File

@@ -0,0 +1,53 @@
## rmbackup.conf

## Where to look for host configs (default: /usr/local/etc/rmbackup.d)
#CONF_LOCATION="/usr/local/etc/rmbackup.d"

## Path to the PID file (default: /var/run/rmbackup.pid)
#PID_FILE="/var/run/rmbackup.pid"

## Format for timstamp that are used to name the backups (default: %Y-%m-%d)
## Defaults to YYYY-MM-DD. When you set this to include the hours and minutes as well, you'll have more granular backups (e.g. hourly).
#BACKUP_TIMESTAMP_FORMAT="%Y-%m-%d"

## Path to directory where we should store the logs (default: /var/log)
#LOG_DIR="/var/log"

## File name for the log file (default: rmbackup.log)
#LOG_FILE="rmbackup.log"

## Timestamp format that is used for logs (default: %Y-%m-%d %H:%M:%S)
#LOG_DATE_FORMAT="%Y-%m-%d %H:%M:%S"

# Directory where we store the backups. Sub directories are created for every host that will be backed up.
#BACKUPS_DIR="/tank/data/backups"

## Name that's used for the symlink of the latest backup (default: last)
#LAST="last"

## Name that's used for the directory of a backup that is in progress (default: inProgress)
#INPROG="inProgress"

## How many days of backups should we keep in place (default: 14)
#GLOBAL_KEEP_DAYS=14

## Path to local rsync binary (default: /usr/local/bin/rsync)
#RSYNC_PATH="/usr/local/bin/rsync"

## Default parameters for rsync (default: --del --quiet). Can be expanded in host configs
#RSYNC_CONF_DEFAULT="--del --quiet"

## Path to local ssh binary (default: /usr/bin/ssh)
#SSH_PATH="/usr/bin/ssh"

# Default path to the binary that's used for getting more privileges on the remote server like doas or sudo (default: /usr/local/bin/doas)
#REMOTE_PRIVILEGES_PATH="/usr/local/bin/doas"

## Default path to the rsync binary on the remote server (default: /usr/local/bin/rsync)
#REMOTE_RSYNC_PATH="/usr/local/bin/rsync"

## Turn on verbose mode (default: false)
#VERBOSE="false"

## Turn on debug mode (default: false)
#DEBUG="false"

+ 1
- 0
rmbackup.d/general.exclude.dist View File

@@ -0,0 +1 @@
.ccache

+ 19
- 24
rmbackup.d/sample.conf.dist View File

@@ -1,33 +1,28 @@
#Sample conf for backup script
#Sample host config for rmbackup

## SSH config for the remote server (default port is 22)
## The hostname of the server
#SSH_SERVER="host2342.example.com"

# The user that logs in to the remote server
SSH_USER="rmbackup"
## An alias that should be used for the output and in logs. If not set, hostname will be used.
#SSH_ALIAS="example.com"

# The hostname or IP of the remote server
SSH_SERVER="example.com"
## The user that should be used for establishing the SSH connection
#SSH_USER="rmbackup"

# The SSH port of the remote server
SSH_PORT=22
## Additional parameters for rsync (e.g. the keyfile for passwordless logins)
#SSH_ARGS="-i .ssh/id_ed25519_rmbackup@host2342.example.com"

# Arguments for SSH (i.e. your keyfile)
SSH_ARGS=""
## The SSH port of the remote server (default: 22)
#SSH_PORT=22

# Which folders sould be backed up from the remote server?
REMOTE_SOURCES=(/root /etc /home /var)
## Path to the binary that's used for getting more privileges on the remote server like doas or sudo (default: /usr/local/bin/doas)
#PRIVILEGES_PATH="/usr/local/bin/doas"

# In which folder should we store the backups on the backup server
# (subdirs for the server will be created by the script)?
## Path to the rsync binary on the remote server (default: /usr/local/bin/rsync)
#RSYNC_PATH="/usr/local/bin/rsync"

TARGET="/Backups/"
## Additional parameters for rsync (eg. verbose mode, exclude patterns). See man rsync for further information
#RSYNC_CONF="--ipv6 --exclude-from=/usr/local/etc/rmbackup.d/general.exclude"

# Additional command line parameters for ssh (verbose mode, exclude patterns...
# see man rsync for further information)
# RSYNC_CONF=(-v)

# Uncomment and add your address if you want to receive the script ouput via e-mail
#MAILREC="jdoe@example.com"

# How many backups should we keep (in days)
KEEP=14
# Directories that should be included in the backups. The list must be separated by spaces. Spaces in the path must be escaped ("\ ").
# REMOTE_SOURCES="/root /etc /home /var"

+ 330
- 140
rmbackup.sh View File

@@ -1,9 +1,9 @@
#!/usr/bin/env sh

# Remote server backup with rsync and config files
# Based on http://wiki.ubuntuusers.de/Skripte/Backup_mit_RSYNC
## rmbackup.sh
# Create incremental backups of remote servers with ssh, rsync and config files

# Copyright 2013 Christian Busch
# Copyright 2013 Christian Baer
# http://github.com/chrisb86/

# Permission is hereby granted, free of charge, to any person obtaining
@@ -25,158 +25,348 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Where to look for config files
CONFLOCATION=/root/bin/rmbackup.d/*.conf

# Give the paths to used tools
SSH="/usr/bin/ssh"; LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date";
MAIL="/usr/bin/mail"; RSYNC="/usr/local/bin/rsync";
LAST="last"; INC="--link-dest=../$LAST"

# Date format for naming the snapshot folders
TODAY=$($DATE +%Y-%m-%d)

# set some rsync parameters
RSYNC_CONF_DEFAULT=(--rsync-path='sudo rsync' --delete --quiet)

LOG=/tank/Backups/rmbackup.log

### Do not edit below this line ###
rmbackup=`basename -- $0`
rmbackup_pid=$$
rmbackup_conf_file="/usr/local/etc/rmbackup.conf"

VERBOSE="${VERBOSE:-false}"
DEBUG="${DEBUG:-false}"
log_date_format="%Y-%m-%d %H:%M:%S" # For logging purposses when config file isn't there

rmbackup_usage_backup="Usage: $rmbackup backup [-f CONFIGFILE] [-vd]"
rmbackup_usage_cleanup="Usage: $rmbackup cleanup [-f CONFIGFILE] [-vd]"

# Show help screen
# Usage: help exitcode
help () {
echo "Usage: $rmbackup command {params}"
echo
echo "backup Backup all defined hosts."
echo " [-f CONFIGFILE] Backup only specified host."
echo " [-v] Turn on verbose mode."
echo " [-d] Turn on debug mode (includes -v)."
echo "cleanup Cleanup old backups."
echo " [-f CONFIGFILE] Cleanup only specified hosts backups."
echo " [-v] Turn on verbose mode."
echo " [-d] Turn on debug mode (includes -v)."
echo "help Show this screen"
exit $1
}

# Print and log messages when verbose mode is on
# Usage: chat [0|1|2|3] MESSAGE
## 0 = regular output
## 1 = error messages
## 2 = verbose messages
## 3 = debug messages

chat () {
messagetype=$1
message=$2
log=$log_dir/$log_file
log_date=$(date "+$log_date_format")

if [ $messagetype = 0 ]; then
echo "[$log_date] [INFO] $message" | tee -a $log ;
fi
#
if [ $messagetype = 1 ]; then
echo "[$log_date] [ERROR] $message" | tee -a $log ; exit 1;
fi

while [ $# -gt 0 ]; do # Until we run out of parameters . . .
if [ $1 = "--backup-mysql" ] || [ $1 = "-m" ]; then
DOBACKUPMYSQL="true"
elif [ $1 = "--backup-files" ] || [ $1 = "-f" ]; then
DOBACKUPFILES="true"
elif [ $1 = "--cleanup" ] || [ $1 = "-c" ]; then
CLEANUP="true"
elif [ $1 = "--verbose" ] || [ $1 = "-v" ]; then
VERBOSE="true"
fi
shift # Check next set of parameters.
done

# Loop through configs
for f in $CONFLOCATION
do
# Set some defaults
SSH_PORT=22
KEEP=14

# load config file
source $f

# some path fiddeling
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
TARGET=$TARGET/
fi
if [ $messagetype = 2 ] && [ "$VERBOSE" = true ]; then
echo "[$log_date] [INFO] $message" | tee -a $log
fi

if [ "$SSH_USER" ] && [ "$SSH_PORT" ]; then
S="$SSH -p $SSH_PORT -l $SSH_USER $SSH_ARGS";
if [ $messagetype = 3 ] && [ "$DEBUG" = true ]; then
echo "[$log_date] [DEBUG] $message" | tee -a $log
fi
}

# Load config file and set default variables
# Usage: init [CONFIGFILE]
init () {

[ ! -f "$rmbackup_conf_file" ] && chat 1 "Config file $rmbackup_conf_file not found. Exiting."

. $rmbackup_conf_file

rmbackup_conf_location="${CONF_LOCATION:-/usr/local/etc/rmbackup.d}"
rmbackup_pid_file="${PID_FILE:-/var/run/rmbackup.pid}"
backup_timestamp_format="${BACKUP_TIMESTAMP_FORMAT:-%Y-%m-%d}"
log_dir="${LOG_DIR:-/var/log}"
log_file="${LOG_FILE:-rmbackup.log}"
log_date_format="${LOG_DATE_FORMAT:-%Y-%m-%d %H:%M:%S}"
backups_dir="$BACKUPS_DIR"
backups_path_last="${LAST:-last}"
backups_path_inprog="${INPROG:-inProgress}"
rsync="${RSYNC_PATH:-/usr/local/bin/rsync}"
rsync_conf_default="${RSYNC_CONF_DEFAULT:---del --quiet}"
ssh="${SSH_PATH:-/usr/bin/ssh}"
remote_privileges_path_default="${REMOTE_PRIVILEGES_PATH:-/usr/local/bin/doas}"
remote_rsync_path_default="${REMOTE_RSYNC_PATH:-/usr/local/bin/rsync}"
global_keep="${GLOBAL_KEEP_DAYS:-14}"

chat 2 "Starting $rmbackup with PID $rmbackup_pid."

chat 3 "rmbackup: $rmbackup"
chat 3 "rmbackup_pid: $rmbackup_pid"
chat 3 "rmbackup_conf_file: $rmbackup_conf_file"
chat 3 "rmbackup_conf_location: $rmbackup_conf_location"
chat 3 "rmbackup_pid_file: $rmbackup_pid_file"
chat 3 "backup_timestamp_format: $backup_timestamp_format"
chat 3 "log_dir: $log_dir"
chat 3 "log_file: $log_file"
chat 3 "log_date_format: $log_date_format"
chat 3 "backups_dir: $backups_dir"
chat 3 "backups_path_last: $backups_path_last"
chat 3 "backups_path_inprog: $backups_path_inprog"
chat 3 "rsync: $rsync"
chat 3 "rsync_conf_default: $rsync_conf_default"
chat 3 "ssh: $ssh"
chat 3 "remote_privileges_path_default: $remote_privileges_path_default"
chat 3 "remote_rsync_path_default: $remote_rsync_path_default"
chat 3 "global_keep :$global_keep"
chat 3 "VERBOSE: $VERBOSE"
chat 3 "DEBUG: $DEBUG"

if [ -z "$backups_dir" ]; then chat 1 "Target directory for backups not set. Please set it in $rmbackup_conf_file!"; fi
}

# Check if script is already running
# Usage: checkpid
checkPID () {

touch $rmbackup_pid_file

# Get stored PID from file
rmbackup_stored_pid=`cat $rmbackup_pid_file`

# Check if stored PID is in use
rmbackup_pid_is_running=`ps aux | awk '{print $2}' | grep $rmbackup_stored_pid`

chat 3 "rmbackup_pid: $rmbackup_pid"
chat 3 "rmbackup_stored_pid: $rmbackup_stored_pid"
chat 3 "rmbackup_pid_is_running: $rmbackup_pid_is_running"

if [ "$rmbackup_pid_is_running" ]; then
# If stored PID is already in use, skip execution
chat 1 "Skipping because $rmbackup is running (PID: $rmbackup_stored_pid)."
else
# Update PID file
echo $rmbackup_pid > $rmbackup_pid_file
chat 0 "Starting work."
fi
}

# Link the specified directory to $backups_path_last
# Usage: backup_host DIRECTORY
link_last () {
link_source_dir=$1

chat 2 "Symlinking $link_source_dir to $backup_target/$backups_path_last."
chat 3 "ln -nsf $link_source_dir $backup_target/$backups_path_last"
ln -nsf "$link_source_dir" "$backup_target/$backups_path_last"

unset link_source_dir
}

# Loads a config file and backups the host
# Usage: backup_host CONFIGFILE
backup_host () {

config_file=$1

# Check if config file exists and exit if not.
if [ ! -f "$1" ] ; then chat 1 "Config file $config_file not found."; fi

chat 2 "Loading Config file $config_file"
chat 3 "config_file: $config_file"

# Load config file
. $config_file

ssh_port="${SSH_PORT:-22}"
privileges="${PRIVILEGES_PATH:-$remote_privileges_path_default}"
remote_rsync_path="${RSYNC_PATH:-$remote_rsync_path_default}"
ssh_server="$SSH_SERVER"
ssh_alias="${SSH_ALIAS:-$SSH_SERVER}"
ssh_user="$SSH_USER"
ssh_args="$SSH_ARGS"
rsync_conf="${rsync_conf_default} ${RSYNC_CONF}"
backup_target="$backups_dir/$ssh_alias"
backup_last="$backup_target/$backups_path_last"
backup_timestamp=$(date "+$backup_timestamp_format")
remote_sources=${REMOTE_SOURCES}
latest_backup=`find $backup_target -type d \( ! -iname "$backups_path_inprog" ! -iname "$backups_path_last" ! -iname ".*" \) -maxdepth 1 -print | sort -r | head -n 1`

chat 3 "ssh_port: $ssh_port"
chat 3 "privileges $privileges"
chat 3 "remote_rsync_path: $remote_rsync_path"
chat 3 "ssh_server: $ssh_server"
chat 3 "ssh_alias: $ssh_alias"
chat 3 "ssh_user: $ssh_user"
chat 3 "ssh_args: $ssh_args"
chat 3 "rsync_conf: ${rsync_conf}"
chat 3 "backup_target: $backup_target"
chat 3 "backup_last: $backup_last"
chat 3 "backup_target: $backup_target"
chat 3 "backup_last: $backup_last"
chat 3 "backup_timestamp: $backup_timestamp"
chat 3 "remote_sources: ${remote_sources}"
chat 3 "latest_backup: $latest_backup"

chat 0 "Starting backup for $ssh_alias (using config file $config_file)"

## Prepare the target directory
if [ ! -d "$backup_target/$backups_path_inprog" ]; then
chat 2 "Preparing target directory."

if [ -d "$backup_target/$backup_timestamp" ]; then
chat 2 "Current backup directory exists. Moving $backup_target/$backup_timestamp to $backup_target/$backups_path_inprog."
chat 3 "mv $backup_target/$backup_timestamp $backup_target/$backups_path_inprog"
mv "$backup_target/$backup_timestamp" "$backup_target/$backups_path_inprog"

link_last "$backup_target/$backups_path_inprog"
else
chat 2 "Creating $backup_target/$backups_path_inprog."
chat 3 "mkdir -p $backup_target/$backups_path_inprog"
mkdir -p "$backup_target/$backups_path_inprog"
fi
else
chat 2 "Target directory $backup_target/$backups_path_inprog exists"
fi

TARGET=$TARGET$SSH_SERVER/

## Check if $backups_path_last symlink exists in backup folder.
## If not, symlink the last complete backup to $backups_path_last
## If no backup exists, link $backups_path_inprog to $backups_path_last
## Create target folder if it doesn't exist
mkdir -p $TARGET

if [ "$CLEANUP" == "true" ]; then

$ECHO $($DATE)" starting cleanup for "$SSH_SERVER" (using config file "$f")" >> $LOG

find $TARGET -type d \( ! -iname "mysql" ! -iname "last" ! -iname ".*" \) -maxdepth 1 -mtime +$KEEP -exec rm -r '{}' '+'
find $TARGET/mysql/ -type d -maxdepth 1 -mtime +$KEEP -exec rm -r '{}' '+'
fi

if [ "$DOBACKUPFILES" == "true" ]; then

$ECHO $($DATE)" starting file backup for "$SSH_SERVER" (using config file "$f")" >> $LOG

# do the backup
for SOURCE in "${REMOTE_SOURCES[@]}"
do
if [ "$S" ] && [ "$SSH_SERVER" ] && [ -z "$TOSSH" ]; then
if [ "$VERBOSE" == "true" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$SSH_SERVER:$SOURCE\" ${RSYNC_CONF_DEFAULT[@]} ${RSYNC_CONF[@]} $TARGET$TODAY $INC" >> $LOG
fi
$RSYNC -e "$S" -avR "$SSH_SERVER:\"$SOURCE\"" "${RSYNC_CONF_DEFAULT[@]}" "${RSYNC_CONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
done

# move the folders and link "last"
if ( [ "$S" ] && [ "$SSH_SERVER" ] ) || ( [ -z "$S" ] ); then
if [ "$VERBOSE" == "true" ]; then
$ECHO "$LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
fi
$LN -nsf "$TARGET"$TODAY "$TARGET"$LAST >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
if [ ! -L "$backup_target/$backups_path_last" ]; then
chat 2 "$backup_target/$backups_path_last not found."
if [ -d "$latest_backup" ] && [ "$latest_backup" -ne "$backup_target" ]; then
chat 2 "Latest backup is $latest_backup."
link_last "$latest_backup"
else
chat 2 "Latest backup not found."
link_last "$backup_target/$backups_path_inprog"
fi
$ECHO $($DATE)" finished file backup for "$SSH_SERVER"." >> $LOG
fi

# Backup MySQL databases
for source in ${remote_sources}
do
chat 2 "Backing up $ssh_alias:$source to $backup_target/$backups_path_inprog."
chat 3 "$rsync -e \"$ssh -p $ssh_port -l $ssh_user $ssh_args\" --link-dest=\"$backup_last\" -aR $ssh_server:$source --rsync-path=$privileges $remote_rsync_path ${rsync_conf} \"$backup_target/$backups_path_inprog\""

if [ "$DOBACKUPMYSQL" == "true" ]; then
$rsync -e "$ssh -p $ssh_port -l $ssh_user $ssh_args" --link-dest="$backup_last" -aR "$ssh_server:$source" --rsync-path="$privileges $remote_rsync_path" ${rsync_conf} "$backup_target/$backups_path_inprog"
done

# Check if there's a .my.cnf on the remote server
HASMYCNF=`$S $SSH_SERVER "test -e ~/.my.cnf && echo 1 || echo 0"`
if [ $? -ne 0 ]; then
ERROR=1
#[TODO] Implement error Handling. Mail?
else
chat 2 "Finishing backup."

# Run mysql backup for all databases if we have a .my.cnf
if [ ${HASMYCNF} = 1 ]; then
## Rename transfer dir to backup_timestamp
chat 3 "mv $backup_target/$backups_path_inprog $backup_target/$backup_timestamp"
mv "$backup_target"/"$backups_path_inprog" "$backup_target"/"$backup_timestamp"

$ECHO $($DATE)" starting mysql backup for "$SSH_SERVER"." >> $LOG
link_last "$backup_target/$backup_timestamp"

# Create the backup target if it doesn't exist
TARGET="$TARGET/mysql/$TODAY"
mkdir -p $TARGET
chat 0 "Finished backup for $ssh_alias."

# Get databases
DATABASES=$($S $SSH_SERVER mysql -Bse "'show databases;'")
chat 2 "Cleaning up variables."
chat 3 "unset ssh_port privileges remote_rsync_path ssh_server ssh_alias ssh_user ssh_args rsync_conf backup_target backup_last backup_timestamp remote_sources latest_backup SSH_PORT PRIVILEGES_PATH RSYNC_PATH SSH_SERVER SSH_ALIAS SSH_USER SSH_ARGS RSYNC_CONF REMOTE_SOURCES ERROR"

# dump the compressed databases to target
for D in $DATABASES;
do
if [ "$D" != "information_schema" ] && [ "$D" != "performance_schema" ]; then
if [ "$VERBOSE" == "true" ]; then
$ECHO "$S $SSH_SERVER mysqldump $D | gzip -c > $TARGET/$D.sql.gz" >> $LOG
fi
$S $SSH_SERVER mysqldump $D | gzip -c > $TARGET/$D.sql.gz
fi
done
$ECHO $($DATE)" finished mysql backup for "$SSH_SERVER"." >> $LOG
else
$ECHO $($DATE)" Couldn't find .my.cnf on "$SSH_SERVER". Skipping MySQL backup." >> $LOG
fi
unset ssh_port privileges remote_rsync_path ssh_server ssh_alias ssh_user ssh_args rsync_conf backup_target backup_last backup_timestamp remote_sources latest_backup SSH_PORT PRIVILEGES_PATH RSYNC_PATH SSH_SERVER SSH_ALIAS SSH_USER SSH_ARGS RSYNC_CONF REMOTE_SOURCES ERROR
fi

# Unset server specific variables
unset SSH_USER
unset SSH_SERVER
unset SSH_PORT
unset SSH_ARGS
unset S
unset REMOTE_SOURCES
unset TARGET
unset RSYNC_CONF
unset DATABASES
unset D
unset hasMycnf
unset MAILREC
unset ERROR
unset KEEP

# send mail if it's configured
if [ -n "$MAILREC" ]; then
if [ $ERROR ];then
$MAIL -s "Error Backup $SSH_SERVER $LOG" $MAILREC < $LOG
else
$MAIL -s "Backup $SSH_SERVER $LOG" $MAILREC < $LOG
fi
}

# Sources a config file and cleans up the backups for this specific host that are older than $keep days
# Usage: backup_host CONFIGFILE
cleanup_host () {

#[TODO] What if all backups are older than $keep days? Prevent from deleting the last backups!

config_file=$1

# Check if config file exists and exit if not.
if [ ! -f "$1" ] ; then chat 1 "Config file $config_file not found."; fi

chat 2 "Loading Config file $config_file"
chat 3 "config_file: $config_file"

# Load config file
. $config_file

ssh_server="$SSH_SERVER"
ssh_alias="${SSH_ALIAS:-$SSH_SERVER}"
backup_target="$backups_dir/$ssh_alias"
keep="${KEEP_DAYS:-$global_keep}"

chat 3 "ssh_server: $ssh_server"
chat 3 "ssh_alias: $ssh_alias"
chat 3 "backup_target: $backup_target"
chat 3 "keep: $keep"

chat 2 "Deleting backups in $backup_target that are older than $keep days."
chat 3 "find $backup_target -type d \( ! -iname \"$backups_path_inprog\" ! -iname \"$backup_last\" ! -iname \".*\" \) -maxdepth 1 -mtime +$keep -exec rm -r '{}' '+'"

find $backup_target -type d \( ! -iname "$backups_path_inprog" ! -iname "$backup_last" ! -iname ".*" \) -maxdepth 1 -mtime +$keep -exec rm -r '{}' '+'
}

case "$1" in
######################## rmbackup.sh HELP ########################
help)
help 0
;;
######################## rmbackup.sh BACKUP ########################
backup)
shift; while getopts :f:vd arg; do case ${arg} in
f) config_file=${OPTARG};;
v) VERBOSE=true;;
d) DEBUG=true; VERBOSE=true;;
?) init; chat 1 "$rmbackup_usage_backup";;
:) init; chat 1 "$rmbackup_usage_backup";;
esac; done; shift $(( ${OPTIND} - 1 ))

init
checkPID

if [ -n "$config_file" ]; then
backup_host $config_file
else
for f in $rmbackup_conf_location/*.conf
do
backup_host $f
done
fi
chat 2 "All jobs done. Exiting."
;;
######################## rmbackup.sh CLEANUP ########################
cleanup)
shift; while getopts :f:vd arg; do case ${arg} in
f) config_file=${OPTARG};;
v) VERBOSE=true;;
d) DEBUG=true; VERBOSE=true;;
?) init; chat 1 "$rmbackup_usage_cleanup";;
:) init; chat 1 "$rmbackup_usage_cleanup";;
esac; done; shift $(( ${OPTIND} - 1 ))

init
checkPID

if [ -n "$config_file" ]; then
cleanup_host $config_file
else
for f in $rmbackup_conf_location/*.conf
do
cleanup_host $f
done
fi
done
chat 2 "All jobs done. Exiting."
;;
*)
help 1
;;
esac

Loading…
Cancel
Save