Rotating Folder Backup Script

How do you create folder backup archives and limit the size by rotating old ones out? A common task and there are plenty of solutions. This is more or less the poor man's solution I'm running from cron.

This script will process each sub folder of a given base directory. If there are modified files it will create a archive tar and possibly clean out old ones. The last 7-days of snapshots and each 1st of the month will be kept.

  1. #!/bin/bash
  3. BASEDIR=/home/projects # SET THIS
  4. BAKDIR=/home/backups # SET THIS
  6. stamp=$(date +%Y-%m-%d)
  8. cd "$BASEDIR"
  9. for prj in * ; do
  11. find "$prj" -mtime -1 -type f | grep "" >/dev/null && {
  12. tar cjf "$BAKDIR/${prj}_${stamp}.tar.bz2" "$prj"
  13. find "$BAKDIR" -name "${prj}_*" -mtime -14 -mtime +7 -not -name "*-01.*" -exec rm {} \;
  14. ls -sh "$BAKDIR/${prj}_"*
  15. }
  17. done

The script needs the bash shell and works with whitespace in directory- and filenames.


Hi Zany,
Can you explain what 'prj' means?

the for do loop enumerates all sub-folders in the BASEDIR (I have one folder for each project, thus prj). Each project is archived on it's own. You can throw that away if you are just looking to archive a single folder. blog © 1999-2016 Christian Zuckschwerdt — powered by seven of drupal — run on lamp