Removed old local binary restic setup, made setup-lxc use other vault files.
This commit is contained in:
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"ansible.python.interpreterPath": "/bin/python3",
|
"ansible.python.interpreterPath": "/bin/python3",
|
||||||
"ansibleVaultInline.keyfile": "~/.vault_pass",
|
"ansibleVaultInline.keyfile": "~/.vault_pass",
|
||||||
|
"yaml.customTags": ["!vault scalar"],
|
||||||
}
|
}
|
||||||
@@ -4,13 +4,6 @@
|
|||||||
become: true
|
become: true
|
||||||
gather_facts: true
|
gather_facts: true
|
||||||
|
|
||||||
vars_files:
|
|
||||||
- ./vault.yaml
|
|
||||||
|
|
||||||
vars:
|
|
||||||
restic_repo: "s3:http://misamisa.duckdns.org:9000/resticbackup"
|
|
||||||
restore_target_path: "/"
|
|
||||||
# restic_password_value: "Stored in vault"
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Upgrade-packages.yml - update packages
|
- name: Upgrade-packages.yml - update packages
|
||||||
@@ -25,21 +18,25 @@
|
|||||||
- name: Setup-restic.yml - setup restic
|
- name: Setup-restic.yml - setup restic
|
||||||
ansible.builtin.import_tasks: ./setup-restic.yml
|
ansible.builtin.import_tasks: ./setup-restic.yml
|
||||||
|
|
||||||
- name: Check if app data directory exists
|
- name: Check if /apps/docker exists
|
||||||
stat:
|
ansible.builtin.stat:
|
||||||
path: "/apps/"
|
path: "/apps/docker"
|
||||||
register: app_data
|
register: apps_docker_dir
|
||||||
|
|
||||||
- name: Restic restore app data Dir
|
- name: Check if /apps/data exists
|
||||||
ansible.builtin.shell:
|
ansible.builtin.stat:
|
||||||
cmd: "restic restore latest --tag {{ ansible_hostname }} --target {{ restore_target_path }}"
|
path: "/apps/data"
|
||||||
environment:
|
register: apps_data_dir
|
||||||
RESTIC_REPOSITORY: "{{ restic_repo }}"
|
|
||||||
RESTIC_PASSWORD: "{{ restic_password_value }}"
|
- name: Run restore-backups.yaml runbook if either dir is missing
|
||||||
AWS_ACCESS_KEY_ID: "{{ aws_access_key_id }}"
|
ansible.builtin.command:
|
||||||
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_access_key }}"
|
cmd: >-
|
||||||
args:
|
ansible-playbook ../restore-backups.yaml
|
||||||
creates: "/apps/data"
|
-i {{ inventory_file }}
|
||||||
|
--limit {{ inventory_hostname }}
|
||||||
|
-e confirm_restore=true
|
||||||
|
delegate_to: localhost
|
||||||
|
when: not apps_docker_dir.stat.exists or not apps_data_dir.stat.exists
|
||||||
|
|
||||||
when: not app_data.stat.exists or not app_data.stat.isdir
|
when: not app_data.stat.exists or not app_data.stat.isdir
|
||||||
|
|
||||||
|
|||||||
@@ -30,213 +30,3 @@
|
|||||||
group: users
|
group: users
|
||||||
mode: '0600'
|
mode: '0600'
|
||||||
|
|
||||||
- name: Copy restic.sh
|
|
||||||
copy:
|
|
||||||
dest: /home/restic/restic.sh
|
|
||||||
content: |
|
|
||||||
#!/bin/bash
|
|
||||||
cd /home/restic/
|
|
||||||
# Check if a flag was passed
|
|
||||||
if [[ "$#" -lt 2 ]]; then
|
|
||||||
echo "Usage: $0 [--backup] [--forget] [--check] [--init] dirtobackup"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Initialize variables
|
|
||||||
backup=false
|
|
||||||
forget=false
|
|
||||||
check=false
|
|
||||||
init=false
|
|
||||||
password=($cat ./.resticpassword)
|
|
||||||
|
|
||||||
last_arg=""
|
|
||||||
|
|
||||||
# Parse arguments
|
|
||||||
case $1 in
|
|
||||||
--backup)
|
|
||||||
backup=true
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--forget)
|
|
||||||
forget=true
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--check)
|
|
||||||
check=true
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--init)
|
|
||||||
init=true
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Usage: $0 [--backup] [--forget] [--check] [--init] dirtobackup"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Check if last argument is valid
|
|
||||||
last_arg=$1
|
|
||||||
if [ -n "$last_arg" ]; then
|
|
||||||
if [ -d "$last_arg" ]; then
|
|
||||||
#Last argument is a directory: $last_arg
|
|
||||||
backupdir=$last_arg
|
|
||||||
elif [ -f "$last_arg" ]; then
|
|
||||||
#Last argument is a file: $last_arg
|
|
||||||
backupdir=$last_arg
|
|
||||||
else
|
|
||||||
echo "Last argument is neither a directory nor a file: $last_arg"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "No valid argument provided after options"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# Execute Restic commands based on flags
|
|
||||||
if $backup; then
|
|
||||||
echo "Backing up... to misamisa"
|
|
||||||
echo "Date: $(date '+%Y-%m-%d_%H-%M-%S')" # Add your Restic backup command here
|
|
||||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) backup --exclude="*lost+found*" $backupdir \
|
|
||||||
| tee backup.out
|
|
||||||
status=$?
|
|
||||||
if [ $status -eq 0 ]; then
|
|
||||||
# If the exit status is 0 (success), send a success message
|
|
||||||
./discord.sh "$(hostname) backup complete"
|
|
||||||
elif [ $status -eq 3 ]; then
|
|
||||||
./discord.sh "$(hostname) backup complete"
|
|
||||||
else
|
|
||||||
# If the exit status is not 0 (failure), send a failure message
|
|
||||||
./discord.sh "$(hostname) Backup has failed"
|
|
||||||
fi
|
|
||||||
echo "Backup completed $(date '+%Y-%m-%d_%H-%M-%S')"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if $forget; then
|
|
||||||
echo "Removing old backups..."
|
|
||||||
# Add your Restic forget command here
|
|
||||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) forget --keep-within-daily 7d --keep-within-weekly 1m --keep-within-monthly 1y
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
# If the exit status is 0 (success), send a success message
|
|
||||||
./discord.sh "$(hostname) forget command completed successfully"
|
|
||||||
else
|
|
||||||
# If the exit status is not 0 (failure), send a failure message
|
|
||||||
./discord.sh "$(hostname) forget command has failed"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if $check; then
|
|
||||||
echo "Checking backups..."
|
|
||||||
# Add your Restic check command here
|
|
||||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) check --read-data
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
# If the exit status is 0 (success), send a success message
|
|
||||||
./discord.sh "$(hostname) Restic Verification complete"
|
|
||||||
else
|
|
||||||
# If the exit status is not 0 (failure), send a failure message
|
|
||||||
./discord.sh "$(hostname) Restic Verification failed!! there is an issue"
|
|
||||||
fi
|
|
||||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) unlock
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
if $init; then
|
|
||||||
echo "Init backup..."
|
|
||||||
# Generate password
|
|
||||||
if [[ -z $(grep '[^[:space:]]' ./.resticpassword) ]] ; then
|
|
||||||
echo "Password file empty. generating passwordwq"
|
|
||||||
tr -dc A-Za-z0-9 </dev/urandom | head -c 25 > ./.resticpassword
|
|
||||||
fi
|
|
||||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) init
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
# If the exit status is 0 (success), send a success message
|
|
||||||
./discord.sh "$(hostname) Restic Init complete for $(hostname)"
|
|
||||||
else
|
|
||||||
# If the exit status is not 0 (failure), send a failure message
|
|
||||||
./discord.sh "$(hostname) Restic init failed!! there is an issue on $(hostname)"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
owner: restic
|
|
||||||
group: users
|
|
||||||
mode: '0755'
|
|
||||||
|
|
||||||
- name: Copy discord.sh
|
|
||||||
copy:
|
|
||||||
dest: /home/restic/discord.sh
|
|
||||||
content: "{{ discord_webhook }}"
|
|
||||||
owner: restic
|
|
||||||
group: users
|
|
||||||
mode: '0755'
|
|
||||||
|
|
||||||
- name: Ensure known_hosts contains misamisa.duckdns.org on port 25456
|
|
||||||
become: yes
|
|
||||||
vars:
|
|
||||||
host: "misamisa.duckdns.org"
|
|
||||||
port: 25456
|
|
||||||
user_dir: "/root" # change if the playbook runs as a different user
|
|
||||||
|
|
||||||
block:
|
|
||||||
|
|
||||||
- name: Try to fetch key
|
|
||||||
shell: >
|
|
||||||
ssh-keyscan -p {{ port }} -H {{ host }}
|
|
||||||
>> {{ user_dir }}/.ssh/known_hosts
|
|
||||||
register: scan_result
|
|
||||||
ignore_errors: yes # we’ll decide later if it’s a real failure
|
|
||||||
|
|
||||||
- name: Verify the key is now in known_hosts
|
|
||||||
shell: grep -Fq "{{ host }}" {{ user_dir }}/.ssh/known_hosts
|
|
||||||
register: verify_result
|
|
||||||
changed_when: False
|
|
||||||
failed_when: verify_result.rc != 0
|
|
||||||
|
|
||||||
rescue:
|
|
||||||
# If we get here, either ssh-keyscan failed or the key wasn’t added.
|
|
||||||
# Retry a few times before giving up.
|
|
||||||
- name: Retry fetching the key until it appears
|
|
||||||
shell: >
|
|
||||||
ssh-keyscan -p {{ port }} -H {{ host }}
|
|
||||||
>> {{ user_dir }}/.ssh/known_hosts
|
|
||||||
register: retry_result
|
|
||||||
retries: 5
|
|
||||||
delay: 30
|
|
||||||
until: retry_result.rc == 0 and
|
|
||||||
(shell("grep -Fq '{{ host }}' {{ user_dir }}/.ssh/known_hosts; echo $?") | int) == 0
|
|
||||||
|
|
||||||
- name: Final check – fail if key still missing
|
|
||||||
shell: grep -Fq "{{ host }}" {{ user_dir }}/.ssh/known_hosts
|
|
||||||
register: final_check
|
|
||||||
changed_when: False
|
|
||||||
failed_when: final_check.rc != 0
|
|
||||||
|
|
||||||
|
|
||||||
- name: Download restic bz2 file
|
|
||||||
get_url:
|
|
||||||
url: "https://github.com/restic/restic/releases/download/v0.18.1/restic_0.18.1_linux_amd64.bz2"
|
|
||||||
dest: "/tmp/restic.bz2"
|
|
||||||
mode: '0755'
|
|
||||||
force: yes
|
|
||||||
register: restic_download
|
|
||||||
|
|
||||||
- name: Extract restic bz2 file
|
|
||||||
ansible.builtin.command:
|
|
||||||
cmd: bunzip2 /tmp/restic.bz2
|
|
||||||
creates: /tmp/restic
|
|
||||||
when: restic_download.changed
|
|
||||||
|
|
||||||
- name: Move restic to /usr/bin/
|
|
||||||
copy:
|
|
||||||
src: "/tmp/restic"
|
|
||||||
dest: "/usr/bin/restic"
|
|
||||||
mode: "0755"
|
|
||||||
owner: root
|
|
||||||
group: root
|
|
||||||
remote_src: yes
|
|
||||||
when: restic_download.changed
|
|
||||||
|
|
||||||
# This can be removed later if needed. used to cleanup existing crontab entries. Leaving incase any older
|
|
||||||
# configured hosts are brought online.
|
|
||||||
- name: Remove crontab entries containing restic.sh for root
|
|
||||||
become: yes
|
|
||||||
shell: crontab -l -u root | grep -v restic.sh | crontab -u root -
|
|
||||||
register: cron_output
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
@@ -6,31 +6,24 @@
|
|||||||
|
|
||||||
# Same source/location mapping as run-backups.yaml, kept in sync intentionally.
|
# Same source/location mapping as run-backups.yaml, kept in sync intentionally.
|
||||||
vars:
|
vars:
|
||||||
# Set to false (default) to require an explicit opt-in before anything destructive happens.
|
|
||||||
# Run with: ansible-playbook restore-backups.yaml -e "confirm_restore=true"
|
|
||||||
confirm_restore: false
|
confirm_restore: false
|
||||||
|
|
||||||
# Defaults to "latest" per host+tag. Override with -e "restore_snapshot_id=abcd1234"
|
# Defaults to "latest" per host+tag. Override with -e "restore_snapshot_id=abcd1234"
|
||||||
# to restore a specific snapshot instead.
|
# to restore a specific snapshot instead. Applied to every target in the loop.
|
||||||
restore_snapshot_id: "latest"
|
restore_snapshot_id: "latest"
|
||||||
|
|
||||||
backup_source_label: >-
|
group_paths:
|
||||||
{%- if 'app_data' in group_names -%}
|
app_data:
|
||||||
apps_data
|
label: apps_data
|
||||||
{%- elif 'app_docker' in group_names -%}
|
path: /apps/data/
|
||||||
apps_docker
|
app_docker:
|
||||||
{%- elif 'home_grail' in group_names -%}
|
label: apps_docker
|
||||||
home_grail
|
path: /apps/docker/
|
||||||
{%- endif -%}
|
home_grail:
|
||||||
|
label: home_grail
|
||||||
|
path: /home/grail/
|
||||||
|
|
||||||
backup_location: >-
|
restore_targets: "{{ group_paths | dict2items | selectattr('key', 'in', group_names) | map(attribute='value') | list }}"
|
||||||
{%- if 'app_data' in group_names -%}
|
|
||||||
/apps/data/
|
|
||||||
{%- elif 'app_docker' in group_names -%}
|
|
||||||
/apps/docker/
|
|
||||||
{%- elif 'home_grail' in group_names -%}
|
|
||||||
/home/grail/
|
|
||||||
{%- endif -%}
|
|
||||||
|
|
||||||
restic_env: &restic_env
|
restic_env: &restic_env
|
||||||
RESTIC_REPOSITORY: "s3:http://misamisa.duckdns.org:9000/resticbackup"
|
RESTIC_REPOSITORY: "s3:http://misamisa.duckdns.org:9000/resticbackup"
|
||||||
@@ -44,23 +37,22 @@
|
|||||||
that:
|
that:
|
||||||
- confirm_restore | bool
|
- confirm_restore | bool
|
||||||
fail_msg: >-
|
fail_msg: >-
|
||||||
This playbook will overwrite {{ backup_location }} on {{ inventory_hostname }}
|
This playbook will overwrite backup_targets paths on {{ inventory_hostname }}
|
||||||
with data from a restic snapshot. Re-run with -e "confirm_restore=true" to proceed,
|
with data from restic snapshots. Re-run with -e "confirm_restore=true" to proceed,
|
||||||
optionally with -e "restore_snapshot_id=<id>" to pick a snapshot other than latest.
|
optionally with -e "restore_snapshot_id=<id>" to pick a snapshot other than latest.
|
||||||
success_msg: "Confirmed. Proceeding with restore on {{ inventory_hostname }}."
|
success_msg: "Confirmed. Proceeding with restore on {{ inventory_hostname }}."
|
||||||
|
|
||||||
- name: Fail fast if backup_location/backup_source_label could not be resolved
|
- name: Fail fast if this host matches no known group
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that:
|
that:
|
||||||
- backup_location | trim | length > 0
|
- restore_targets | length > 0
|
||||||
- backup_source_label | trim | length > 0
|
|
||||||
fail_msg: >-
|
fail_msg: >-
|
||||||
Host {{ inventory_hostname }} did not match any known inventory group
|
Host {{ inventory_hostname }} is not in any group listed in group_paths
|
||||||
(app_data, app_docker, home_grail). Check group membership.
|
(app_data, app_docker, home_grail). Check group membership.
|
||||||
|
|
||||||
- name: List available snapshots for this host/tag (for the log)
|
- name: List available snapshots (per target, for the log)
|
||||||
community.docker.docker_container:
|
community.docker.docker_container:
|
||||||
name: restic_snapshots_list
|
name: "restic_snapshots_list_{{ item.label }}"
|
||||||
image: restic/restic:latest
|
image: restic/restic:latest
|
||||||
state: started
|
state: started
|
||||||
detach: false
|
detach: false
|
||||||
@@ -69,13 +61,16 @@
|
|||||||
command: >-
|
command: >-
|
||||||
snapshots
|
snapshots
|
||||||
--host "{{ inventory_hostname }}"
|
--host "{{ inventory_hostname }}"
|
||||||
--tag "{{ backup_source_label }}"
|
--tag "{{ item.label }}"
|
||||||
env: *restic_env
|
env: *restic_env
|
||||||
register: restic_snapshots_result
|
register: restic_snapshots_result
|
||||||
|
loop: "{{ restore_targets }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.label }}"
|
||||||
|
|
||||||
- name: Show available snapshots
|
- name: Show available snapshots
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
var: restic_snapshots_result.container.Output
|
var: restic_snapshots_result.results
|
||||||
|
|
||||||
- name: Restore Sequence with Safety Catch
|
- name: Restore Sequence with Safety Catch
|
||||||
block:
|
block:
|
||||||
@@ -86,15 +81,18 @@
|
|||||||
|
|
||||||
- name: Ensure restore target exists with correct ownership before mounting
|
- name: Ensure restore target exists with correct ownership before mounting
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
path: "{{ backup_location }}"
|
path: "{{ item.path }}"
|
||||||
state: directory
|
state: directory
|
||||||
owner: docker
|
owner: docker
|
||||||
group: docker
|
group: docker
|
||||||
mode: "0755"
|
mode: "0755"
|
||||||
|
loop: "{{ restore_targets }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.label }}"
|
||||||
|
|
||||||
- name: Check for and remove stale locks
|
- name: Check for and remove stale locks (per target)
|
||||||
community.docker.docker_container:
|
community.docker.docker_container:
|
||||||
name: restic_unlock
|
name: "restic_unlock_{{ item.label }}"
|
||||||
image: restic/restic:latest
|
image: restic/restic:latest
|
||||||
state: started
|
state: started
|
||||||
detach: false
|
detach: false
|
||||||
@@ -103,10 +101,13 @@
|
|||||||
env: *restic_env
|
env: *restic_env
|
||||||
register: restic_unlock_result
|
register: restic_unlock_result
|
||||||
failed_when: restic_unlock_result.status != 0
|
failed_when: restic_unlock_result.status != 0
|
||||||
|
loop: "{{ restore_targets }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.label }}"
|
||||||
|
|
||||||
- name: Restore latest snapshot into place
|
- name: Restore latest snapshot into place (per target)
|
||||||
community.docker.docker_container:
|
community.docker.docker_container:
|
||||||
name: restic_restore
|
name: "restic_restore_{{ item.label }}"
|
||||||
image: restic/restic:latest
|
image: restic/restic:latest
|
||||||
state: started
|
state: started
|
||||||
detach: false
|
detach: false
|
||||||
@@ -116,18 +117,21 @@
|
|||||||
command: >-
|
command: >-
|
||||||
restore {{ restore_snapshot_id }}
|
restore {{ restore_snapshot_id }}
|
||||||
--host "{{ inventory_hostname }}"
|
--host "{{ inventory_hostname }}"
|
||||||
--tag "{{ backup_source_label }}"
|
--tag "{{ item.label }}"
|
||||||
--target /
|
--target /
|
||||||
volumes:
|
volumes:
|
||||||
# NOTE: rw (no :ro) here — this is the whole point of a restore.
|
# NOTE: rw (no :ro) here — this is the whole point of a restore.
|
||||||
- "{{ backup_location }}:/backup_target/{{ backup_source_label }}"
|
- "{{ item.path }}:/backup_target/{{ item.label }}"
|
||||||
env: *restic_env
|
env: *restic_env
|
||||||
register: restic_restore_result
|
register: restic_restore_result
|
||||||
failed_when: restic_restore_result.status != 0
|
failed_when: restic_restore_result.status != 0
|
||||||
|
loop: "{{ restore_targets }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.label }}"
|
||||||
|
|
||||||
- name: Show restore output
|
- name: Show restore output
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
var: restic_restore_result.container.Output
|
var: restic_restore_result.results
|
||||||
always:
|
always:
|
||||||
- name: Ensure the application is started back up
|
- name: Ensure the application is started back up
|
||||||
ansible.builtin.systemd:
|
ansible.builtin.systemd:
|
||||||
|
|||||||
Reference in New Issue
Block a user