Skip to content

Instantly share code, notes, and snippets.

@xr09
Last active January 4, 2016 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr09/8674546 to your computer and use it in GitHub Desktop.
Save xr09/8674546 to your computer and use it in GitHub Desktop.
Minimal rotating backup system
#!/usr/bin/bash
backup_number=8
base_name="backup.gz"
backup_path="/opt/somebackup/"
cd $backup_path
# clean any old temp backup
rm -f *.tmp
# change this line with your specific backup: pgdump, tar, rsync, etc..
echo "$(date +%T)" > "$base_name".tmp
# make sure the backups are getting done before discarding old ones
if [ ! -f "$base_name".tmp ]
then
echo "ERROR: Backup file \"$base_name.tmp\" not found"
exit -1
fi
# delete the oldest
rm -f $base_name.$backup_number
# rotate backups
for i in $(seq $backup_number -1 2)
do
if [ -f "$base_name".$(($i-1)) ]
then
mv "$base_name".$(($i-1)) "$base_name".$i
fi
done
# tmp is now the newest backup
mv "$base_name".tmp "$base_name".1
@pgl
Copy link

pgl commented May 15, 2014

Could use a set -e?

@xr09
Copy link
Author

xr09 commented Aug 4, 2014

Don't know if I should, this confuses me. http://mywiki.wooledge.org/BashFAQ/105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment