Skip to content

Instantly share code, notes, and snippets.

@muyesh
Last active November 5, 2018 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save muyesh/583d9a9999d91845924c to your computer and use it in GitHub Desktop.
Save muyesh/583d9a9999d91845924c to your computer and use it in GitHub Desktop.
samba_backup script for Ubuntu 14.04
#!/bin/sh
#
# Copyright (C) Matthieu Patou <mat@matws.net> 2010-2011
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
TARGETDIRS="/var/lib/samba/private /var/lib/samba/sysvol /etc/samba"
WHERE=/backup/samba-dc1
if [ -n "$1" ] && [ "$1" = "-h" -o "$1" = "--usage" ]; then
echo "samba_backup '[provisiondirs]' [destinationdir]"
echo "Will backup your provision located in provisiondir to archive stored in destinationdir"
echo "Default Ubuntu provisiondirs: $TARGETDIRS"
echo "Default destinationdir: $WHERE"
exit 0
fi
if [ $# -gt 2 ]; then
echo "If you give provisiondirs parameter, Don't forget add Quotation!"
exit 0
fi
[ -n "$1" -a -d "$1" ]&&TARGETDIRS=$1
[ -n "$2" -a -d "$2" ]&&WHERE=$2
#Number of days to keep the backup
DAYS=90
WHEN=`date +%d%m%y`
if [ ! -d $WHERE ]; then
echo "Missing backup directory $WHERE"
exit 1
fi
if [ ! -d $FROMWHERE ]; then
echo "Missing or wrong provision directory $FROMWHERE"
exit 1
fi
for tdir in $TARGETDIRS;do
cd `dirname $tdir`
d=`basename $tdir`
relativedirname=`find . -type d -name "$d" -prune`
if [ "$relativedirname" = "" ]; then
echo 'break!'
break
fi
n=`echo $tdir|sed 's|\/|_|g'|sed 's/^_//'`
if [ "$d" = "private" ]; then
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"
exit 1
fi
done
tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --exclude=*.ldb --warning=no-file-ignored --transform 's/.ldb.bak$/.ldb/' >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2"
exit 1
fi
find $relativedirname -name "*.ldb.bak" -exec rm {} \;
else
tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2"
exit 1
fi
fi
done
find $WHERE -name "samba4_*bz2" -mtime +$DAYS -exec rm {} \; >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment