samba_backup: fix bug, add command line parameter, improve error messages
authorBrian Martin <bmartin@martinconsulting.com>
Thu, 26 Sep 2013 00:01:24 +0000 (17:01 -0700)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 5 Oct 2013 11:51:34 +0000 (13:51 +0200)
Also remove .bak suffix from tdb/ldb backups for more consistent restore procedures

Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Matthieu Patou <mat@matws.net>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Sat Oct  5 13:51:34 CEST 2013 on sn-devel-104

source4/scripting/bin/samba_backup

index 8f9cc838a77ede9069a5788b72ae64ae5aed8d2f..3e22abecd9eac6c51ea03f04fb1133739cc9ca31 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+# 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  {} \;