system backup cleanup script

soulshepard

Verified User
Joined
Feb 7, 2008
Messages
128
dont know if any has allready has a script like this or a functionality is coming soon, but to not run out of diskspace i have made this addition as a cron script to clean up the "xx-xx-xx" type system backups that da makes

in addition i use the admin backup/transfer for all the user/reseller data backup this overwrites nicely
the system backup if you would ever need to rebuild fast it would be handy to have it thats why i also use the systembackup.

if any one has an idea of how i could improve suggestions are allways welcome. but this is done to my abbilities and view and it works for my servers

the script parses a directory, finds the folderd xx-xx-xx, replaces the "-" and substracts the days, in a case loop i walk all those results and removes all but the last 2 days, another way to shorten this is i would only need the previous day then it would be a bit simpeler but for me this is better.

would be nice to see a functionality like this in DA and even better if we would have a pre and post script function so you can customise your actiosn before backups and after backup (pre:mount stuff set rights, start stop stuff and post:dismount copy start stop other stuff)

run this script in your crontab
it uses vars for the binaries that are resolved with which, would be wice to set the which path first and all other would resolve. i dod not made a check if those executables exists, for now this would be bloating a simple functionality but is not unthinkable.

to enable the script please set the ENABLESCRIPT to 1 and TESTMODE to 0 and verify the WHICH path

Soul.


HTML:
#!/bin/sh
#
### Clean DA system backup script ###
###
### version 1.2
###
### jan [AT] makeitwork [DOT] nu
###

## Config
##
## set these values to enable the script. (0=off 1=on)
#
ENABLESCRIPT=0
TESTMODE=1

## Supply the which binary path, this will resolve all other binaries for the script.
#
WHICH="/usr/bin/which"

## Directadmin backup Path
#
BACKUPPATH=/backup/directadmin-system

## Date format
# direactadmin uses month-day-year (with creating my backup folders) with sed we remove the - and match the date format in the caseselect
#
DATEFORMAT="+%m%d%y"


## Binary Setup
#
DATE="$($WHICH date)"
GREP="$($WHICH grep)"
CUT="$($WHICH cut)"
RM="$($WHICH rm)"
SED="$($WHICH sed)"
LS="$($WHICH ls | $GREP -v "alias")"
ECHO="$($WHICH echo)"

### Start Executing
##
# make sure we did edit the script after download.
#
if [ ! "x$ENABLESCRIPT" = "x1" ];
then
        $ECHO edit the script to anable it
exit 1
fi

## Collect backup entries in the supplied folder.
#
LSOUTPUT="$($LS $BACKUPPATH | $SED 's/-//g' | $GREP -i $($DATE +"%y"))"

## Main Loop
#
for results in $LSOUTPUT
do

## Make sure $results is not empty
#
if [ ! "x$results" = "x" ];
        then
                ## recreating filename from dateformat
                #
                REMOVETARGET=`$ECHO ${results:0:2}-${results:2:2}-${results:4:2}`
                #
                ## walk each $results and remove it if it does not hit, the case else (*) will remove the entry.
                #
                case $results in

                        ## skip today
                        #
                        $($DATE --date='-0 day' "$DATEFORMAT"))
                                $ECHO skipping : $BACKUPPATH/$REMOVETARGET
                                ;;

                        ## skip yesterday
                        #
                        $($DATE --date='-1 day' "$DATEFORMAT"))
                                $ECHO skipping : $BACKUPPATH/$REMOVETARGET
                                ;;

                        ## remove all other
                        #
                        *                )
                                $ECHO REMOVING : $BACKUPPATH/$REMOVETARGET
                                if [ "x$TESTMODE" = "x1" ];
                                then
                                        $ECHO "*** Testmode will not delete, edit script to disable ***"
                                else
                                        if [ ! "x$REMOVETARGET" = "x" ] ;
                                        then
                                                $ECHO "         : $RM -rf $BACKUPPATH/$REMOVETARGET"
                                                $RM -rf $BACKUPPATH/$REMOVETARGET
                                        fi
                                fi
                                ;;
                esac

                REMOVETARGET=

        else
                $ECHO "error empty results $results"
        fi
done
 
Last edited:
You probably should not be using system backup anyway. The admin backup does everything you need and has a restore function.
 
You probably should not be using system backup anyway. The admin backup does everything you need and has a restore function.

as mentioned i allready use the admin backup. but the system backup is for fast system restore thats why i wrote the schript to let it run but clean up the not used onces :d
 
I wrote a little script, posted here a while back,
to move each days backups into a folder like
'mon', or 'tues', or whatever the day of the week was,
and was only 3 lines long I think:

http://www.directadmin.com/forum/showthread.php?t=9913

you could, before running that, delete anything in your
backup directory which is over 1.5 days old, say,
which would be a one-line script.

then you'd have at most 2 days backups, or thereabouts.
 
If anything happens to the system files mostly likely the hard drive is going bad or you were hacked. In either case you have to do a fresh install of the OS and DA.
 
Last edited:
If anything happens to the system files mostly likely the hard drive is going back or you were hacked. In either case you have to do a fresh install of the OS and DA.

Exactly - we've never backed up the system files, it's too easy to restore the user sites on a fresh install and fresh hardware, which prevents any rootkits or compromised files from being transferred over with it.
 
I wrote a little script, posted here a while back,
to move each days backups into a folder like
'mon', or 'tues', or whatever the day of the week was,
and was only 3 lines long I think:

http://www.directadmin.com/forum/showthread.php?t=9913

you could, before running that, delete anything in your
backup directory which is over 1.5 days old, say,
which would be a one-line script.

then you'd have at most 2 days backups, or thereabouts.

thx squirrelhost, indeed these two linux do the same :)
me and my old C= 64 habbits of scripting... (allways trying to do every little step)

#!/usr/local/bin/bash
rm -rf /backup/system/$(date +"%u")
mv /backup/system/$(date +"%m-%d-%y") /backup/system/$(date +"%u")

in defence of the systembackup script. i agree a fresh install is faster, but it can also include files and folders on your da server that fall out of the scope of a user backup. and the config files that may be altered are handy to review even if you are not restoring the backup, for me a restore into a test enviroment to check before i upgrade my production servers with any libs or binaries.

anyway .. thx for squirrelhost nad it completely killing a few work hours in 3 lines .. har har :D
 
Back
Top