Skip to content

Instantly share code, notes, and snippets.

@pkutzner
Created February 6, 2018 22:30
Show Gist options
  • Save pkutzner/c53c5b31a3e34135ff215e425fdb9362 to your computer and use it in GitHub Desktop.
Save pkutzner/c53c5b31a3e34135ff215e425fdb9362 to your computer and use it in GitHub Desktop.
Samba4 Backup Script for Ubuntu package installations.
#!/bin/bash
#
# Copyright (C) Matthieu Patou <[email protected]> 2010-2011
# Copyright (C) Preston Kutzner <[email protected]> 2018
#
# 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
# MERCHANTIBILITY 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 <https://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 ISO 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).
#
# Revised 2018-02-06, Preston Kutzner, as follows:
# - Modify to backup Ubuntu packaged version of Samba4.
# - Convert DIRS variable from string to array.
# - Remove ability to specify FROMWHERE base directory, since
# Ubuntu spreads some files out. These paths are now
# specified in the DIRS array and checked for sanity within
# the main 'for' loop.
# - Add debug (dry-run) option.
# - Fix error where 'else' section of main 'if' didn't prepend
# samba4_ to the beginning of the tarball filename.
# - Now requires bash, due to double-bracket test for 'DEBUG'
# and use of bash parameter expansions.
DEBUG="false"
WHERE=/usr/local/backups
DAYS=90
if [[ "$DEBUG" =~ ^[Tt]rue ]]; then
echo "Debug enabled"
ECHO="/bin/echo"
fi
if [ -n "$1" ] && [ "$1" = "-h" -o "$1" = "--usage" ]; then
echo "samba_backup [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 destinationdir: $WHERE"
echo "Default retpd: $DAYS"
exit 0
fi
[ -n "$1" -a "$1" != "-" ]&&WHERE=$1 # Use parm or default if "-". Validate later.
[ -n "$2" -a "$2" -eq "$2" 2> /dev/null ]&&DAYS=$2 # Use parm or default if non-numeric (incl "-").
# Number of days to keep the backup
WHEN=`date +%Y-%m-%d` # ISO 8601 standard date.
DIRS=('/var/lib/samba/private' '/var/lib/samba/sysvol' '/etc/samba')
if [ ! -d $WHERE ]; then
echo "Missing backup directory $WHERE"
exit 1
fi
for d in ${DIRS[@]}; do
# Sanity check.
if [ ! -d "$d" ]; then
echo "Missing or wrong provision directory $d"
exit 1
fi
### Bash parameter expansion explanations ###
#
# ${d%/*} Returns path up to, but not including last leaf
# e.g. /var/lib/samba/private would be returned as
# /var/lib/samba
# ${d##*/} Returns last leaf only.
# e.g. /var/lib/samba/private would be returned as
# private
# ${d#/*} Returns path minus leading '/'
# e.g. /var/lib/samba/private would be returned as
# var/lib/samba/private
cd "${d%/*}"
relativedirname=`find . -type d -name "${d##*/}" -prune`
n=`echo ${d#/*} | sed 's/\//_/g'`
if [ "${d##*/}" = "private" ]; then
find $relativedirname -name "*.ldb.bak" -exec $ECHO rm {} \;
for ldb in `find $relativedirname -name "*.ldb"`; do
$ECHO tdbbackup $ldb
Status=$?
if [ $Status -ne 0 ]; then
echo "Error while backing up $ldb - status $Status"
exit 1
fi
done
# Run the backup
# --warning=no-file-ignored set to suppress "socket ignored" messages.
$ECHO tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --exclude=\*.ldb --warning=no-file-ignored --transform 's/.ldb.bak$/.ldb/'
Status=$?
if [ $Status -ne 0 -a $Status -ne 1 ]; then
echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 - status = $Status"
exit 1
fi
find $relativedirname -name "*.ldb.bak" -exec $ECHO rm {} \;
else
# Run the backup.
# --warning=no-file-ignored set to suppress "socket ignored" messages.
$ECHO tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 $relativedirname --warning=no-file-ignored
Status=$?
if [ $Status -ne 0 ]; then
echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 - status = $Status"
exit 1
fi
fi
done
$ECHO find ${WHERE} -name "samba4_*bz2" -mtime +$DAYS -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment