From b3934b0d6e7ff0ffdb94a19613f14a8e93d8ed32 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 16 Jul 2024 11:04:25 +0200 Subject: [PATCH] backup_added --- Enter your MFA token: | 1 - backupubu.yml | 134 +++++++++++++++++++++++++++++++++++++++++ vault.yml | 60 +++++++++--------- 3 files changed, 165 insertions(+), 30 deletions(-) delete mode 100644 Enter your MFA token: create mode 100644 backupubu.yml diff --git a/Enter your MFA token: b/Enter your MFA token: deleted file mode 100644 index d5416b4..0000000 --- a/Enter your MFA token: +++ /dev/null @@ -1 +0,0 @@ -MOuARJXBfnkW,GN0o,aD diff --git a/backupubu.yml b/backupubu.yml new file mode 100644 index 0000000..38b62f5 --- /dev/null +++ b/backupubu.yml @@ -0,0 +1,134 @@ +--- +- name: Install backup software and configure backup to Synology NAS + hosts: ubugpu.localdomain + become: false + vars_files: + - vault.yml + + vars: + backup_dir: "/home" + nas_target_base: "/volume1/Backup" + cron_time: "0 17 * * 0" # This represents every Sunday at 17:00 + hostname: "{{ ansible_hostname }}" + cron_name: "Wake NAS and Backup to Synology NAS" + ssh_key_paths: + - "/home/alex/.ssh/id_ed25519.pub" + - "/home/alex/.ssh/id_rsa.pub" + - "/home/alex/.ssh/id_ecdsa.pub" + - "/home/alex/.ssh/id_dsa.pub" + + tasks: + - name: Update apt cache + apt: + update_cache: yes + become: yes + + - name: Ensure rsync is installed + apt: + name: rsync + state: present + become: yes + + - name: Ensure etherwake is installed + apt: + name: etherwake + state: present + become: yes + + - name: Find network interface with 192.168.1.x IP address + shell: ip -o -4 addr list | grep '192.168.1.' | awk '{print $2}' + register: network_interface + + - name: Set network_interface fact + set_fact: + network_interface: "{{ network_interface.stdout }}" + + - name: Find existing SSH public key + stat: + path: "{{ item }}" + with_items: "{{ ssh_key_paths }}" + register: ssh_key_check + + - name: Set SSH key path + set_fact: + ssh_key_path: "{{ ssh_key_check.results | selectattr('stat.exists', 'equalto', true) | map(attribute='item') | first }}" + when: ssh_key_check.results | selectattr('stat.exists', 'equalto', true) | length > 0 + + - name: Debug SSH key path + debug: + msg: "SSH key path: {{ ssh_key_path }}" + + - name: Read the SSH public key + slurp: + src: "{{ ssh_key_path }}" + register: ssh_pub_key + when: ssh_key_path is defined + + - name: Ensure .ssh directory exists on the NAS + command: "ssh alex@alex-ds414.localdomain 'mkdir -p ~/.ssh && chmod 700 ~/.ssh'" + ignore_errors: yes + + - name: Add the SSH public key to the NAS authorized_keys + command: "ssh alex@alex-ds414.localdomain 'echo {{ ssh_pub_key.content | b64decode }} >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'" + when: ssh_key_path is defined + + - name: Show debug info for rsync_user and rsync_password + debug: + msg: "rsync_user: {{ rsync_user }}, rsync_password: {{ rsync_password }}" + + - name: Wake up the NAS + command: sudo etherwake -i {{ network_interface }} 00:11:32:43:FA:11 + async: 0 + poll: 0 + become: yes + + - name: Wait for 1 minute to allow NAS to start up initially + wait_for: + timeout: 60 + + - name: Check if NAS is reachable via SSH + command: "ssh -o BatchMode=yes -o ConnectTimeout=5 alex@alex-ds414.localdomain echo 'SSH connection successful'" + register: nas_ping_result + ignore_errors: yes + + - name: Fail if the NAS is not reachable via SSH + fail: + msg: "NAS is not reachable via SSH. Exiting." + when: nas_ping_result.rc != 0 + + - name: Perform the test rsync + command: rsync -av --dry-run --delete {{ backup_dir }}/ alex@alex-ds414.localdomain:{{ nas_target_base }}/{{ hostname }}/ + register: rsync_test + ignore_errors: yes + failed_when: rsync_test.rc not in [0, 23] + + - name: Fail if the rsync test fails for reasons other than permission issues + fail: + msg: "rsync test failed for reasons other than permission issues. Exiting." + when: rsync_test.rc not in [0, 23] + + - name: Perform the backup using rsync + command: rsync -av --delete {{ backup_dir }}/ alex@alex-ds414.localdomain:{{ nas_target_base }}/{{ hostname }}/ + when: rsync_test.rc in [0, 23] + ignore_errors: yes + failed_when: rsync_test.rc not in [0, 23] + + - name: Check if cron job exists + cron: + name: "{{ cron_name }}" + state: present + register: cron_present + ignore_errors: yes + + - name: Add cron job for backup if not present + cron: + name: "{{ cron_name }}" + minute: "{{ cron_time.split()[0] }}" + hour: "{{ cron_time.split()[1] }}" + day: "{{ cron_time.split()[2] }}" + month: "{{ cron_time.split()[3] }}" + weekday: "{{ cron_time.split()[4] }}" + job: "sudo etherwake -i {{ network_interface }} 00:11:32:43:FA:11 && sleep 300 && rsync -av --delete /home/ alex@alex-ds414.localdomain:{{ nas_target_base }}/{{ hostname }}/" + when: rsync_test.rc in [0, 23] and (cron_present is not defined or cron_present is failed) + become: yes + diff --git a/vault.yml b/vault.yml index e8203ec..261d790 100644 --- a/vault.yml +++ b/vault.yml @@ -1,30 +1,32 @@ $ANSIBLE_VAULT;1.1;AES256 -37396134343831373366656239333433393164656337666531626564343733326662303631653366 -3262353332666436383665323939313866653934663730390a396132383264613434366664616566 -37393530336637313933326565346438356166333534393434343534613531653231313437396431 -6137646361663135340a353335623235343732303330636334646335636261326231363962303565 -66613765666363663164613566353664353033656663326365306335343962393664356263653564 -64316166393164616163663431643832303064323064363037313534333638376330653464343664 -30313335666162363864616466626265353130636561626265353665666234333666393962393937 -63353464333431643834323532353465306339343839373934333732356163356261633265653565 -39393933363365373134663835623063636361376366343765366236316561336362633232333936 -36353264613866373531393335346637386461643337363365326631343433333138366331336564 -39316233323739373233343666303335626265623965313831373436623965366365613363303331 -62616630346136303238623161613938653466393365663530663439353862376232316433643837 -35383931666263633836326530393364373831306238373139316462323163303134653437323564 -39633038376566303161353361663764623662623561663730633363656265333732396539663836 -34386438393837656139643430653166323866353834323662663632643833353333626336656334 -61343532313665353939353139346563636539343964316632353362623334656537623036373631 -64666633653365373139633963373563306335303764396664396663383764666463383436666138 -33643037636430366439666463653339373239383733326432626366363730326631376133353065 -38303331613135613563346131363238333563663231646663343762376263623935663264303661 -38343932366161323132373964373566383036316532383562363365666538386133386638363837 -61633465646561653437653839323962363533333565653434633938333835663162643634323235 -37663031376235646162656538613339303131646631663465396163366237666462353461323730 -62616162393735326137316634353033303937313834313432643561343962366434316363376232 -36626235303630643866636661326532653764656437373736376430303834613861663836323433 -61643933633331316134326666663031656131353232336266656634373639393136666632353235 -39383730633634343561386436356636303339633463373463373032323739333338346562653138 -34613763303538326637383239383638386632343730633934383365613034316631393763646137 -36343561303639666663313233383436373464643233336262633437623266323263333062323732 -3632 +62336234333739646236656537663162393235393436313563366531366238356633346535386263 +6130616535396334393662653862653230363538623538630a373135666365386530356539353462 +61333065643432313337383838396264336638393437336439333836393335383732626237313332 +6233353032373034640a663964386163393933633861653633363262313239366365333031353361 +39613039326637636135323965666530643832313637386239346235626233373564363236306334 +62306463623366626562616634346134383839653035353636306237336438623538373362306565 +63623463386566616133353830303065646139373831313539663764666364393535303063376431 +65393634386138326538653834663161323638653130323035643162323732653730643261353864 +64626539323765386636383665306431353239623766643266396231306566366231363761373132 +32356139383964323864303166363964663532373263316164653932306235373137333061613562 +38316564363930346264626432373766366462336362626531396630373838353165346234383339 +64373264313737316231323034366231323963313439613665323261393336396530346335333830 +31343161343330656364333763316139353561616336343539363338616633666466656162666230 +32316537633333353865666664346330646561343133653939366230323730303364633266323231 +32643666626335353535376338306138613063383762656330396362356339636366383762383638 +30653832376464353630306332653062616262613537386261623933303132396265373961383439 +38356639353335643032653664373131343535646133306136366132363761346535613035313864 +35663263383437343565643835313763636538353037323734353162303064383932633463383233 +34386239666434653133396361623532323739333263353461346231656131653230343632326266 +37646161643836333565616535363762356637396365343831326631646566333363643537666438 +30363933313839326365653139336630653132333831643931356661663631613863626161383864 +65386666313939393439343230353639623530333566613366373963666438366362346433663261 +64633638306638623966653930623166353136643062393031646533303734616666383333636432 +32626464316332666531396461353135336230376663656432343636323732653632633139333336 +63313762666536623038373339356462613839366332343034663736633432653336613531366138 +30643837366365666235353966396562313531613739646262333165353237616630623166366532 +31666564623331326261643535383531363365303766643765373266363338356466343331363462 +30633733663239616434393130326533663837633235383666336432643962323032623535663763 +61663133636465323666306133363830383339376563386333303964336663613130376130636239 +36356366333331373532373563303261376535626534353535613963353530646137323430356439 +386431623137383431633133336266316335