diff --git a/defaults/main.yml b/defaults/main.yml index 82c4230..ff13be7 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -2,7 +2,8 @@ restic_install: false restic_version: 0.9.4 -restic_script_path: /root/restic-backup.sh +restic_path: /usr/local/bin/restic +restic_env_file_path: /root/.restic_env restic_repository_name: restic restic_default_folders: @@ -14,6 +15,7 @@ restic_databases: [] restic_forget: true restic_forget_keep_within: 30d restic_prune: true +restic_check: true restic_ssh_host: backup restic_ssh_port: 22 diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..74869f1 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,5 @@ +--- + +- name: systemd reload + systemd: + daemon_reload: yes diff --git a/tasks/install.yml b/tasks/install.yml index 17e9dca..8ec96e6 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -1,9 +1,10 @@ --- -- name: Install fuse + +- name: Install fuse (to mount repositories) apt: name: fuse -- name: Install bzip2 +- name: Install bzip2 (to install restic) apt: name: bzip2 @@ -21,7 +22,7 @@ copy: remote_src: true src: '/tmp/restic_{{ restic_version }}_linux_amd64' - dest: /usr/local/bin/restic + dest: "{{ restic_path }}" mode: 0755 - name: Remove downloaded file diff --git a/tasks/main.yml b/tasks/main.yml index a088588..0df0aa3 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,14 +2,13 @@ - name: Check if restic is installed stat: - path: /usr/local/bin/restic + path: "{{ restic_path }}" register: restic_binary - - include_tasks: install.yml when: not restic_binary.stat.exists or restic_install -- name: Add SSH config +- name: Overwrite SSH config for backup server template: src: ssh_config.j2 dest: /root/.ssh/config @@ -24,33 +23,27 @@ mode: 0600 when: restic_ssh_private_key is defined -- name: Add restic-env +- name: Add restic_env in home folder template: - src: restic-env.j2 - dest: /root/.restic-env + src: restic_env.j2 + dest: restic_env_file_path owner: root group: root mode: 0600 -- name: Add restic-backup.sh - template: - src: restic-backup.sh.j2 - dest: /root/restic-backup.sh - owner: root - group: root - mode: 0700 - vars: - restic_folders_combined: '{{ restic_default_folders + restic_folders }}' - - name: Add systemd service for restic template: src: restic-backup.service.j2 dest: /etc/systemd/system/restic-backup.service + vars: + restic_folders_combined: '{{ restic_default_folders + restic_folders }}' + notify: systemd reload - name: Add systemd timer for restic template: src: restic-backup.timer.j2 dest: /etc/systemd/system/restic-backup.timer + notify: systemd reload - name: Enable restic timer systemd: diff --git a/templates/restic-backup.service.j2 b/templates/restic-backup.service.j2 index 9b6f814..80b5996 100644 --- a/templates/restic-backup.service.j2 +++ b/templates/restic-backup.service.j2 @@ -3,5 +3,26 @@ Description=Restic backup [Service] Type=oneshot -ExecStart={{ restic_script_path }} User={{ restic_user }} +Environment="RESTIC_REPOSITORY=sftp:{{ restic_ssh_host }}:{{ restic_repository_name }}" +Environment="RESTIC_PASSWORD={{ restic_password}}" + +{% if restic_check %} +ExecStartPre={{ restic_path }} check +{% endif -%} + +{% for folder in restic_folders_combined %} +ExecStart={{ restic_path }} backup --verbose {{ folder.path }} {{ folder.exclude if folder.exclude is defined else '' }} +{% endfor -%} + +{% for database in restic_databases %} +ExecStart={{ database.dump_command}} | {{ restic_path }} backup --verbose --stdin --stdin-filename {{ database.name }}.sql +{% endfor -%} + +{% if restic_forget %} +ExecStartPost={{ restic_path }} forget --keep-within {{ restic_forget_keep_within }} +{% endif -%} + +{% if restic_prune %} +ExecStartPost={{ restic_path }} prune +{% endif -%} diff --git a/templates/restic-backup.sh.j2 b/templates/restic-backup.sh.j2 deleted file mode 100644 index c095501..0000000 --- a/templates/restic-backup.sh.j2 +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -source ~/.restic-env - -echo -e "\n`date` - Starting backup...\n" - -{% for folder in restic_folders_combined %} -restic backup --verbose {{ folder.path }} {{ folder.exclude if folder.exclude is defined else '' }} -{% endfor %} - -{% for database in restic_databases %} -{{ database.dump_command}} | restic backup --verbose --stdin --stdin-filename {{ database.name }}.sql -{% endfor -%} - -echo -e "\n`date` - Running forget and prune...\n" - -{% if restic_forget %} -restic forget --keep-within {{ restic_forget_keep_within }} -{% endif %} - -{% if restic_prune %} -restic prune -{% endif %} - -echo -e "\n`date` - Backup finished.\n" - diff --git a/templates/restic-env.j2 b/templates/restic_env.j2 similarity index 100% rename from templates/restic-env.j2 rename to templates/restic_env.j2