From 871803165a2b44d2e541bc508a15d6e89ede22ec Mon Sep 17 00:00:00 2001 From: tolerryan <105945216+tolerryan@users.noreply.github.com> Date: Thu, 17 Oct 2024 23:32:04 -0500 Subject: [PATCH] removed restic role --- roles/lib/setup-restic.yml | 118 +++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 6 deletions(-) diff --git a/roles/lib/setup-restic.yml b/roles/lib/setup-restic.yml index dab729c..1f3c31f 100644 --- a/roles/lib/setup-restic.yml +++ b/roles/lib/setup-restic.yml @@ -4,8 +4,6 @@ become: yes remote_user: ansible gather_facts: false - roles: - - {role: roles-ansible.restic , tags: restic} vars: restic_create_schedule: true @@ -22,14 +20,122 @@ scheduled: true schedule_oncalendar: '*-*-* 01:00:00' tasks: + - name: Create Restic user + user: + name: restic + uid: 2001 + group: user + state: present + create_home: yes + home: /home/restic + shell: /bin/bash + + - name: Add key for Restic user + ansible.posix.authorized_key: + user: restic + state: present + key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOsDIrV6QIMtpJFNpZEgHnkYgFC6OXMJQFc1JqrnpCzY fake@gmail.com" + + - name: Copy restic.sh + copy: + dest: /home/restic/restic.sh + content: | + #!/bin/bash + + # Check if a flag was passed + if [[ "$#" -lt 1 ]]; then + echo "Usage: $0 [--backup] [--forget] [--check]" + exit 1 + fi + + # Initialize variables + backup=false + forget=false + check=false + password=($cat ./.resticpassword) + # Parse arguments + for arg in "$@" + do + case $arg in + --backup) + backup=true + ;; + --forget) + forget=true + ;; + --check) + check=true + ;; + *) + echo "Unknown argument: $arg" + exit 1 + ;; + esac + done + + # 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 ./ + if [ $? -eq 0 ]; then + # If the exit status is 0 (success), send a success message + ./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 + owner: restic + group: restic + mode: '0644' + + - name: Copy discord.sh + copy: + dest: /heme/restic/discord.sh + content: {{ discord_webhook }}} + owner: restic + group: restic + mode: '0644' + - name: Create SSH config file copy: - dest: /home/docker/.ssh/config + dest: /home/restic/.ssh/config content: | Hostname misamisa.duckdns.org Port 25456 User restic IdentityFile vm - owner: docker - group: docker - mode: '0640' + owner: restic + group: restic + mode: '0644' +