Results 1 to 3 of 3

Thread: My version of "keep daily backups"

  1. #1
    Member
    Join Date
    Feb 2008
    Posts
    81

    Default My version of "keep daily backups"

    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. #2
    Member
    Join Date
    Feb 2008
    Posts
    81

    Default Re: My version of "keep daily backups"

    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. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Default Re: My version of "keep daily backups"

    exactly what i was looking for thanks for sharing. how do you get this script to run before the backup?

Similar Threads

  1. daily /scripts/upcp cron using the correct "Homedir"
    By damainman in forum cPanel & WHM Discussions
    Replies: 0
    Last Post: 05-25-2010, 01:26 AM
  2. High memory usage in "Daily Process Log"
    By labahost in forum Optimization
    Replies: 0
    Last Post: 12-16-2009, 11:02 AM
  3. ok I give <?xml version="1.0" encoding="UTF-8"?>
    By asmithjr in forum cPanel & WHM Discussions
    Replies: 6
    Last Post: 01-09-2008, 01:09 PM
  4. "Restore Backups" AND Restore a "Full Backup/cpmove file"
    By protocol in forum cPanel & WHM Discussions
    Replies: 2
    Last Post: 06-28-2005, 02:53 AM

Tags for this Thread