#!/bin/bash # # Automated Backup System with Backup Rotation # # Copyright (c) Mesut Erdemir ( http://mesuterdemir.com ) # # 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # The above copyright notice and this permission notice shall be included in # all copies of the script. # # Add this script in cron tab in order to be executed once per day. # Example (03:30 at night every day): # 30 03 * * * /root/scripts/automated-backup.sh &> /dev/null # /root/scripts/automated-backup.sh > /var/log/automated-backup.log # # NOTE: If you redirect output to a file, you should define a log rotate job in /etc/logrotate.d # # # Author: Mesut Erdemir # Date: 2011-10-02 16:00:00 ########### Variable Definitions ########### SOURCEDIR="/home/mesut/Documents" # Path to Source Folder BACKUPDIR="/home/mesut/Backups" # Path to Save Folder DELETEOLDER=0 # Set 0 for Keep Previous Backups ########### Create Backup Directories ############# FDATE=`date +%F` # Full Date, YYYY-MM-DD # If Backup directory is not found, exit if ([ ! -d $SOURCEDIR/ ] || [ ! -d $BACKUPDIR/ ]) ; then echo Backup or Source Directory not Exist! exit 0 fi # If Backup has taken today, exit if [ ! -d $BACKUPDIR/$FDATE/ ] ; then mkdir $BACKUPDIR/$FDATE/ else echo Daily Backup is Already Done. Exiting. exit 0 fi ########### Create Tar.Gz Archive ############# tar -czvPf $BACKUPDIR/$FDATE/backup$FDATE.tar.gz $SOURCEDIR/ ########### Delete Previous Versions ########### if [ $DELETEOLDER -gt 0 ] ; then echo ----------- DELETED FOLDERS ----------- find $BACKUPDIR -maxdepth 1 -type d -mtime +$DELETEOLDER -exec echo {} \; -exec rm -rf {} \; fi # Done exit 0