#!/bin/bash
#
# Copyright (c) 2009 Lee Marzke
#
# 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, version 2 of the License.
#
# 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/>.
#
# ---------------------------------------------------------------------
# DPBACKUP
#
# Version 0.5.2 - 14 July 2009
#
# Encrypted Backups with Duplicity
#
#
# Based on Jon Watson's Script at http://duplicity.nongnu.org/contrib.html and
# Damon Timm's Script at same location.
#
# History:
# Original: Jon Watson's script ( no version )
#
# 0.5 2009/4/9 Lee Marzke (lee@marzke.net)
# Added parameters where useful. Added ability to pass ssh password from script.
# Fixed parsing of repDate variable under dash for Ubuntu machines
# Combined Full and Incremental into one script, added functions
# Added stats and restore options. Added GPL. Tested with Duplicity 0.5.13
# 0.5.1 2009/4/10 Lee Marzke (lee@marzke.net)
# Added default options to restore function, to allow restoring with no encryption.
# Split Full and Incr logfiles. Fixed processing of directories with spaces.
# Restore of a folder now places that folder in restore directory.
# 0.5.2 2009/7/14 Lee Marzke (lee@marzke.net>
# Reduced log level, added more excluded directorires.
#
#
# TODO:
# 1. Parameterize machine name, backup mode
# Export the PASSPHRASE variable for gpg encryption, uncomment this for GPG use
#export PASSPHRASE=""
# Export FTP_PASSWORD use by SSH for login to remote server.
export FTP_PASSWORD=""
#locations, no trailing slashes
DP_SOURCE=/home/user
DP_DESTINATION=scp://root@server//var/backup/hostname
DP_RESTORE_DEST=/home/user/restore
DP_EMAIL=user@domain
DP_OPTIONS="--ssh-askpass --no-encryption"
#Select One of thise clean options
#DP_CLEAN_OPTIONS="remove-older-than 14D -v4 --force"
DP_CLEAN_OPTIONS="remove-all-but-n-full 3 -v4 --force"
#Backup Options
DP_BACKUP_OPTIONS=-v5
#DP_INCLUDE='--include /home/user/Desktop'
DP_INCLUDE=
# Exclude IMAP mail stored on remote servers, thumbnails, etc.
DP_EXCLUDE="--exclude ${DP_SOURCE}/restore --exclude ${DP_SOURCE}/logs --exclude ${DP_SOURCE}/.mozilla-thunderbird/*/ImapMail --exclude ${DP_SOURCE}/restore --exclude ${DP_SOURCE}/.thumbnails --exclude ${DP_SOURCE}/.kde/share/apps/kmail/imap/ --exclude ${DP_SOURCE}/.evolution/mail/imap/ --exclude ${DP_SOURCE}/.mozilla/firefox/*/Cache/ "
#DP_EXCLUDE=
# Get the date
repDate=`date +%Y%m%d`
#Logfile paths
DP_LOGDIR=/home/snk/logs
DP_FLOGFILE="$DP_LOGDIR/Backup_Full_${repDate}_localmachine_admin.log"
DP_ILOGFILE="$DP_LOGDIR/Backup_Incr_${repDate}_localmachine_admin.log"
DP_STATSFILE="$DP_LOGDIR/Backup_Stats_localmachine_admin.log"
##################################################################################
# No changes should be needed below here
##################################################################################
# Set Mode from Command line
DP_MODE=$1
#DP_MODE=
#DP_MODE=full
DP_RESTORE_PATH_REQUEST=$2
DP_CMD=
function dobackup
{
echo "=============================================================================="
echo .
echo "Starting ${DP_MODE} Backup"
DP_CMD="/usr/bin/duplicity \
$DP_MODE \
$DP_BACKUP_OPTIONS \
$DP_OPTIONS \
$DP_INCLUDE \
$DP_EXCLUDE \
$DP_SOURCE/ \
$DP_DESTINATION"
echo "Executing: ${DP_CMD}"
eval $DP_CMD
}
function docleanup
{
echo "=============================================================================="
echo .
echo "Cleanup"
DP_CMD="/usr/bin/duplicity \
$DP_CLEAN_OPTIONS \
$DP_OPTIONS \
$DP_DESTINATION"
echo "Executing: ${DP_CMD}"
eval $DP_CMD
}
function dogetstats
{
echo "=============================================================================="
echo .
echo "Getting Stats"
DP_CMD="/usr/bin/duplicity \
collection-status \
--ssh-askpass \
-v5 \
$DP_DESTINATION"
echo "Executing: ${DP_CMD}"
eval $DP_CMD
}
function dorestore
{
echo "=============================================================================="
echo .
echo "Starting Restore"
eval mkdir -p ${DP_RESTORE_DEST}
DP_CMD="/usr/bin/duplicity restore ${DP_OPTIONS} ${DP_RESTORE_OPTIONS} ${DP_DESTINATION}/ ${DP_RESTORE_DEST}/"
echo "Executing: ${DP_CMD}"
eval $DP_CMD
}
if [ "$1" = "full" ]; then
dobackup full | tee -a ${DP_FLOGFILE}
docleanup | tee -a ${DP_FLOGFILE}
dogetstats | tee ${DP_STATSFILE}
elif [ "$1" = "stats" ]; then
dogetstats
exit
elif [ "$1" = "verify" ]; then
echo "Not implemented"
exit
elif [ "$1" = "--help" ]; then
echo "dpbackup [ full | stats | restore PATH ]"
echo .
echo "Where: full - do full backup"
echo " <blank> - do incremental backup"
echo " stats - print statisics on backup chains"
echo " restore - restore PATH to ${DP_RESTORE_DEST}"
echo " where PATH is any directory inside ${DP_SOURCE}/"
echo " example restore Documents"
exit
elif [ "$1" = "restore" ]; then
if [ $DP_RESTORE_PATH_REQUEST = "" ]; then
DP_RESTORE_OPTIONS=
else
DP_RESTORE_OPTIONS="--file-to-restore '${DP_RESTORE_PATH_REQUEST}'"
DP_RESTORE_DEST="'${DP_RESTORE_DEST}/${DP_RESTORE_PATH_REQUEST}'"
fi
if [ "$3" != "yes" ]; then
echo ">> You will restore ${DP_SOURCE}/${DP_RESTORE_PATH_REQUEST} to ${DP_RESTORE_DEST}"
echo ">> You can override this question by executing 'verify yes' next time"
echo "Are you sure you want to do that ('yes' to continue)?"
read ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "You said << ${ANSWER} >> so I am exiting now."
exit 1
fi
echo "Restoring now ..."
fi
dorestore
echo "Done"
exit
elif [ "$1" = "test" ]; then
echo "This was a non-duplicity scripting test: check logfile for file sizes."
else
#Incremental Backup
dobackup | tee -a ${DP_ILOGFILE}
docleanup | tee -a ${DP_ILOGFILE}
dogetstats | tee ${DP_STATSFILE}
fi
# Get the disk space
# NOT working until ssh password passing fixed.
#echo "Availble Disk Space on Server" >> $DP_LOGDIR/Backup_Log_$repDate_localmachine_admin.log
#echo >> $DP_LOGDIR//Backup_Log_${repDate}_localmachine_admin.log /usr/bin/ssh root@10.24.1.8 df -h >> $DP_LOGDIR/Full_Backup_Log_${repDate}_localmachine_admin.log
# Mail me the results
if [ "${DP_MODE}" = "full" ]; then
mail -s "[backup] ${DP_MODE}" $DP_EMAIL < ${DP_STATSFILE}
else
mail -s "[backup] incr" $DP_EMAIL < ${DP_STATSFILE}
fi
#Set permissions on logs so non-root users can run
chgrp backup ${DP_LOGDIR}/*.log
chmod 0664 ${DP_LOGDIR}/*.log
unset PASSPHRASE
unset FTP_PASSWORD
unset DP_CMD