The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My version of "keep daily backups"

Discussion in 'cPanel Developers' started by WhiteDog, May 27, 2012.

  1. WhiteDog

    WhiteDog Well-Known Member

    Joined:
    Feb 19, 2008
    Messages:
    118
    Likes Received:
    0
    Trophy Points:
    16
    As a host, I've always wanted to store multiple copies of my users backups. I also feel that the current daily / weekly : monthly system is outdated as all my users have databases which change so often that, restoring a monthly backup, would be setting them back to the stone age :)

    There are a few scripts floating around which keep multiple versions of backups / copy the daily to the "day of the week". I was not 100% satisfied with those solutions, so I came up with my own solution. Some pointers on where my script differs from the others. Read below carefully!

    • This script runs BEFORE cpbackup, so place it in /scripts/precpbackup
    • No backups are copied, they are renamed. This saves on disk space and IO.
    • You have to configure cPanel to ONLY tae a DAILY backup. The script will handle the weekly. As no weeklies are created, again saving on IO.
    • You can define the number of daily / weekly copies to keep. Monthlies are not created as i don't need that.
    • Because daily and weekly folders still exist, they can sttill be downloaded by the end-user from within cPanel.

    How the script works:
    • The script runs BEFORE the backups are taken.
    • Your current "daily" is renamed to "daily_YYYYMMDD_HHMM" or "weekly_YYYYMMDD_HHMM". The date is the date that the folder was created (so generally yesteday date).
    • weeklies / dailies are deleted if their number exceeds the value set (DAYS_TO_KEEP / WEEKS_TO_KEEP)

    And finally, the script:
    Code:
    #####################################
    # How2 Solutions - http://how2.be
    # Keep Daily Backup
    # version 2.2
    #####################################
    # Configuration
    DAYS_TO_KEEP=3
    WEEKS_TO_KEEP=2
    #####################################
    
    CPBACKUP_DEST_PATH="/backup/cpbackup"
    
    # check if the cpbackup directory exists
    if [ ! -d "$CPBACKUP_DEST_PATH" ]
    then
        echo "FATAL ERROR - CPBACKUP_DEST_PATH not found"
    	exit 1
    fi
    
    # process weekly
    if [ -d "$CPBACKUP_DEST_PATH/weekly" ]
    then
        # check if weekly older then 6.5 days
        if [ $(( (`date +%s` - `stat -L --format %Y "$CPBACKUP_DEST_PATH/weekly/"`) > (60*60*156) )) -eq 1 ];
        then
            # weekly is archived
            WEEKLY_DATE=$(stat "$CPBACKUP_DEST_PATH/weekly" --format %y|cut -f1 -d'.')
            WEEKLY_DATE=${WEEKLY_DATE//[-:]/}
            WEEKLY_TARGET=$CPBACKUP_DEST_PATH/weekly_${WEEKLY_DATE/ /_}
            mv "$CPBACKUP_DEST_PATH/weekly" "$WEEKLY_TARGET"
        else
            echo "weekly not older then 6.5 days";
        fi
    else
        # no weekly, nothing to rename
        echo "$CPBACKUP_DEST_PATH/weekly not found"
    fi
    
    # process daily
    if [ -d "$CPBACKUP_DEST_PATH/daily" ]
    then
        if [ ! -d "$CPBACKUP_DEST_PATH/weekly" ]
        then
            # daily becmes new weekly
            mv "$CPBACKUP_DEST_PATH/daily" "$CPBACKUP_DEST_PATH/weekly"
        else
            # daily is archived
            DAILY_DATE=$(stat "$CPBACKUP_DEST_PATH/daily" --format %y|cut -f1 -d'.')
            DAILY_DATE=${DAILY_DATE//[-:]/}
            DAILY_TARGET=$CPBACKUP_DEST_PATH/daily_${DAILY_DATE/ /_}
            mv "$CPBACKUP_DEST_PATH/daily" "$DAILY_TARGET"
        fi
    else
        # no daily, nothing to rename
        echo "$CPBACKUP_DEST_PATH/daily not found"
    fi
    
    # find and delete old folders
    rm -Rf `ls $CPBACKUP_DEST_PATH/daily_* -td | tail -n +$DAYS_TO_KEEP`
    rm -Rf `ls $CPBACKUP_DEST_PATH/weekly_* -td | tail -n +$WEEKS_TO_KEEP`
    
    exit 1
    Feel free to use / change / comment. If you like it, please leave a message :)
    The only part I'm not yet 100% happy with is that sometimes your weekly is more newer then your daily. Not confusing to me but might be for the end-user downloading from within cPanel. I will probably change this someday into a system I like better.
     
  2. WhiteDog

    WhiteDog Well-Known Member

    Joined:
    Feb 19, 2008
    Messages:
    118
    Likes Received:
    0
    Trophy Points:
    16
    As I can't seem to edit the starting post, here is an improved versin as cpbackup sometimes fzils to start if there is no "daily" present:

    Code:
    #####################################
    # How2 Solutions - http://how2.be
    # Keep Daily Backup
    # version 2.3
    #####################################
    # Configuration
    DAYS_TO_KEEP=4
    WEEKS_TO_KEEP=2
    #####################################
    
    CPBACKUP_DEST_PATH="/backup/cpbackup"
    
    # check if the cpbackup directory exists
    if [ ! -d "$CPBACKUP_DEST_PATH" ]
    then
        echo "FATAL ERROR - CPBACKUP_DEST_PATH not found"
    	exit 1
    fi
    
    # process weekly
    if [ -d "$CPBACKUP_DEST_PATH/weekly" ]
    then
        # check if weekly older then 6.5 days
        if [ $(( (`date +%s` - `stat -L --format %Y "$CPBACKUP_DEST_PATH/weekly/"`) > (60*60*156) )) -eq 1 ];
        then
            # weekly is archived
            WEEKLY_DATE=$(stat "$CPBACKUP_DEST_PATH/weekly" --format %y|cut -f1 -d'.')
            WEEKLY_DATE=${WEEKLY_DATE//[-:]/}
            WEEKLY_TARGET=$CPBACKUP_DEST_PATH/weekly_${WEEKLY_DATE/ /_}
            mv "$CPBACKUP_DEST_PATH/weekly" "$WEEKLY_TARGET"
        else
            echo "weekly not older then 6.5 days";
        fi
    else
        # no weekly, nothing to rename
        echo "$CPBACKUP_DEST_PATH/weekly not found"
    fi
    
    # process daily
    if [ -d "$CPBACKUP_DEST_PATH/daily" ]
    then
        if [ ! -d "$CPBACKUP_DEST_PATH/weekly" ]
        then
            # daily becomes new weekly
            mv "$CPBACKUP_DEST_PATH/daily" "$CPBACKUP_DEST_PATH/weekly"
    
            # keep cPanel in the loop
            mkdir "$CPBACKUP_DEST_PATH/daily"
            cp "$CPBACKUP_DEST_PATH/weekly/cpbackupstatus.cfg" "$CPBACKUP_DEST_PATH/daily/cpbackupstatus.cfg"
        else
            # daily is archived
            DAILY_DATE=$(stat "$CPBACKUP_DEST_PATH/daily" --format %y|cut -f1 -d'.')
            DAILY_DATE=${DAILY_DATE//[-:]/}
            DAILY_TARGET=$CPBACKUP_DEST_PATH/daily_${DAILY_DATE/ /_}
            mv "$CPBACKUP_DEST_PATH/daily" "$DAILY_TARGET"
    
            # keep cPanel in the loop
            mkdir "$CPBACKUP_DEST_PATH/daily"
            cp "$DAILY_TARGET/cpbackupstatus.cfg" "$CPBACKUP_DEST_PATH/daily/cpbackupstatus.cfg"
        fi
    else
        # no daily, nothing to rename
        echo "$CPBACKUP_DEST_PATH/daily not found"
    fi
    
    # find and delete old folders
    rm -Rf `ls $CPBACKUP_DEST_PATH/daily_* -td | tail -n +$DAYS_TO_KEEP`
    rm -Rf `ls $CPBACKUP_DEST_PATH/weekly_* -td | tail -n +$WEEKS_TO_KEEP`
    
    exit 1
     
  3. gswahhab

    gswahhab Registered

    Joined:
    Mar 18, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    exactly what i was looking for thanks for sharing. how do you get this script to run before the backup?
     
Loading...

Share This Page