From 8fe1f405e915c1f37a810371a86ed39cfc7eddbe Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Wed, 25 Sep 2013 17:01:24 -0700 Subject: samba_backup: fix bug, add command line parameter, improve error messages Also remove .bak suffix from tdb/ldb backups for more consistent restore procedures Reviewed-by: Andreas Schneider Reviewed-by: Andrew Bartlett Reviewed-by: Matthieu Patou Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Sat Oct 5 13:51:34 CEST 2013 on sn-devel-104 --- source4/scripting/bin/samba_backup | 52 +++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 15 deletions(-) (limited to 'source4') diff --git a/source4/scripting/bin/samba_backup b/source4/scripting/bin/samba_backup index 8f9cc838a77..3e22abecd9e 100755 --- a/source4/scripting/bin/samba_backup +++ b/source4/scripting/bin/samba_backup @@ -15,24 +15,39 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +# Revised 2013-09-25, Brian Martin, as follows: +# - Allow retention period ("DAYS") to be specified as a parameter. +# - Allow individual positional parameters to be left at the default +# by specifying "-" +# - Use IS0 8601 standard dates (yyyy-mm-dd instead of mmddyyyy). +# - Display tar exit codes when reporting errors. +# - Don't send error messages to /dev/null, so we know what failed. +# - Suppress useless tar "socket ignored" message. +# - Fix retention period bug when deleting old backups ($DAYS variable +# could be set, but was ignored). + + FROMWHERE=/usr/local/samba WHERE=/usr/local/backups +DAYS=90 # Set default retention period. if [ -n "$1" ] && [ "$1" = "-h" -o "$1" = "--usage" ]; then - echo "samba_backup [provisiondir] [destinationdir]" - echo "Will backup your provision located in provisiondir to archive stored in destinationdir" + echo "samba_backup [provisiondir] [destinationdir] [retpd]" + echo "Will backup your provision located in provisiondir to archive stored" + echo "in destinationdir for retpd days. Use - to leave an option unchanged." echo "Default provisiondir: $FROMWHERE" echo "Default destinationdir: $WHERE" + echo "Default destinationdir: $DAYS" exit 0 fi -[ -n "$1" -a -d "$1" ]&&FROMWHERE=$1 -[ -n "$2" -a -d "$2" ]&&WHERE=$2 +[ -n "$1" -a "$1" != "-" ]&&FROMWHERE=$1 # Use parm or default if "-". Validate later. +[ -n "$2" -a "$2" != "-" ]&&WHERE=$2 # Use parm or default if "-". Validate later. +[ -n "$3" -a "$3" -eq "$3" 2> /dev/null ]&&DAYS=$3 # Use parm or default if non-numeric (incl "-"). DIRS="private etc sysvol" #Number of days to keep the backup -DAYS=90 -WHEN=`date +%d%m%y` +WHEN=`date +%Y-%m-%d` # ISO 8601 standard date. if [ ! -d $WHERE ]; then echo "Missing backup directory $WHERE" @@ -52,24 +67,31 @@ for d in $DIRS;do find $relativedirname -name "*.ldb.bak" -exec rm {} \; for ldb in `find $relativedirname -name "*.ldb"`; do tdbbackup $ldb - if [ $? -ne 0 ]; then - echo "Error while backuping $ldb" + Status=$? # Preserve $? for message, since [ alters it. + if [ $Status -ne 0 ]; then + echo "Error while backing up $ldb - status $Status" exit 1 fi done - tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --exclude=*.ldb >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2" + # Run the backup. + # --warning=no-file-ignored set to suppress "socket ignored" messages. + tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --exclude=\*.ldb --warning=no-file-ignored --transform 's/.ldb.bak$/.ldb/' + Status=$? # Preserve $? for message, since [ alters it. + if [ $Status -ne 0 -a $Status -ne 1 ]; then # Ignore 1 - private dir is always changing. + echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 - status = $Status" exit 1 fi find $relativedirname -name "*.ldb.bak" -exec rm {} \; else - tar cjf ${WHERE}/${n}.${WHEN}.tar.bz2 $relativedirname >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo "Error while archiving ${WHERE}/${n}.${WHEN}.tar.bz2" + # Run the backup. + # --warning=no-file-ignored set to suppress "socket ignored" messages. + tar cjf ${WHERE}/${n}.${WHEN}.tar.bz2 $relativedirname --warning=no-file-ignored + Status=$? # Preserve $? for message, since [ alters it. + if [ $Status -ne 0 ]; then + echo "Error while archiving ${WHERE}/${n}.${WHEN}.tar.bz2 - status = $Status" exit 1 fi fi done -find $WHERE -name "samba4_*bz2" -mtime +90 -exec rm {} \; >/dev/null 2>&1 +find $WHERE -name "samba4_*bz2" -mtime +$DAYS -exec rm {} \; -- cgit