Skip to content

Instantly share code, notes, and snippets.

@sbryant
Created July 28, 2014 18:17
Show Gist options
  • Save sbryant/de93ae53c379b196a332 to your computer and use it in GitHub Desktop.
Save sbryant/de93ae53c379b196a332 to your computer and use it in GitHub Desktop.
prune_old_backups() {
[ -n "$backups_to_keep" ] || return
local num_re="[0-9]+"
[ "$backups_to_keep" =~ "$num_re" ] || { echo "Backups to keep needs to be a number"; return }
num_of_backups=$(ls -l "$backup_dir" | wc -l)
if [ "$num_of_backups" > "$backups_to_keep" ]; then
echo "Pruning backups"
dirs_to_remove=$(ls -t | awk "NR>$backups_to_keep")
echo "would remove $dirs_to_remove"
echo "echo $dir_to_remove | xargs rm -rf"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment