Compare commits
No commits in common. "b509ca1dffff1330ef619f43c3fcc4501f5e9057" and "0cb692783104055478fcaf969e81c2d92c2eb159" have entirely different histories.
b509ca1dff
...
0cb6927831
37
README.md
37
README.md
@ -1,5 +1,8 @@
|
|||||||
Sets up backups to Cloudflare R2
|
Sets up backups to Cloudflare R2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Instalation
|
# Instalation
|
||||||
|
|
||||||
In requirements.yml:
|
In requirements.yml:
|
||||||
@ -11,38 +14,8 @@ roles:
|
|||||||
name: setup-backups
|
name: setup-backups
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
|
|
||||||
Create a Cloudflare R2 account and create an admin token.
|
Create a Cloudflare R2 account and create an admin token.
|
||||||
|
|
||||||
Create a file under `templates/backup-scripts/<hostname>.sh.j2` that
|
|
||||||
is a shell script that prepares the data for backup - leave empty if
|
|
||||||
not necessary.
|
|
||||||
|
|
||||||
Then in your playbook run:
|
|
||||||
|
|
||||||
```yml
|
|
||||||
|
|
||||||
- name: "Setup backups"
|
|
||||||
hosts: ansible1
|
|
||||||
roles:
|
|
||||||
- setup-backups
|
|
||||||
vars:
|
|
||||||
backup_bucket_prefix: "backup"
|
|
||||||
extra_backup_paths: # most sealcode-roles automatically register their backup dirs in the /backup-dirs txt file, but we can add anything not covered by that here
|
|
||||||
- /var/homebox/data
|
|
||||||
cloudflare_r2_access_key: "cloudflare R2 Access key (for S3-type API)"
|
|
||||||
cloudflare_r2_secret_key: "cloudflare R2 secret key (for S3-type API)"
|
|
||||||
cloudflare_r2_endpoint: https://some_endpoint.r2.cloudflarestorage.com
|
|
||||||
BACKUP_PASSWORD: password
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
If you are implementing a role that has to register a directory for
|
|
||||||
automatic backups, add a task in your role's `tasks/backup.yml`:
|
|
||||||
|
|
||||||
```yml
|
|
||||||
- ansible.builtin.lineinfile:
|
|
||||||
path: "/backup-dirs"
|
|
||||||
line: "{{juice_sqlite_path}}"
|
|
||||||
```
|
|
||||||
|
|||||||
@ -1,2 +1 @@
|
|||||||
backup_bucket_prefix: backup
|
backup_bucket_prefix: backup
|
||||||
extra_backup_paths: []
|
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
## The playbook first runs the 'backup.yml' from each role
|
|
||||||
- name: create the /backup-dirs file
|
|
||||||
file:
|
|
||||||
path: "/backup-dirs"
|
|
||||||
state: "touch"
|
|
||||||
mode: "0400"
|
|
||||||
changed_when: "true" # we always want ti give roles a chance to register dirs
|
|
||||||
|
|
||||||
- name: Ensure file contains lines from array
|
|
||||||
lineinfile:
|
|
||||||
path: /backup-dirs
|
|
||||||
line: "{{ item }}"
|
|
||||||
create: yes
|
|
||||||
state: present
|
|
||||||
loop: "{{ extra_backup_paths }}"
|
|
||||||
@ -1,6 +1,14 @@
|
|||||||
- debug:
|
- debug:
|
||||||
var: group_names
|
var: group_names
|
||||||
|
|
||||||
|
- set_fact:
|
||||||
|
all_backup_paths: "{{ all_backup_paths | default([]) + (lookup('file', 'inventory/group_vars/' + item + '.yml') | from_yaml | dict2items | selectattr('key', 'equalto', 'backup_paths') | map(attribute='value') | list | first | default([])) }}"
|
||||||
|
loop: "{{ group_names }}"
|
||||||
|
when: all_backup_paths is not defined
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: all_backup_paths
|
||||||
|
|
||||||
- name: make sure restic is installed
|
- name: make sure restic is installed
|
||||||
apt: state=latest pkg=restic
|
apt: state=latest pkg=restic
|
||||||
|
|
||||||
@ -13,7 +21,7 @@
|
|||||||
content: "{{ BACKUP_PASSWORD }}"
|
content: "{{ BACKUP_PASSWORD }}"
|
||||||
mode: "0400"
|
mode: "0400"
|
||||||
|
|
||||||
- name: Install rclone
|
- name: Install boto3 and botocore using apt
|
||||||
become: yes
|
become: yes
|
||||||
apt:
|
apt:
|
||||||
name:
|
name:
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# returns code 0 if backup is necessary, 1 otherwise
|
# returns code 0 if backup is necessary, 1 otherwise
|
||||||
while IFS= read -r file; do
|
|
||||||
if [ ! -e "$file" ]; then
|
eval "$DIRS_TO_BACKUP_STR"
|
||||||
exit 0
|
|
||||||
fi
|
for file in "${DIRS_TO_BACKUP[@]}"; do
|
||||||
done </backup-dirs
|
if [ ! -e "$file" ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
@ -7,7 +7,9 @@ $RESTIC --password-file=$PWD_FILE unlock
|
|||||||
date
|
date
|
||||||
echo "Sending the backup to the destination..."
|
echo "Sending the backup to the destination..."
|
||||||
|
|
||||||
cat /backup-dirs | xargs -d '\n' $RESTIC --password-file=$PWD_FILE backup
|
eval "$DIRS_TO_BACKUP_STR" # turn the string into an array
|
||||||
|
|
||||||
|
$RESTIC --password-file=$PWD_FILE backup "${DIRS_TO_BACKUP[@]}"
|
||||||
|
|
||||||
date
|
date
|
||||||
echo "Pruning the backup on the destination..."
|
echo "Pruning the backup on the destination..."
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
declare -a DIRS_TO_BACKUP=({% for item in all_backup_paths %}"{{ item }}"{% if not loop.last %} {% endif %}{% endfor %})
|
||||||
|
export DIRS_TO_BACKUP_STR=$(declare -p DIRS_TO_BACKUP)
|
||||||
export RESTIC_REPOSITORY="rclone:cloudflare-r2:{{ backup_bucket_prefix }}-{{ inventory_hostname }}"
|
export RESTIC_REPOSITORY="rclone:cloudflare-r2:{{ backup_bucket_prefix }}-{{ inventory_hostname }}"
|
||||||
export PWD_FILE=/backup-pwd
|
export PWD_FILE=/backup-pwd
|
||||||
export RESTIC=/usr/bin/restic
|
export RESTIC=/usr/bin/restic
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user