Simple Script to Make Rotating Backups of a Directory

There are times where I want to make a simple, rotating backup of a directory so I can go back and look at how thing were in the past, or push a backup to another location. Below is the basic script I use. It makes a timestamped tgz, and then deletes the backups older then some number of days. file contents:
tar -czvf ~/devbak/public_html_$(date +%m%d%y-%T).tgz ~/public_html/* –exclude=*work* –exclude=*docs* –exclude=*.log –exclude=*.svn* –exclude=*.git*
# command to delete old backups
find ~/devbak/*.tgz -mtime +7 -exec rm -f {} \;
It puts the backups in a dir “devbak” and excludes a number directories that can bulk the file up, but not be very useful for the intended purpose. You will certainly need to look it over and tweak the script for your purpose.
To have it automatically run once a day, add the script to a cronjob.
You can edit you cronjob with the command:
crontab -e
Example line to add:
# 0 1 * * *   /home/user/cronjobs/
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *