From 09e1b71414525027e4606adfc7f024911759964b Mon Sep 17 00:00:00 2001
From: "Al hel md. shahriar zaman" <alhel444@gmail.com>
Date: Mon, 13 Jan 2025 20:41:02 +0600
Subject: [PATCH 1/3] set up 2 more crons

---
 infrastructure/server-setup/backups.yml | 26 +++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/infrastructure/server-setup/backups.yml b/infrastructure/server-setup/backups.yml
index 04ef87f3b..29ad96ac9 100644
--- a/infrastructure/server-setup/backups.yml
+++ b/infrastructure/server-setup/backups.yml
@@ -174,3 +174,29 @@
       when: "'backups' in groups and groups['backups'] | length > 0 and ssh_test.rc != 0"
       tags:
         - backups
+
+    #if the file doesn't transfer properly we will get an error alert, since we are using /var/log/opencrvs-backup.error.log file for hold the log
+    - name: Set a cron for transfarreing the file to the backup server
+      cron:
+        user: '{{ crontab_user }}'
+        name: 'Transfer a file to {{ backup_server_remote_target_directory }}'
+        minute: '55'
+        hour: '23'
+        job: 'touch test.txt && rsync -a --prgress ./test.txt {{ backup_server_user }}@{{ destination_server }}:{{ backup_server_remote_target_directory }}/ 2>> /var/log/opencrvs-backup.error.log'
+        state: present
+
+- hosts: backups
+  become: yes 
+  tasks:
+    - name: 'Set a cron to delete the test file'
+      cron: 
+        user: '{{ crontab_user }}'
+        name: 'Delete the test file from {{ backup_server_remote_target_directory }}'
+        minute: '58'
+        hour: '23'
+         job: |
+           if [ -f {{ backup_server_remote_target_directory }}/test.txt ]; then
+             rm -f {{ backup_server_remote_target_directory }}/test.txt
+           fi
+        state: present
+  
\ No newline at end of file

From 5c917d3e8ffcbb33e4073f9404f4bc3ede096759 Mon Sep 17 00:00:00 2001
From: alsmk <alhel444@gmail.com>
Date: Mon, 13 Jan 2025 22:13:19 +0600
Subject: [PATCH 2/3] Corrected the approach

---
 infrastructure/server-setup/backups.yml | 43 ++++++++++++-------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/infrastructure/server-setup/backups.yml b/infrastructure/server-setup/backups.yml
index 29ad96ac9..23ab3c3c3 100644
--- a/infrastructure/server-setup/backups.yml
+++ b/infrastructure/server-setup/backups.yml
@@ -174,29 +174,26 @@
       when: "'backups' in groups and groups['backups'] | length > 0 and ssh_test.rc != 0"
       tags:
         - backups
+    
+    - name: Create a file to transfer to the backup server
+      shell: touch /tmp/test_file.txt
+      tags:
+        - backups
 
-    #if the file doesn't transfer properly we will get an error alert, since we are using /var/log/opencrvs-backup.error.log file for hold the log
-    - name: Set a cron for transfarreing the file to the backup server
-      cron:
-        user: '{{ crontab_user }}'
-        name: 'Transfer a file to {{ backup_server_remote_target_directory }}'
-        minute: '55'
-        hour: '23'
-        job: 'touch test.txt && rsync -a --prgress ./test.txt {{ backup_server_user }}@{{ destination_server }}:{{ backup_server_remote_target_directory }}/ 2>> /var/log/opencrvs-backup.error.log'
-        state: present
+    - name: Check ability to transfer a file to the backup server
+      shell: rsync -avz -e "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5" /tmp/test_file.txt {{ backup_server_user }}@{{ destination_server }}:{{ backup_server_remote_target_directory }}/test_file.txt
+      remote_user: '{{ crontab_user }}'
+      register: rsync_test
+      when: "'backups' in groups and groups['backups'] | length > 0"
+      ignore_errors: yes
+      tags:
+        - backups
 
-- hosts: backups
-  become: yes 
-  tasks:
-    - name: 'Set a cron to delete the test file'
-      cron: 
-        user: '{{ crontab_user }}'
-        name: 'Delete the test file from {{ backup_server_remote_target_directory }}'
-        minute: '58'
-        hour: '23'
-         job: |
-           if [ -f {{ backup_server_remote_target_directory }}/test.txt ]; then
-             rm -f {{ backup_server_remote_target_directory }}/test.txt
-           fi
-        state: present
+    - name: Fail if rsync test failed
+      fail:
+        msg: 'rsync to the backup server failed'
+      when: "'backups' in groups and groups['backups'] | length > 0 and rsync_test.rc != 0"
+      tags:
+        - backups
+  
   
\ No newline at end of file

From c97eb91da98c6d41bcbdc0f332d4b820b8423408 Mon Sep 17 00:00:00 2001
From: "Al hel md. shahriar zaman" <alhel444@gmail.com>
Date: Tue, 14 Jan 2025 17:34:47 +0600
Subject: [PATCH 3/3] a draft for copying file from backup to staging

---
 infrastructure/server-setup/backups.yml | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/infrastructure/server-setup/backups.yml b/infrastructure/server-setup/backups.yml
index 23ab3c3c3..e723c4937 100644
--- a/infrastructure/server-setup/backups.yml
+++ b/infrastructure/server-setup/backups.yml
@@ -185,15 +185,14 @@
       remote_user: '{{ crontab_user }}'
       register: rsync_test
       when: "'backups' in groups and groups['backups'] | length > 0"
-      ignore_errors: yes
       tags:
         - backups
-
-    - name: Fail if rsync test failed
-      fail:
-        msg: 'rsync to the backup server failed'
-      when: "'backups' in groups and groups['backups'] | length > 0 and rsync_test.rc != 0"
+         
+    - name: Check if file can be transferred to the staging server from backup server
+      shell: ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 {{ backup_server_user }}@{{ destination_server }} 'rsync -avz -e "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5" {{ backup_server_remote_target_directory }}/test_file.txt {{ crontab_user }}@{{ inventory_hostname }}:/tmp/test_file.txt'
+      remote_user: '{{ crontab_user }}'
+      register: rsync_test_from_backup
+      when: "'backups' in groups and groups['backups'] | length > 0 and backup_restore_encryption_passphrase and periodic_restore_from_backup"
       tags:
         - backups
-  
-  
\ No newline at end of file
+        
\ No newline at end of file