s4-dsdb: Return error codes as windows does for Tombstone reanimation
[Samba.git] / source4 / scripting / bin / samba_backup
blob3e22abecd9eac6c51ea03f04fb1133739cc9ca31
1 #!/bin/sh
3 # Copyright (C) Matthieu Patou <mat@matws.net> 2010-2011
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Revised 2013-09-25, Brian Martin, as follows:
19 # - Allow retention period ("DAYS") to be specified as a parameter.
20 # - Allow individual positional parameters to be left at the default
21 # by specifying "-"
22 # - Use IS0 8601 standard dates (yyyy-mm-dd instead of mmddyyyy).
23 # - Display tar exit codes when reporting errors.
24 # - Don't send error messages to /dev/null, so we know what failed.
25 # - Suppress useless tar "socket ignored" message.
26 # - Fix retention period bug when deleting old backups ($DAYS variable
27 # could be set, but was ignored).
31 FROMWHERE=/usr/local/samba
32 WHERE=/usr/local/backups
33 DAYS=90 # Set default retention period.
34 if [ -n "$1" ] && [ "$1" = "-h" -o "$1" = "--usage" ]; then
35 echo "samba_backup [provisiondir] [destinationdir] [retpd]"
36 echo "Will backup your provision located in provisiondir to archive stored"
37 echo "in destinationdir for retpd days. Use - to leave an option unchanged."
38 echo "Default provisiondir: $FROMWHERE"
39 echo "Default destinationdir: $WHERE"
40 echo "Default destinationdir: $DAYS"
41 exit 0
44 [ -n "$1" -a "$1" != "-" ]&&FROMWHERE=$1 # Use parm or default if "-". Validate later.
45 [ -n "$2" -a "$2" != "-" ]&&WHERE=$2 # Use parm or default if "-". Validate later.
46 [ -n "$3" -a "$3" -eq "$3" 2> /dev/null ]&&DAYS=$3 # Use parm or default if non-numeric (incl "-").
48 DIRS="private etc sysvol"
49 #Number of days to keep the backup
50 WHEN=`date +%Y-%m-%d` # ISO 8601 standard date.
52 if [ ! -d $WHERE ]; then
53 echo "Missing backup directory $WHERE"
54 exit 1
57 if [ ! -d $FROMWHERE ]; then
58 echo "Missing or wrong provision directory $FROMWHERE"
59 exit 1
62 cd $FROMWHERE
63 for d in $DIRS;do
64 relativedirname=`find . -type d -name "$d" -prune`
65 n=`echo $d | sed 's/\//_/g'`
66 if [ "$d" = "private" ]; then
67 find $relativedirname -name "*.ldb.bak" -exec rm {} \;
68 for ldb in `find $relativedirname -name "*.ldb"`; do
69 tdbbackup $ldb
70 Status=$? # Preserve $? for message, since [ alters it.
71 if [ $Status -ne 0 ]; then
72 echo "Error while backing up $ldb - status $Status"
73 exit 1
75 done
76 # Run the backup.
77 # --warning=no-file-ignored set to suppress "socket ignored" messages.
78 tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --exclude=\*.ldb --warning=no-file-ignored --transform 's/.ldb.bak$/.ldb/'
79 Status=$? # Preserve $? for message, since [ alters it.
80 if [ $Status -ne 0 -a $Status -ne 1 ]; then # Ignore 1 - private dir is always changing.
81 echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 - status = $Status"
82 exit 1
84 find $relativedirname -name "*.ldb.bak" -exec rm {} \;
85 else
86 # Run the backup.
87 # --warning=no-file-ignored set to suppress "socket ignored" messages.
88 tar cjf ${WHERE}/${n}.${WHEN}.tar.bz2 $relativedirname --warning=no-file-ignored
89 Status=$? # Preserve $? for message, since [ alters it.
90 if [ $Status -ne 0 ]; then
91 echo "Error while archiving ${WHERE}/${n}.${WHEN}.tar.bz2 - status = $Status"
92 exit 1
95 done
97 find $WHERE -name "samba4_*bz2" -mtime +$DAYS -exec rm {} \;