#!/bin/sh
#
# Simple, configurable rsync backup script
#
# You define the command (rsync + path), the arguments, and what
#  to back up (and to where).  Supports multiple locations, via LOCX
#  variables.
# 
# If you call it with a '-q' flag, it will put the log in 
#  /var/log/backup.log; otherwise, it will write to stdout.
#
#
# Written by Wyatt Olson 
#  <wyatt.olson@gmail.com>
#  https://drummer.homeunix.org
#
# Changelog
#	v 2.0	2006.01.07	Split up command to allow for multiple locations
#	v 1.0	2004.??.??	Original release
#
######################################

# Needed to prevent file globbing of *s, etc in the LOCX section.
set -f

# Clear last log if we are in quiet mode.
LOG=/var/log/backup.log
if [ "$1" = "-q" ]
then
	date > $LOG
fi

# This should be the full path to rsync.
RSYNC="/usr/bin/rsync" 

# Arguments for all locations.  This should probably include at 
#  least '-ae ssh'.
GEN_ARGS="-avze ssh --size-only --timeout 1200 \
 --exclude .DS_Store \
 --exclude .Trash"

# Locations, including location specific arguments.  Suggested naming 
#  convention of LOC1, LOC2, ... LOCX.

# This location will backup the entire home folder, but will ignore folders such as Music and Movies.
LOC1="-R --delete-excluded --delete-after \
 --exclude Documents/Downloads/ \
 --exclude Documents/Static/ \
 --exclude Desktop/ \
 --exclude Music/ \
 --exclude Movies/ \
 /home backup-user@server.example.com:/home/backup-user/backup"

# This next location will pull a folder from the server, and put it on the client in the specified folder.
LOC2="--delete-excluded --delete-after \
 --exclude logs \
 backup-user@server.example.com:/var/www /home/user/documents/html"

# Which locations to back up.  Include them in quotes, e.g. "${LOCX}" 
#  (otherwise the shell splits each argument into a separate LOC variable). 
for LOC in "${LOC1}" "${LOC2}" 
do
	COMMAND="${RSYNC} ${GEN_ARGS} ${LOC}"

	if [ "$1" = "-q" ]
	then
		echo "\nStarting ${COMMAND}" >> ${LOG}
		date >> ${LOG}
		${COMMAND} 1>>${LOG} 2>>${LOG}
		echo "Finished ${COMMAND}" >> ${LOG}
		date >> $LOG
	else
		echo "\nStarting ${COMMAND}"
		${COMMAND}
		echo "Finished ${COMMAND}"
	fi
done 

