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.