Skip to content

Instantly share code, notes, and snippets.

@babolivier
Last active October 8, 2018 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babolivier/d88cd4bc7f297f11bf04211f5f570ec7 to your computer and use it in GitHub Desktop.
Save babolivier/d88cd4bc7f297f11bf04211f5f570ec7 to your computer and use it in GitHub Desktop.
Script run as a cron task that launch backups creation and upload created backups to an OpenStack Swift container, after having deleted every backup older than a week old. Place the scripts creating backups in /etc/backup. If you want the backups directory to be cleaned first, create a script named like "00_clean.sh" that does that.
#!/bin/bash -e
# Log in error log
1>&2 echo "--- `date -Is` ---"
# Log in output log
echo "--- `date -Is` ---"
if [ "$#" != "1" ]; then
1>&2 echo "Usage: backup.sh HOST_NAME"
exit 1
fi
hostname=$1
# Export OpenStack auth environment variables
source /home/brendan/openrc.sh
d=`date +"%Y%m%d"`
echo "Running backup scripts..."
sudo run-parts --arg=$hostname --arg=$d /etc/backup
cd /var/backups
echo "Uploading backups..."
openstack object create backups $hostname/*-$d.tar.gz > /dev/null
echo "Checking if there are backups older than a week old for this host..."
backup_files=`openstack object list backups -f value --prefix $hostname`
one_week_ago=`date -d "1 week ago" +"%Y%m%d"`
backups_to_remove=""
for f in $backup_files; do
fdate=`echo "$f" | rev | cut -d"-" -f1 | rev | cut -d"." -f1`
if [[ $fdate -lt $one_week_ago ]]; then
backups_to_remove="$backups_to_remove $f"
fi
done
if [ -n "$backups_to_remove" ]; then
echo "Deleting all backups but the ones from the past week (sliding window)..."
openstack object delete backups $backups_to_remove
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment