2017-09-07 18:35:01 +00:00
#!/bin/bash
if [ -z "$1" ] || [ ! -z "$2" ]
then
printf "Possible: info | init | list | backup | mount \n\n"
fi
# Sourcing the backup-passphrase
. /root/.borg.passphrase
# Small helper commands, like listing backups, will help us in the future :)
if [ "$1" = "info" ]
then
if [ -z "$2" ]; then printf "run $0 with list and use the backup-tag to request information\n"; exit 1; fi
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2017-09-07 18:35:01 +00:00
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
/usr/local/bin/borg info $REPOSITORY::$2 {{ b.options }}
{% endfor %}
exit 0
fi
if [ "$1" = "mount" ]
then
if [ -z "$2" ]; then printf "Select the backup-server\n"; exit 1; fi
if [ -z "$3" ]; then printf "Select the backup to mount\n"; exit 1; fi
if [ -z "$4" ]; then printf "Select the path to mount the backup on\n"; exit 1; fi
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2017-09-07 18:35:01 +00:00
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
/usr/local/bin/borg mount $REPOSITORY::$3 $4 {{ b.options }}
if [ "$?" = "0" ]; then printf "Backup mounted on $4, do not forget to unmount!\n"; fi
exit 0
{% endfor %}
fi
if [ "$1" = "list" ]
then
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2017-09-07 18:35:01 +00:00
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
printf "Archives on {{ b.fqdn }} :\n"
/usr/local/bin/borg list -v $REPOSITORY {{ b.options }}
{% endfor %}
exit 0
fi
if [ "$1" = "init" ]
then
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2017-09-07 18:35:01 +00:00
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
borg init $REPOSITORY {{ b.options }}
{% endfor %}
exit 0
fi
if [ "$1" = "backup" ]
then
date=`date +%Y%m%d-%H%M`
# Running some commands pre-backup
2017-10-01 11:05:59 +00:00
{% for precommand in borgbackup_pre_commands %}
2017-09-07 18:35:01 +00:00
{{ precommand }}
{% endfor %}
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2017-09-07 18:35:01 +00:00
printf "Backing up to {{ b.fqdn }} :\n"
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2017-10-01 11:05:59 +00:00
/usr/local/bin/borg create --compression zlib,6 --stats $REPOSITORY::$date {{ b.options }} {% for dir in borgbackup_include %}{{ dir }} {% endfor %}{% if automysql.stat.isdir is defined and automysql.stat.isdir == True %}/var/lib/automysqlbackup{% endif %}
2017-09-07 18:35:01 +00:00
if [ "$?" -eq "0" ]; then printf "Backup succeeded on $date\n" >> /var/log/borg-backup.log; fi
2017-10-01 17:24:50 +00:00
{% if not borgbackup_appendonly %}
# prune old backups
2017-10-01 11:05:59 +00:00
/usr/local/bin/borg prune -v $REPOSITORY {{ b.options }} -H {{ borgbackup_retention.hourly }} -d {{ borgbackup_retention.daily }} -w {{ borgbackup_retention.weekly }} -m {{ borgbackup_retention.monthly }} -y {{ borgbackup_retention.yearly }}
2017-10-01 17:24:50 +00:00
{% endif %}
2017-09-07 18:35:01 +00:00
{% endfor %}
fi