Skip to content

Instantly share code, notes, and snippets.

@tjdett
Last active October 12, 2017 04:54
Show Gist options
  • Save tjdett/d6bb193374be8c097bdc0f4d3098525a to your computer and use it in GitHub Desktop.
Save tjdett/d6bb193374be8c097bdc0f4d3098525a to your computer and use it in GitHub Desktop.
Backup script for use with backup2swift
[Unit]
Description=Run backup script
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'PATH=/opt/bin:$PATH /opt/bin/backup.sh /etc/systemd/system /opt/bin'
#!/bin/bash
# (c) The University of Queensland 2017
# MIT Licensed (https://opensource.org/licenses/MIT)
set -e
TIMESTAMP=$(date +'%Y%m%dT%H%M%S')
KEEP_SECS=$(expr 7 '*' 24 '*' 60 '*' 60 )
# If today is Sunday
if [[ $(date +'%w') == '0' ]]; then
# Keep backups for four weeks
KEEP_SECS=$(expr 4 '*' 7 '*' 24 '*' 60 '*' 60 )
# On every fouth Sunday
if [[ $(expr $(date +'%W') % 4) == '0' ]]; then
# Keep backups for a year
KEEP_SECS=$(expr 365 '*' 24 '*' 60 '*' 60 )
fi
fi
for DIR in "$@"
do
NAME=$(basename $DIR)
BASEDIR=$(dirname $DIR)
DEST=/var/backup/${TIMESTAMP}-${NAME}.tar
tar cf "$DEST" -C "$BASEDIR" "$NAME"
backup2swift backup -c /var/backup/config.json --delete-after $KEEP_SECS "$DEST"
rm "$DEST"
done
[Unit]
Description=Run backup script
[Timer]
OnCalendar=*-*-* 16:00:00
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment