From 358aee023954f29f0e022656ffc473bcc305580b Mon Sep 17 00:00:00 2001 From: tolerryan Date: Tue, 28 Jul 2026 01:02:00 +0000 Subject: [PATCH] Removed old local binary restic setup, made setup-lxc use other vault files. --- .vscode/settings.json | 1 + playbooks/setup-lxc.yaml | 39 ++++--- playbooks/setup-restic.yml | 210 ------------------------------------- restore-backups.yml | 78 +++++++------- 4 files changed, 60 insertions(+), 268 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index baff55a..cede486 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "ansible.python.interpreterPath": "/bin/python3", "ansibleVaultInline.keyfile": "~/.vault_pass", + "yaml.customTags": ["!vault scalar"], } \ No newline at end of file diff --git a/playbooks/setup-lxc.yaml b/playbooks/setup-lxc.yaml index dec86c6..7a63e9b 100644 --- a/playbooks/setup-lxc.yaml +++ b/playbooks/setup-lxc.yaml @@ -4,13 +4,6 @@ become: 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: - name: Upgrade-packages.yml - update packages @@ -25,21 +18,25 @@ - name: Setup-restic.yml - setup restic ansible.builtin.import_tasks: ./setup-restic.yml - - name: Check if app data directory exists - stat: - path: "/apps/" - register: app_data + - name: Check if /apps/docker exists + ansible.builtin.stat: + path: "/apps/docker" + register: apps_docker_dir - - name: Restic restore app data Dir - ansible.builtin.shell: - cmd: "restic restore latest --tag {{ ansible_hostname }} --target {{ restore_target_path }}" - environment: - RESTIC_REPOSITORY: "{{ restic_repo }}" - RESTIC_PASSWORD: "{{ restic_password_value }}" - AWS_ACCESS_KEY_ID: "{{ aws_access_key_id }}" - AWS_SECRET_ACCESS_KEY: "{{ aws_secret_access_key }}" - args: - creates: "/apps/data" + - name: Check if /apps/data exists + ansible.builtin.stat: + path: "/apps/data" + register: apps_data_dir + + - name: Run restore-backups.yaml runbook if either dir is missing + ansible.builtin.command: + cmd: >- + ansible-playbook ../restore-backups.yaml + -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 diff --git a/playbooks/setup-restic.yml b/playbooks/setup-restic.yml index 84881a5..9c51bf0 100644 --- a/playbooks/setup-restic.yml +++ b/playbooks/setup-restic.yml @@ -30,213 +30,3 @@ group: users 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 ./.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 \ No newline at end of file diff --git a/restore-backups.yml b/restore-backups.yml index 1f02ba4..d5a4543 100644 --- a/restore-backups.yml +++ b/restore-backups.yml @@ -6,31 +6,24 @@ # Same source/location mapping as run-backups.yaml, kept in sync intentionally. 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 # 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" - backup_source_label: >- - {%- 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 -%} + group_paths: + app_data: + label: apps_data + path: /apps/data/ + app_docker: + label: apps_docker + path: /apps/docker/ + home_grail: + label: home_grail + path: /home/grail/ - backup_location: >- - {%- 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 -%} + restore_targets: "{{ group_paths | dict2items | selectattr('key', 'in', group_names) | map(attribute='value') | list }}" restic_env: &restic_env RESTIC_REPOSITORY: "s3:http://misamisa.duckdns.org:9000/resticbackup" @@ -44,23 +37,22 @@ that: - confirm_restore | bool fail_msg: >- - This playbook will overwrite {{ backup_location }} on {{ inventory_hostname }} - with data from a restic snapshot. Re-run with -e "confirm_restore=true" to proceed, + This playbook will overwrite backup_targets paths on {{ inventory_hostname }} + with data from restic snapshots. Re-run with -e "confirm_restore=true" to proceed, optionally with -e "restore_snapshot_id=" to pick a snapshot other than latest. 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: that: - - backup_location | trim | length > 0 - - backup_source_label | trim | length > 0 + - restore_targets | length > 0 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. - - name: List available snapshots for this host/tag (for the log) + - name: List available snapshots (per target, for the log) community.docker.docker_container: - name: restic_snapshots_list + name: "restic_snapshots_list_{{ item.label }}" image: restic/restic:latest state: started detach: false @@ -69,13 +61,16 @@ command: >- snapshots --host "{{ inventory_hostname }}" - --tag "{{ backup_source_label }}" + --tag "{{ item.label }}" env: *restic_env register: restic_snapshots_result + loop: "{{ restore_targets }}" + loop_control: + label: "{{ item.label }}" - name: Show available snapshots ansible.builtin.debug: - var: restic_snapshots_result.container.Output + var: restic_snapshots_result.results - name: Restore Sequence with Safety Catch block: @@ -86,15 +81,18 @@ - name: Ensure restore target exists with correct ownership before mounting ansible.builtin.file: - path: "{{ backup_location }}" + path: "{{ item.path }}" state: directory owner: docker group: docker 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: - name: restic_unlock + name: "restic_unlock_{{ item.label }}" image: restic/restic:latest state: started detach: false @@ -103,10 +101,13 @@ env: *restic_env register: restic_unlock_result 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: - name: restic_restore + name: "restic_restore_{{ item.label }}" image: restic/restic:latest state: started detach: false @@ -116,18 +117,21 @@ command: >- restore {{ restore_snapshot_id }} --host "{{ inventory_hostname }}" - --tag "{{ backup_source_label }}" + --tag "{{ item.label }}" --target / volumes: # 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 register: restic_restore_result failed_when: restic_restore_result.status != 0 + loop: "{{ restore_targets }}" + loop_control: + label: "{{ item.label }}" - name: Show restore output ansible.builtin.debug: - var: restic_restore_result.container.Output + var: restic_restore_result.results always: - name: Ensure the application is started back up ansible.builtin.systemd: