Removing old server backups?

chatwizrd

Verified User
Joined
Jul 3, 2005
Messages
2,005
Does anyone know of a shell or perl script I could run in my server backup directory to remove old backups. We backup all users home directorys and after about 10-15 days the backup harddrive gets too full. I can code shell scripts but have no clue what method to use for testing to delete older folders.

I would like to be able to keep files up to 5 days old and delete the rest.

Let me know if anyone has a clue on this.

I am using the server backup method through directadmin.

Thanks
 
Here's what we do: Our backups are copied off of the server nightly, so we use the "clean_tmps" variables in /etc/periodic.conf (see /etc/default/periodic.conf for all of the possible options) to clean the directory where backups are run to.

You could also do something similar with "find /backupdir -atime ..."

Andy
 
currently on a few servers I do the full system
backup, and these are later copied to remote
server by ftp.

the local server backups, are firstly
output into directories such as

/backup/system/10-02-05

for example.

a couple of hours after this backup is
complete I run a shell script to
move this to a directory named
after the day of the week. This
overwrites any previous such directory
and we only then have 7 such backup
directories at the end of each day on
each servers. ( i actually do remove
it first in the script)

the script I sun in cron each day is:
#!/usr/local/bin/bash
rm -rf /backup/system/$(date +"%u")
mv /backup/system/$(date +"%m-%d-%y") /backup/system/$(date +"%u")
 
never write a script from memory!

here's the corrected version, which I
checked on live server:

$ less /usr/local/etc/daily_backup.sh

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


when this ran this morning, it produced

/backup/system/Sun/

with this morning's backups in it.
 
Last edited:
to find old files, or find + delete:

/usr/bin/find /backup/system -mtime +7

this finds all files/dirs under
/backup/system which are older than 7 days.

to delete these also:

/usr/bin/find /backup/system -mtime +7 -delete

( you can add this line to cron )
 
Here is a little script I made to do the task.

Code:
#!/bin/sh

backup_directory="/backup" # Set to backup folder. No trailing /
backup_time="5" # Remove backups how many days old? Number variable only!

#### Commands ####

find_files=`find $backup_directory -maxdepth +1 -mtime +$backup_time`

#### Begin Script ####

ver="Directory cleaner and backup v1.0 by Andrew ([email protected])"

##

echo ""
echo "Looking for old backups... Older then $backup_time days old."
echo "----"
if [ -z "$find_files" ]; then                 
echo "None Found!"
else
echo "$find_files"
fi
echo "----"
echo ""
echo "If any folders were found they have deleted."
find $backup_directory -mtime +$backup_time -delete
echo ""
echo "Done."
echo ""
echo "Thanks for using $ver."
echo ""

How to make it work.

Use the su command to become root.

1. Pico or vi with your favorite editor the following:

vi /sbin/backupcleaner

2. Input the above script into the /sbin/backupcleaner file

3. Edit the variable backup_directory to where your backups are stored.

Example:
backup_directory="/usr/backups"

Dont use a trailing /

4. Change the variable backup_time="5" ... If you so desire. This sets the days it will delete a folder if it is older then the set amount of days.

5. Save the file your editing /sbin/backupcleaner

6. chmod 750 /sbin/backupcleaner

7. Create a crontab.

crontab -e

Input the following:

@daily /sbin/backupcleaner | mail -s 'Backup directory cleaner cron' [email protected]

Change the email [email protected] to the email address you want to be emailed to.

Save the crontab.

This should make it run daily at midnight and email you the results.

----

Works flawless for me on FreeBSD 5.4. Probably will work with any unix/linux.

Let me know if you have any questions/comments.

Thanks to squirrelhost for the find command variables on how to make this work easily.
 
Last edited:
I am on Linux not BSD and got the following error

Code:
find: invalid predicate `-delete'

so I changed the deletion line to the following and it worked

Code:
find $backup_directory -mtime +$backup_time -exec rm -rf {} \;

when testing the script I am very wary of any rm -rf command, so first I modified the script to comment out the actual deletion line to make sure I was getting the right results

then I enabled the deletion line again and tested the script by itself
/sbin/backupcleaner
from the command line interactively but I first changed the deletion line using
-ok
instead of
-exec

(because -ok causes a prompts for Y or N)


then I changed the script back to -exec again and tested the script interactively one more time with the pipe to my mail address. Looks good, so I added it to my cron.

works great, thanks guys! :)
 
Cool. Hope it works out well for you. It does exactly what I was needing it to. I got sick of deleting the folders manually... and when backing up entire user home directorys I dont feel like ftping tons of gigs of data to another server.
 
Back
Top