Wednesday, September 4, 2013

Backup script for full and incremental backup

Backup script for full and incremental backup


#!/bin/bash
#
# Backup script - Full and Incremental
# Version 0.8
# Created by LinuxStories

#
# For debug remove below hash
#set -x
#
#
DATA="/home /root/"
CONFIG="/etc"
LIST="/tmp/backlist_$$.txt"
DST="/backup_export"
#


#
# here I am setting a time stamp variable which I like to use for logging
TIMESTAMP=`date +%Y%m%d.%H%M`

# let's create a variable for the backup file name file
FNAME="Backup"

# let's create a variable for the log file, let's also name the log file with the filename and timestamp it
LOG="$FNAME-$TIMESTAMP.log"

set $(date)
if test "$1" = "Sun" ; then
        # weekly a full backup of all data and config. settings:
        #
        # start the backup, create a log file which will record any messages run by this script
        echo "*** Starting full backup -- $TIMESTAMP $DATA" >> ${LOG}
        tar cvfz "$DST/data/data_full_$6-$2-$3.tar.gz" $DATA >> ${LOG}
        rm -f $DST/data/data_diff* >> ${LOG}
        echo "*** Delete old full backup" >> ${LOG}
        find $DST/data/ -mtime +6 -exec rm {} \;
        echo "*** Ending backup of $DATA directories -- $TIMESTAMP" >> ${LOG}
        #
        # start the backup, create a log file which will record any messages run by this script
        echo "*** Starting full backup -- $TIMESTAMP $CONFIG" >> ${LOG}
        tar cvfz "$DST/config/config_full_$6-$2-$3.tar.gz" $CONFIG >> ${LOG}
        rm -f $DST/config/config_diff* >> ${LOG}
        echo "*** Delete old full backup" >> ${LOG}
        find $DST/config/ -mtime +6 -exec rm {} \;
        echo "*** Ending backup of $CONFIG directories -- $TIMESTAMP" >> ${LOG}
else
        # incremental backup:
        #
        echo "*** Starting incremental backup -- $TIMESTAMP $DATA" >> ${LOG}
        find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
        # start the backup, create a log file which will record any messages run by this script
        tar cvfzT  "$DST/data_diff_$6-$2-$3.tar.gz" "$LIST" >> ${LOG}
        rm -f "$LIST" >> ${LOG}
        echo "*** Ending backup of $DATA directories -- $TIMESTAMP" >> ${LOG}
        #
        echo "*** Starting incremental backup -- $TIMESTAMP $CONFIG" >> ${LOG}
        find $CONFIG -depth -type f  \( -ctime -1 -o -mtime -1 \) -print > $LIST
        # start the backup, create a log file which will record any messages run by this script
        tar cvfzT "$DST/config/config_diff_$6-$2-$3.tar.gz" "$LIST" >> ${LOG}
        rm -f "$LIST" >> ${LOG}
        echo "*** Ending backup of $CONFIG directories -- $TIMESTAMP" >> ${LOG}
fi

# End the backup, append to log file created by this script
echo "*** Ending total backup -- $TIMESTAMP ***" >> ${LOG}

2 comments :

  1. #!/bin/sh
    # full and incremental backup script

    #Mount the device where your backup get stored
    #mount /dev/ /home/backups
    #COUNT=`mount|grep "^10.10.10.30"|wc -l`
    #echo $COUNT
    #if [ $COUNT -lt 1 ]
    #then
    #mount /mnt/backupserv
    #fi


    #Change the 5 variables below to fit your computer/backup

    COMPUTER=88webserver # name of this computer
    DIRECTORIES="/var/www/html" # directoris to backup
    BACKUPDIR=/Backup_disk/Autobackup/Code # where to store the backup
    TIMEDIR=/tmp/destination/backup/serverbackup/ # where to store time of full backup
    TAR=/bin/tar # name and locaction of tar

    #You should not have to change anything below here

    #PATH=/usr/local/bin:/usr/bin:/bin
    PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
    DOW=`date +%a` # Day of the week e.g. Mon
    DOM=`date +%d` # Date of the Month e.g. 27
    DM=`date +%d%b` # Date and Month e.g. 27Sep

    # On the 1st of the month a permanet full backup is made
    # Every Sunday a full backup is made - overwriting last Sundays backup
    # The rest of the time an incremental backup is made. Each incremental
    # backup overwrites last weeks incremental backup of the same name.
    #
    # if NEWER = "", then tar backs up all files in the directories
    # otherwise it backs up files newer than the NEWER date. NEWER
    # gets it date from the file written every Sunday.


    # Monthly full backup
    if [ $DOM = "01" ]; then
    NEWER=""
    $TAR $NEWER -zcvf $BACKUPDIR/$COMPUTER-$DM.tar.gz $DIRECTORIES
    fi

    # Weekly full backup
    if [ $DOW = "Thu" ]; then
    NEWER=""
    NOW=`date +%d-%b`

    # Update full backup date
    echo $NOW > $TIMEDIR/$COMPUTER-full-date
    $TAR $NEWER -zcvf $BACKUPDIR/$COMPUTER-$DOW.tar.gz $DIRECTORIES

    # Make incremental backup - overwrite last weeks
    else

    # Get date of last full backup
    NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
    $TAR $NEWER -zcvf $BACKUPDIR/$COMPUTER-$DOW.tar.gz $DIRECTORIES
    fi

    #UnMount the device where your backup get stored
    #umount /home/backups


    I was runing this incremental backup script on my server but could not working properly, it script runing but data size big during to holidays, please help me how get only changes files.

    ReplyDelete
  2. Hello,

    you can used the bellow commands to find the changes for incremental backup

    LIST="/tmp/backlist_$$.txt"
    DATA"/home /root/" # Ex. path foer backup

    echo "*** Starting incremental backup -- $TIMESTAMP $DATA" >> ${LOG}
    find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
    # start the backup, create a log file which will record any messages run by this script
    tar cvfzT "$DST/data_diff_$6-$2-$3.tar.gz" "$LIST" >> ${LOG}
    rm -f "$LIST" >> ${LOG}
    echo "*** Ending backup of $DATA directories -- $TIMESTAMP" >> ${LOG}

    ReplyDelete