puppet/rsync/templates/rsync-backup.sh.erb

35 lines
1.2 KiB
Text

#!/bin/bash
LOGFILE="/srv/rsync-log/<%= @_log_host -%>-$(date +"%F-%T").log"
FLOCK="/usr/bin/flock"
FLOCK_OPTS="--exclusive --nonblock"
FLOCK_FILE="/var/lock/<%= @name -%>"
RSYNC="/usr/bin/rsync"
RSYNC_OPTS=( -axR <%= @enable_acl ? "-A" : "" -%> <%= @options ? options : "" -%> --delete --itemize-changes <%= @_excludes -%> )
(
$FLOCK $FLOCK_OPTS 200 || exit $?
/usr/bin/printf "***** <%= @_source -%> -> <%= @_target -%>\n"
/usr/bin/printf "***** Backup started %s\n\n" "$(date +"%F %T UTC")"
/usr/bin/time -p $RSYNC "${RSYNC_OPTS[@]}" <%= @_source -%> <%= @_target %>
RSYNCEXIT=$?
if [ $RSYNCEXIT -ne 0 ]; then
echo "ERROR: rsync job (<%= @name -%>) process returned $RSYNCEXIT"
fi
/usr/bin/printf "\n***** Backup ended %s\n" "$(date +"%F %T UTC")"
) 200>$FLOCK_FILE >$LOGFILE 2>&1
FLOCKEXIT=$? # flock's return code
# Look for unknown lines in log
/bin/egrep -v "^$|([^ ]{11} )|([*]{5} )|(\*deleting )|(real|user|sys)" $LOGFILE >/dev/null 2>&1
if [ $? -eq 0 ]; then
/bin/egrep -v "^([^ ]{11} )" $LOGFILE
fi
if [ $FLOCKEXIT -ne 0 ]; then
echo "ERROR: Rsync job (<%= @name -%>) already running or other error (exit code $FLOCKEXIT)"
exit $FLOCKEXIT
fi