feat(systemd): use service only for backups

And other cleanup/var improvements
This commit is contained in:
angristan 2019-03-21 10:33:44 +01:00
parent b62df04415
commit 5462e8dee5
7 changed files with 43 additions and 47 deletions

View file

@ -2,7 +2,8 @@
restic_install: false restic_install: false
restic_version: 0.9.4 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_repository_name: restic
restic_default_folders: restic_default_folders:
@ -14,6 +15,7 @@ restic_databases: []
restic_forget: true restic_forget: true
restic_forget_keep_within: 30d restic_forget_keep_within: 30d
restic_prune: true restic_prune: true
restic_check: true
restic_ssh_host: backup restic_ssh_host: backup
restic_ssh_port: 22 restic_ssh_port: 22

5
handlers/main.yml Normal file
View file

@ -0,0 +1,5 @@
---
- name: systemd reload
systemd:
daemon_reload: yes

View file

@ -1,9 +1,10 @@
--- ---
- name: Install fuse
- name: Install fuse (to mount repositories)
apt: apt:
name: fuse name: fuse
- name: Install bzip2 - name: Install bzip2 (to install restic)
apt: apt:
name: bzip2 name: bzip2
@ -21,7 +22,7 @@
copy: copy:
remote_src: true remote_src: true
src: '/tmp/restic_{{ restic_version }}_linux_amd64' src: '/tmp/restic_{{ restic_version }}_linux_amd64'
dest: /usr/local/bin/restic dest: "{{ restic_path }}"
mode: 0755 mode: 0755
- name: Remove downloaded file - name: Remove downloaded file

View file

@ -2,14 +2,13 @@
- name: Check if restic is installed - name: Check if restic is installed
stat: stat:
path: /usr/local/bin/restic path: "{{ restic_path }}"
register: restic_binary register: restic_binary
- include_tasks: install.yml - include_tasks: install.yml
when: not restic_binary.stat.exists or restic_install when: not restic_binary.stat.exists or restic_install
- name: Add SSH config - name: Overwrite SSH config for backup server
template: template:
src: ssh_config.j2 src: ssh_config.j2
dest: /root/.ssh/config dest: /root/.ssh/config
@ -24,33 +23,27 @@
mode: 0600 mode: 0600
when: restic_ssh_private_key is defined when: restic_ssh_private_key is defined
- name: Add restic-env - name: Add restic_env in home folder
template: template:
src: restic-env.j2 src: restic_env.j2
dest: /root/.restic-env dest: restic_env_file_path
owner: root owner: root
group: root group: root
mode: 0600 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 - name: Add systemd service for restic
template: template:
src: restic-backup.service.j2 src: restic-backup.service.j2
dest: /etc/systemd/system/restic-backup.service 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 - name: Add systemd timer for restic
template: template:
src: restic-backup.timer.j2 src: restic-backup.timer.j2
dest: /etc/systemd/system/restic-backup.timer dest: /etc/systemd/system/restic-backup.timer
notify: systemd reload
- name: Enable restic timer - name: Enable restic timer
systemd: systemd:

View file

@ -3,5 +3,26 @@ Description=Restic backup
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart={{ restic_script_path }}
User={{ restic_user }} 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 -%}

View file

@ -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"