2018-01-11 22:26:43 +00:00
#!{{ borgbackup_shell }}
2017-09-07 18:35:01 +00:00
if [ -z "$1" ] || [ ! -z "$2" ]
then
printf "Possible: info | init | list | backup | mount \n\n"
fi
# Sourcing the backup-passphrase
2018-01-28 17:01:15 +00:00
. ~{{ borgbackup_client_user }}/.borg.passphrase
2017-09-07 18:35:01 +00:00
# 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 %}
2021-03-10 09:05:53 +00:00
{% if b.port %}
REPOSITORY=ssh://{{ b.user }}@{{ b.fqdn }}:{{ b.port }}/{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2018-08-16 07:01:44 +00:00
{% else %}
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
{% endif %}
2018-01-28 18:35:00 +00:00
/usr/local/bin/borg info {{ b.options }} $REPOSITORY::$2
2017-09-07 18:35:01 +00:00
{% 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 %}
2021-03-10 09:05:53 +00:00
{% if b.port %}
REPOSITORY=ssh://{{ b.user }}@{{ b.fqdn }}:{{ b.port }}/{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2018-08-16 07:01:44 +00:00
{% else %}
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
{% endif %}
2018-01-28 18:35:00 +00:00
/usr/local/bin/borg mount {{ b.options }} $REPOSITORY::$3 $4
2017-09-07 18:35:01 +00:00
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 %}
2021-03-10 09:05:53 +00:00
{% if b.port %}
REPOSITORY=ssh://{{ b.user }}@{{ b.fqdn }}:{{ b.port }}/{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2018-08-16 07:01:44 +00:00
{% else %}
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
{% endif %}
2017-09-07 18:35:01 +00:00
printf "Archives on {{ b.fqdn }} :\n"
2018-01-28 18:35:00 +00:00
/usr/local/bin/borg list {{ b.options }} -v $REPOSITORY
2017-09-07 18:35:01 +00:00
{% endfor %}
exit 0
fi
if [ "$1" = "init" ]
then
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2021-03-10 09:05:53 +00:00
{% if b.port %}
REPOSITORY=ssh://{{ b.user }}@{{ b.fqdn }}:{{ b.port }}/{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2018-08-16 07:01:44 +00:00
{% else %}
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
{% endif %}
2018-05-19 14:13:19 +00:00
/usr/local/bin/borg init --encryption={{ borgbackup_encryption_mode }}{% if borgbackup_appendonly_repoconfig %} --append-only{% endif %} {{ b.options }} $REPOSITORY
2017-09-07 18:35:01 +00:00
{% endfor %}
exit 0
fi
if [ "$1" = "backup" ]
then
date=`date +%Y%m%d-%H%M`
2019-07-11 20:08:09 +00:00
LOG_DEST="/var/log/borgbackup/${date}"
LOG_FILE="${LOG_DEST}.log"
log(){ echo -e "\n$(date '+%Y-%m-%d %H:%M:%S') $@" | tee -a "$LOG_FILE"; }
_term(){ echo -e "\n## END ##" | tee -a "$LOG_FILE"; exit 1; }
trap _term SIGINT SIGTERM
# Running some commands pre-backup
(
2017-10-01 11:05:59 +00:00
{% for precommand in borgbackup_pre_commands %}
2019-07-11 20:08:09 +00:00
{{ precommand }}
2017-09-07 18:35:01 +00:00
{% endfor %}
2019-07-11 20:08:09 +00:00
) &>> "$LOG_FILE"
{% if borgbackup_remote_ratelimit %}
{% set rate_limit %} --remote-ratelimit={{borgbackup_remote_ratelimit}} {% endset %}
{% else %}
{% set rate_limit = " " %}
{% endif %}
2017-09-07 18:35:01 +00:00
2017-10-01 17:51:38 +00:00
{% for b in borgbackup_servers %}
2021-03-10 09:05:53 +00:00
{% if b.port %}
REPOSITORY=ssh://{{ b.user }}@{{ b.fqdn }}:{{ b.port }}/{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
2018-08-16 07:01:44 +00:00
{% else %}
REPOSITORY={{ b.user }}@{{ b.fqdn }}:{{ b.home }}{{ b.pool }}/{{ inventory_hostname }}
{% endif %}
2017-09-07 18:35:01 +00:00
2019-07-11 20:08:09 +00:00
log "## Backing up to {{ b.fqdn }} "
/usr/local/bin/borg create {{ rate_limit }} -x --compression {{ borgbackup_compression }} --stats {{ b.options }} $REPOSITORY::$date {% for dir in borgbackup_include %}{{ dir }} {% endfor %} {% for dir in borgbackup_exclude %} --exclude '{{ dir }}'{% endfor %} &>> $LOG_FILE
2017-09-07 18:35:01 +00:00
2018-01-11 22:21:43 +00:00
if [ "$?" -eq "0" ]; then printf "Backup succeeded on $date to {{ b.fqdn }}\n" >> /var/log/borg-backup.log; fi
2017-10-01 17:24:50 +00:00
2019-07-11 20:08:09 +00:00
log "## Checking the archive integrity "
/usr/local/bin/borg check $REPOSITORY::$date -v &>> "$LOG_FILE"
log "## Retrieving archive json file"
/usr/local/bin/borg info $REPOSITORY::$date --json > "${LOG_DEST}.json"
2017-10-01 17:24:50 +00:00
{% if not borgbackup_appendonly %}
2019-07-11 20:08:09 +00:00
log "## Pruning the repository"
/usr/local/bin/borg prune {{ b.options }} -v $REPOSITORY -H {{ borgbackup_retention.hourly }} -d {{ borgbackup_retention.daily }} -w {{ borgbackup_retention.weekly }} -m {{ borgbackup_retention.monthly }} -y {{ borgbackup_retention.yearly }} &>> "$LOG_FILE"
2017-10-01 17:24:50 +00:00
{% endif %}
2017-09-07 18:35:01 +00:00
{% endfor %}
2018-01-11 22:24:11 +00:00
# Running some commands post-backup
2019-07-11 20:08:09 +00:00
{% if borgbackup_post_commands |length > 1 %}
(
{% for postcommand in borgbackup_post_commands %}
2018-01-11 22:24:11 +00:00
{{ postcommand }}
2019-07-11 20:08:09 +00:00
{% endfor %}
) &>> "$LOG_FILE"
{% endif %}
2018-01-11 22:24:11 +00:00
2019-07-11 20:08:09 +00:00
_term
2017-09-07 18:35:01 +00:00
fi