diff --git a/defaults/main.yml b/defaults/main.yml index cc09f97..0792d88 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,12 +1,16 @@ --- borgbackup_required: True -borgbackup_ssh_key: "~/.ssh/id_borg_rsa" +borgbackup_client_user: root +borgbackup_ssh_key: "~{{ borgbackup_client_user }}/.ssh/id_borg_rsa" -borgbackup_version: "1.0.11" -borgbackup_checksum: "sha256:fbdc0e0d6d05a0935551f2f408f370236a76b7a30d3bb90a31c3628fe3611359" +borgbackup_version: "1.1.4" +borgbackup_checksum: "sha256:4ecf507f21f0db7c437b2ef34566273d7ba5a7d05e921c6f0e3406c3f96933a7" borgbackup_download_url: "https://github.com/borgbackup/borg/releases/download/{{ borgbackup_version }}/borg-linux64" +borgbackup_compression: "auto,zlib,6" +borgbackup_encryption_mode: keyfile + borgbackup_pre_commands: - '[[ ! -f "/usr/sbin/automysqlbackup" ]] || /usr/sbin/automysqlbackup' @@ -28,7 +32,6 @@ borgbackup_retention: monthly: 6 yearly: 1 -borgbackup_cron_user: root borgbackup_cron_day: "*" borgbackup_cron_hour: "{{ 5|random }}" borgbackup_cron_minute: "{{ 59|random }}" diff --git a/tasks/borg-client.yml b/tasks/borg-client.yml index fce6b6a..d67df3b 100644 --- a/tasks/borg-client.yml +++ b/tasks/borg-client.yml @@ -1,6 +1,11 @@ --- - name: client | generate ssh key for this machine - shell: if [ -f {{ borgbackup_ssh_key }} ]; then rm -f {{ borgbackup_ssh_key }}; fi && ssh-keygen -q -t rsa -b 4096 -f {{ borgbackup_ssh_key }} -N "" creates="{{ borgbackup_ssh_key }}.pub" + user: + name: "{{ borgbackup_client_user }}" + generate_ssh_key: yes + ssh_key_bits: 2048 + ssh_key_file: "{{ borgbackup_ssh_key }}" + ssh_key_type: rsa - name: client | fetch ssh-key shell: "cat {{ borgbackup_ssh_key }}.pub" @@ -9,14 +14,14 @@ - name: client | write passphrase lineinfile: - dest: "/root/.borg.passphrase" + dest: "~{{ borgbackup_client_user }}/.borg.passphrase" state: "present" line: 'export BORG_PASSPHRASE="{{ borgbackup_passphrase }}"' create: "yes" - name: client | disable strict key checking for backup servers blockinfile: - dest: /root/.ssh/config + dest: "~{{ borgbackup_client_user }}/.ssh/config" create: yes marker: "### {mark} ANSIBLE MANAGED BLOCK {{ item.fqdn }} ###" content: | @@ -90,13 +95,14 @@ - name: client | create backup-directory on backup server shell: /usr/local/bin/borg-backup init + become_user: "{{ borgbackup_client_user }}" register: backup_init changed_when: "'Remember your passphrase' in backup_init.stderr" - name: client | create backup cronjob cron: cron_file: "borg-backup" - user: "{{ borgbackup_cron_user }}" + user: "{{ borgbackup_client_user }}" name: "borg-backup" minute: "{{ borgbackup_cron_minute }}" hour: "{{ borgbackup_cron_hour }}" diff --git a/templates/borg-backup.sh.j2 b/templates/borg-backup.sh.j2 index 3f89d6d..9c631e1 100644 --- a/templates/borg-backup.sh.j2 +++ b/templates/borg-backup.sh.j2 @@ -7,7 +7,7 @@ fi # Sourcing the backup-passphrase -. /root/.borg.passphrase +. ~{{ borgbackup_client_user }}/.borg.passphrase # Small helper commands, like listing backups, will help us in the future :) @@ -16,7 +16,7 @@ if [ "$1" = "info" ] if [ -z "$2" ]; then printf "run $0 with list and use the backup-tag to request information\n"; exit 1; fi {% for b in borgbackup_servers %} REPOSITORY={% if b.type == 'hetzner' %}ssh://{% endif %}{{ b.user }}@{{ b.fqdn }}:{% if b.type == 'hetzner' %}23/./{% endif %}{{ b.home }}{{ b.pool }}/{{ inventory_hostname }} - /usr/local/bin/borg info $REPOSITORY::$2 {{ b.options }} + /usr/local/bin/borg info {{ b.options }} $REPOSITORY::$2 {% endfor %} exit 0 fi @@ -28,7 +28,7 @@ if [ "$1" = "mount" ] if [ -z "$4" ]; then printf "Select the path to mount the backup on\n"; exit 1; fi {% for b in borgbackup_servers %} REPOSITORY={% if b.type == 'hetzner' %}ssh://{% endif %}{{ b.user }}@{{ b.fqdn }}:{% if b.type == 'hetzner' %}23/./{% endif %}{{ b.home }}{{ b.pool }}/{{ inventory_hostname }} - /usr/local/bin/borg mount $REPOSITORY::$3 $4 {{ b.options }} + /usr/local/bin/borg mount {{ b.options }} $REPOSITORY::$3 $4 if [ "$?" = "0" ]; then printf "Backup mounted on $4, do not forget to unmount!\n"; fi exit 0 {% endfor %} @@ -39,7 +39,7 @@ if [ "$1" = "list" ] {% for b in borgbackup_servers %} REPOSITORY={% if b.type == 'hetzner' %}ssh://{% endif %}{{ b.user }}@{{ b.fqdn }}:{% if b.type == 'hetzner' %}23/./{% endif %}{{ b.home }}{{ b.pool }}/{{ inventory_hostname }} printf "Archives on {{ b.fqdn }} :\n" - /usr/local/bin/borg list -v $REPOSITORY {{ b.options }} + /usr/local/bin/borg list {{ b.options }} -v $REPOSITORY {% endfor %} exit 0 fi @@ -48,7 +48,7 @@ if [ "$1" = "init" ] then {% for b in borgbackup_servers %} REPOSITORY={% if b.type == 'hetzner' %}ssh://{% endif %}{{ b.user }}@{{ b.fqdn }}:{% if b.type == 'hetzner' %}23/./{% endif %}{{ b.home }}{{ b.pool }}/{{ inventory_hostname }} - /usr/local/bin/borg init $REPOSITORY {{ b.options }} + /usr/local/bin/borg init --encryption={{ borgbackup_encryption_mode }} {{ b.options }} $REPOSITORY {% endfor %} exit 0 fi @@ -66,13 +66,13 @@ if [ "$1" = "backup" ] printf "Backing up to {{ b.fqdn }} :\n" REPOSITORY={% if b.type == 'hetzner' %}ssh://{% endif %}{{ b.user }}@{{ b.fqdn }}:{% if b.type == 'hetzner' %}23/./{% endif %}{{ b.home }}{{ b.pool }}/{{ inventory_hostname }} - /usr/local/bin/borg create -p --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 %} {% for dir in borgbackup_exclude %} --exclude '{{ dir }}'{% endfor %} + /usr/local/bin/borg create --progress --compression {{ borgbackup_compression }} --stats {{ b.options }} $REPOSITORY::$date {% for dir in borgbackup_include %}{{ dir }} {% endfor %}{% if automysql.stat.isdir is defined and automysql.stat.isdir == True %}/var/lib/automysqlbackup{% endif %} {% for dir in borgbackup_exclude %} --exclude '{{ dir }}'{% endfor %} if [ "$?" -eq "0" ]; then printf "Backup succeeded on $date to {{ b.fqdn }}\n" >> /var/log/borg-backup.log; fi {% if not borgbackup_appendonly %} # prune old backups - /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 }} + /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 }} {% endif %} {% endfor %}