#!/bin/bash

# ### Keeps a mirror of a local directory on the remote and full (hardlinked/diff) backups in a backup directory on the remote ###
# this is somewhat questionable and not extensively tested.
# source and destination directories are the same

# date (yy-mm-dd)
DAY=`date +%y-%m-%d`
# # date (yyyy-mm-dd)
# DAY=`date +%Y-%m-%d`
# # day of the week (7 day cycle) uncomment next 5 lines (and comment day above)
# DAY=`date +%A`
# # when using a day of the week, this serves to clear the 7 day old backup (to be replaced w/ todays backup)
# [ -d /tmp/emptydir ] || mkdir /tmp/emptydir
# rsync --delete -av -e ssh /tmp/emptydir/ $SERVER:$ROOTDIR/$BACKUP/$DIRI/$DAY
# rmdir /tmp/emptydir

# _destination_ or source server
# SERVER=foo@host.domain.tld
SERVER=192.168.1.4

# root directory on _source_ and destination
ROOTDIR=/var/www/html
# if source and destination root directories are different
# replace $ROOTDIR w/ $DESTROOT e.g. $SERVER:$DESTROOT...
# and --link-dest=$DESTROOT...
# DESTROOT=/var/www/html

# backup directory as sub directory of rootdir
# use $SERVER:$BACKUP and full path if its not
BACKUP=backup

# directorys to be backed up (subdirectories of rootdir)
# these directories need to exist on the remote 
# e.g. /var/www/html/backup/DWS
DIRI=DWS
DIRII=dalrun
# DIRIII=
# DIRIIII=

# While multiple directories (above) is an option, excluding is also an option
# Excludes can be in a file --exclude-from=<path to file> or one exclude per --exclude=<foo>
# source directories to exclude - <dir>/ = any directory matching <dir>, /<dir>/ = root directory match
# source files to exclude /<file> = root file match, <dir>/<file> = any matching <file> in <dir>?

# add -n to test, -z for compressed transfer, more v's for increased verbosity

# push - send directories on this machine to backup server
# backup DIRI to $DAY using yesterdays mirror for diff/hard links !!has to be on the same partition!! (--backup doesn't)
rsync -av -e ssh --link-dest=$ROOTDIR/$DIRI $ROOTDIR/$DIRI/ $SERVER:$ROOTDIR/$BACKUP/$DIRI/$DAY
# update mirror
rsync -av -e ssh --delete $ROOTDIR/$DIRI/ $SERVER:$ROOTDIR/$DIRI
# DIRII
rsync -av -e ssh --link-dest=$ROOTDIR/$DIRII $ROOTDIR/$DIRII/ $SERVER:$ROOTDIR/$BACKUP/$DIRII/$DAY
rsync -av -e ssh --delete $ROOTDIR/$DIRII/ $SERVER:$ROOTDIR/$DIRII
# # DIRIII
# rsync -av -e ssh --link-dest=$ROOTDIR/$DIRIII $ROOTDIR/$DIRIII/ $SERVER:$ROOTDIR/$BACKUP/$DIRIII/$DAY
# rsync -av -e ssh --delete $ROOTDIR/$DIRIII/ $SERVER:$ROOTDIR/$DIRIII
# # DIRIIII
# rsync -av -e ssh --link-dest=$ROOTDIR/$DIRIIII $MIRROR/$DIRIIII/ $TOSERVER:$ROOTDIR/$BACKUP/$DIRIIII/$DAY
# rsync -av -e ssh --delete $ROOTDIR/$DIRIIII/ $TOSERVER:$ROOTDIR/$DIRIIII

# Restore
# rsync -avv -e ssh --delete  $SERVER:$ROOTDIR/$BACKUP/[DIR]/[nn-nn-nn]or[day]/ /var/www/html/[DIR] 
# rsync -avv -e ssh --delete  192.168.1.4:/var/www/html/backup/DWS/[nn-nn-nn]or[day]/ /var/www/html/DWS 

# # Push example with $DESTROOT, order is always source > destination (--link-dest(ination))
# rsync -av -e ssh --link-dest=$DESTROOT/$DIRI $ROOTDIR/$DIRI/ $SERVER:$DESTROOT/$BACKUP/$DIRI/$DAY
# rsync -av -e ssh --delete $ROOTDIR/$DIRI/ $SERVER:$DESTROOT/$DIRI
# 
# # Pull example - pull directories from remote to to this/backup server
# rsync -av -e ssh --link-dest=$ROOTDIR/$DIRI $SERVER:$ROOTDIR/$DIRI/ $ROOTDIR/$BACKUP/$DIRI/$DAY
# rsync -av -e ssh --delete $SERVER:$ROOTDIR/$DIRI/ $ROOTDIR/$DIRI
# 
# # Pull example with $DESTROOT
# rsync -av -e ssh --link-dest=$DESTROOT/$DIRI $SERVER:$ROOTDIR/$DIRI/ $DESTROOT/$BACKUP/$DIRI/$DAY
# rsync -av -e ssh --delete $SERVER:$ROOTDIR/$DIRI/ $DESTROOT/$DIRI
