Delete DA Full System Backup Folder Older than x days

Here is one I wrote in sh:

Code:
#!/bin/sh

backup_directory="/backup/system" # No trailing /
backup_time="2" # 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 ""

You just set it up in a cron and pipe it to the mail command like follows:

0 11,23 * * * /path/to/backupclean | mail -s 'Backup Cleaner' [email protected]
 
chatwizrd said:
Here is one I wrote in sh:

Code:
#!/bin/sh

backup_directory="/backup/system" # No trailing /
backup_time="2" # 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 ""

You just set it up in a cron and pipe it to the mail command like follows:

0 11,23 * * * /path/to/backupclean | mail -s 'Backup Cleaner' [email protected]

Great one :)

the find -mtime is what I start with in my first version
But in a real use I want to keep some backup of someday last month
if you find all folder older than x days you just have to move the folder you want to keep somewhere else.
But I don't want to move it somewhere I just rename it to something difference from default then it will prevent to delete with my script
Also if we run script daily we will not have any folder older than x days to find (unless we change to lower days settings in script)
then it's comeout like that

Now we have choices of 2 kind of methods, Greate!
 
Here's my solution (I couldn't get the one above to actually find the files - I'm running debian 4.0 FWIW)

Create a crontab with this code (probably need to be root):

Code:
find /backup -mtime +7 -type d -depth -exec rm -r {} \;

where "/backup" is the directory the backups are in and "7" is the number of days to keep the backups before they're deleted.
 
(I couldn't get the one above to actually find the files - I'm running debian 4.0 FWIW)

In the case script not work as it should to, check what's say in the log file
/var/log/delbackup.log

Here is the script installation:
1. Down load and extract
2. upload extracted file (deloldbackup.pl) to server
3. chmod 755 deloldbackup.pl
4. add the following line to crontab #crontab -e
0 5 * * * root /path/deloldbackup.pl

Please note: The script will do delete only 1 folder each day (exactly x day old folder), the folder older than x days will still remain.


If you have 30 folders for last 30 days in the folder and setup script for 7 days
you will left 29 folders after running the script.

The logic is, if you run script everyday you will never have folder older than x+1 day, so no need to waste the time to search for it, the script is just do delete exactly x days old folder.

:)
 
Back
Top