Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: auto restore option for backups #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ simply appending them:
$ /path/to/backup/script/backup-example.sh --tag deployment
```

### Access scripts
```bash
. /path/to/backup/script/access-example.sh
restic snapshots
```

### Restore scripts
```bash
# This will restore the latest backup
/path/to/backup/script/restore-example.sh
```

### CRON / Scheduled Tasks
In order to make use of defined backups, they can be automatically setup as
scheduled tasks. You have to be aware of the fact that (on linux systems at
Expand Down Expand Up @@ -105,6 +117,7 @@ restic_repos:
location: sftp:user@host:/srv/restic-repo
password: securepassword2
init: true

```

### Backups
Expand All @@ -118,6 +131,7 @@ Available variables:
| `name` | yes | The name of this backup. Used together with pruning and scheduling and needs to be unique. |
| `repo` | yes | The name of the repository to backup to. |
| `src` | yes | The source directory or file |
| `auto_restore` | no | This will auto restore the latest backup in the event that a state file does not exist in /var/restic-auto-restore |
| `stdin` | no | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? |
| `stdin_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. |
| `stdin_filename` | no | The filename used in the repository. |
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ restic_repos: {}
restic_backups: []
restic_create_cron: false

restic_auto_restore_dir: /var/restic-auto-restore

restic_dir_owner: '{{ ansible_user | default(ansible_user_id) }}'
restic_dir_group: '{{ ansible_user | default(ansible_user_id) }}'
26 changes: 26 additions & 0 deletions tasks/distribution/Linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@
- item.src is defined or item.stdin and item.stdin_cmd is defined
- item.repo in restic_repos

- name: Create restore script
template:
src: restic_restore_Linux.j2
dest: '{{ restic_script_dir }}/restore-{{ item.name }}.sh'
mode: '0700'
owner: '{{ restic_dir_owner }}'
group: '{{ restic_dir_group }}'
no_log: true
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.src is defined or item.stdin is defined
- item.src is defined or item.stdin and item.stdin_cmd is defined
- item.repo in restic_repos

- name: Setup CRON jobs
cron:
name: 'arillso.restic backup {{ item.name }}'
Expand All @@ -47,3 +62,14 @@
- item.name is defined
- item.scheduled | default(false)
with_items: '{{ restic_backups }}'

- name: restic auto_restore
include: restic_auto_restore.yml
when:
- restic_backups_loop.name is defined
- restic_backups_loop.src is defined or restic_backups_loop.stdin is defined
- restic_backups_loop.src is defined or restic_backups_loop.stdin and restic_backups_loop.stdin_cmd is defined
- restic_backups_loop.repo in restic_repos
loop: "{{ restic_backups }}"
loop_control:
loop_var: restic_backups_loop
25 changes: 25 additions & 0 deletions tasks/distribution/restic_auto_restore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# - name: auto_restore check
# debug:
# msg: "auto_restore in? {{ restic_backups_loop }}"

# - name: auto_restore enabled
# debug:
# msg: "auto_restore enabled {{ restic_backups_loop }}"
# when: restic_backups_loop.auto_restore is defined

- name: "check for a state file {{ restic_auto_restore_dir }}/{{ restic_backups_loop.name }}.state"
stat:
path: "{{ restic_auto_restore_dir }}/{{ restic_backups_loop.name }}.state"
register: restic_restore_state_file
when: restic_backups_loop.auto_restore is defined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would fire even if auto_restore: false

Suggested change
when: restic_backups_loop.auto_restore is defined
when:
- restic_backups_loop.auto_restore is defined
- restic_backups_loop


# - name: auto_restore stat
# debug:
# msg: "auto_restore stat results {{ restic_restore_state_file }}"
# when: restic_restore_state_file is defined

- name: "run restore if {{ restic_auto_restore_dir }}/{{ restic_backups_loop.name }}.state and auto restore is set"
shell: "{{ restic_script_dir }}/restore-{{ restic_backups_loop.name }}.sh && touch {{ restic_auto_restore_dir }}/{{ restic_backups_loop.name }}.state"
when:
- restic_backups_loop.auto_restore is defined
- not restic_restore_state_file.stat.exists
23 changes: 23 additions & 0 deletions templates/restic_restore_Linux.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# {{ ansible_managed }}
# Restore script for {{ item.src|default('stdin') }}
# Use this file to restore a latest Backup with one execution.

export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }}
export RESTIC_PASSWORD={{ restic_repos[item.repo].password }}
BACKUP_NAME={{ item.name }}
{% if restic_repos[item.repo].aws_access_key is defined %}
export AWS_ACCESS_KEY_ID={{ restic_repos[item.repo].aws_access_key }}
{% endif %}
{% if restic_repos[item.repo].aws_secret_access_key is defined %}
export AWS_SECRET_ACCESS_KEY={{ restic_repos[item.repo].aws_secret_access_key }}
{% endif %}
{% if restic_repos[item.repo].aws_default_region is defined %}
export AWS_DEFAULT_REGION={{ restic_repos[item.repo].aws_default_region }}
{% endif %}
{% if item.src is defined %}
BACKUP_SOURCE={{ item.src }}
{% endif %}
set -euxo pipefail

restic restore latest --target /
1 change: 1 addition & 0 deletions vars/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _platform_map:
restic_create_paths:
- '{{ restic_download_path }}/bin'
- '{{ restic_script_dir }}'
- '{{ restic_auto_restore_dir }}'

restic_bin_bath: '{{ restic_download_path }}/bin/restic-{{ restic_version }}'

Expand Down