#!/bin/bash BACKUP_LOC="/mnt/backup" EXCLUDE_FILE="/root/scripts/backup/exclude" month="`date '+%B'`" nextmonth="` date --date '1 month' '+%B' `" lastmonth="` date --date '1 month ago' '+%B' `" lastyear="` date --date '1 year ago' '+%Y' `" today="`date '+%d'`" yesterday="`date --date '1 day ago' '+%d'`" if [ -n "$1" ] then BACKUP_LOC="$1" fi #If next month exists, then its from a year ago, so append the year onto next month. if [ -e "$BACKUP_LOC/$nextmonth" ] then mv $BACKUP_LOC/$nextmonth $BACKUP_LOC/$nextmonth.$lastyear fi #If last month of last year exists, then delete it. if [ -e "$BACKUP_LOC/$lastmonth.$lastyear" ] then rm -Rf $BACKUP_LOC/$lastmonth.$lastyear fi #Create folder structure if it doesn't exist if [ ! -e "$BACKUP_LOC/$month" ] then mkdir $BACKUP_LOC/$month fi if [ ! -e "$BACKUP_LOC/$month/$today" ] then mkdir $BACKUP_LOC/$month/$today fi #Finally the backup cp -avlf $BACKUP_LOC/$month/$yesterday/. $BACKUP_LOC/$month/$today /usr/bin/rsync -av --ignore-errors --delete --exclude-from=$EXCLUDE_FILE /home $BACKUP_LOC/$month/$today/ /usr/bin/rsync -av --ignore-errors --delete --exclude-from=$EXCLUDE_FILE /data $BACKUP_LOC/$month/$today/ /usr/bin/rsync -av --ignore-errors --delete --exclude-from=$EXCLUDE_FILE /etc $BACKUP_LOC/$month/$today/ /usr/bin/rsync -av --ignore-errors --delete --exclude-from=$EXCLUDE_FILE /var/www $BACKUP_LOC/$month/$today/