From c9b044e870d01e4722ebb81a187cef8424e37934 Mon Sep 17 00:00:00 2001 From: cgbautista Date: Wed, 5 Nov 2025 16:43:33 +0100 Subject: [PATCH 1/4] [Feat.] backup script can now expand the files.tar.gz backup in the remote server. Added a new flag to be able to use the same name for that file each time, so only a single backup has to be managed (as the new backup system is used instead) --- DHIS2/backups/README.md | 5 +++- DHIS2/backups/backup_tomcat_instance.sh | 40 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/DHIS2/backups/README.md b/DHIS2/backups/README.md index e4b36fbf..125cdb71 100644 --- a/DHIS2/backups/README.md +++ b/DHIS2/backups/README.md @@ -30,6 +30,9 @@ Options: - `--exclude-db`: Exclude the database dump from the backup. - `--exclude-audit`: Exclude audit table. - `--exclude-files`: Exclude the DHIS2 files from the backup. +- `--reuse-backup-for-files`: The filename for the backup of DHIS2 files will ommit the period, timestamp and custom name to use the same name each time (It will still include the instance name). + +Script now allows the user to configure a remote script to convert the copied files.tar.gz into a folder (not a simple decompress into that folder, but a script that allows to reference previous backup files in order to minimize disk usage) ### Example @@ -46,4 +49,4 @@ bash /path/to/backup_tomcat_instance.sh --name TEST --exclude-db Backup with periodicity, excluding audit table and remote copy: ```bash bash /path/to/backup_tomcat_instance.sh --periodicity day-in-week --format custom --destination hostname.example --exclude-audit -``` \ No newline at end of file +``` diff --git a/DHIS2/backups/backup_tomcat_instance.sh b/DHIS2/backups/backup_tomcat_instance.sh index 315e9f45..153031c4 100644 --- a/DHIS2/backups/backup_tomcat_instance.sh +++ b/DHIS2/backups/backup_tomcat_instance.sh @@ -7,6 +7,9 @@ export db_server=localhost export db_name=dhis2 export dump_dest_path="/path/to/backups" export dump_remote_dest_path="/path/to/backups" +export remote_expand_script="/path/to/script/to/convert/targz/into/folders.sh" +export remote_log="/path/to/logfile/in/remote/server.log" +export dump_remote_dest_folder="/path/to/expand/script/ouput/folder" export db_user="db_user" export db_pass="db_pass" export DHIS2_HOME="/path/to/dhis2_home" @@ -16,6 +19,7 @@ DB_REMOTE_DEST_SERVER="" SKIP_DB=0 SKIP_AUDIT=0 SKIP_FILES=0 +REUSE_BACKUP=0 PERIOD_NAME="" FORMAT="custom" TIMESTAMP="" @@ -80,6 +84,7 @@ usage() { formatted_print "--exclude-db: Exclude the database dump from the backup." formatted_print "--exclude-audit: Exclude the audit table from the backup." formatted_print "--exclude-files: Exclude the DHIS2 files from the backup." + formatted_print "--reuse-backup-for-files: The filename for the backup of DHIS2 files will ommit the period, timestamp and custom name to use the same name each time (It will still include the instance name)." formatted_print "" formatted_print "Example: ./backup_db.sh --periodicity day-in-week --format custom --destination hostname.example" } @@ -175,6 +180,10 @@ process_options() { SKIP_FILES=1 shift ;; + --reuse-backup-for-files) + REUSE_BACKUP=1 + shift + ;; -h | --help) usage exit 0 @@ -264,6 +273,22 @@ backup_db() { fi } +expand_backup_in_remote() { + local files_backup_file=$1 + local files_path="${dump_dest_path}/${files_backup_file}" + local dump_remote_dest_path="${dump_remote_dest_path}/." + local dump_remote_dest_folder="${dump_remote_dest_folder}" + local remote_expand_script="${remote_expand_script}" + local remote_log="${remote_log}" + + if [ -z "$files_backup_file" ]; then + error "No backup files to convert." + return 1 + fi + + ssh ${DB_REMOTE_DEST_SERVER} "${remote_expand_script} -i \"${files_path}\" -o \"${dump_remote_dest_folder}\" 2>&1 | tee -a \"${remote_log}\" " +} + copy_backup_to_remote() { local db_backup_file=$1 files_backup_file=$2 local db_path="${dump_dest_path}/${db_backup_file}" @@ -300,6 +325,12 @@ backup() { backup_file_base="BACKUP-${dhis2_instance}-${PERIOD_NAME}${BACKUP_NAME}" + if [ $REUSE_BACKUP -eq 0 ]; then + backup_file_files_base="$backup_file_base" + else + backup_file_files_base="BACKUP-${dhis2_instance}-SINGLE" + fi + if [ $SKIP_DB -eq 0 ]; then if backup_db "${backup_file_base}"; then success @@ -309,7 +340,7 @@ backup() { fi if [ $SKIP_FILES -eq 0 ]; then - if backup_dhis2_folders "${DHIS2_HOME}" "${backup_file_base}"; then + if backup_dhis2_folders "${DHIS2_HOME}" "${backup_file_files_base}"; then success else fail 3 @@ -319,6 +350,13 @@ backup() { if [[ ! "$DB_REMOTE_DEST_SERVER" == "" ]]; then if copy_backup_to_remote "${DB_BACKUP_FILE}" "${FILES_BACKUP_FILE}"; then success + if [ $SKIP_FILES -eq 0 ]; then + if expand_backup_in_remote "${FILES_BACKUP_FILE}"; then + success + else + fail 4 + fi + fi else fail 2 fi From 8bc87ecf11c4a7c283714b818ef4855e0c0a871b Mon Sep 17 00:00:00 2001 From: cgbautista Date: Thu, 20 Nov 2025 09:47:21 +0100 Subject: [PATCH 2/4] expanding the backup in the remote server is an optional feature --- DHIS2/backups/backup_tomcat_instance.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DHIS2/backups/backup_tomcat_instance.sh b/DHIS2/backups/backup_tomcat_instance.sh index 153031c4..1f827cfd 100644 --- a/DHIS2/backups/backup_tomcat_instance.sh +++ b/DHIS2/backups/backup_tomcat_instance.sh @@ -286,6 +286,11 @@ expand_backup_in_remote() { return 1 fi + if [ -z "$remote_expand_script" ]; then + error "No script configured to expand the backup in the remote server" + return 0 + fi + ssh ${DB_REMOTE_DEST_SERVER} "${remote_expand_script} -i \"${files_path}\" -o \"${dump_remote_dest_folder}\" 2>&1 | tee -a \"${remote_log}\" " } From dc1d8a0ed41186388bac3b14b44ed1e3004b2dac Mon Sep 17 00:00:00 2001 From: cgbautista Date: Thu, 20 Nov 2025 10:03:37 +0100 Subject: [PATCH 3/4] added log line for clarity --- DHIS2/backups/backup_tomcat_instance.sh | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/DHIS2/backups/backup_tomcat_instance.sh b/DHIS2/backups/backup_tomcat_instance.sh index 1f827cfd..4f705d3a 100644 --- a/DHIS2/backups/backup_tomcat_instance.sh +++ b/DHIS2/backups/backup_tomcat_instance.sh @@ -286,10 +286,7 @@ expand_backup_in_remote() { return 1 fi - if [ -z "$remote_expand_script" ]; then - error "No script configured to expand the backup in the remote server" - return 0 - fi + log "expand files backup into ${DB_REMOTE_DEST_SERVER}..." ssh ${DB_REMOTE_DEST_SERVER} "${remote_expand_script} -i \"${files_path}\" -o \"${dump_remote_dest_folder}\" 2>&1 | tee -a \"${remote_log}\" " } @@ -356,10 +353,12 @@ backup() { if copy_backup_to_remote "${DB_BACKUP_FILE}" "${FILES_BACKUP_FILE}"; then success if [ $SKIP_FILES -eq 0 ]; then - if expand_backup_in_remote "${FILES_BACKUP_FILE}"; then - success - else - fail 4 + if [ ! "$remote_expand_script" == "" ]; then + if expand_backup_in_remote "${FILES_BACKUP_FILE}"; then + success + else + fail 4 + fi fi fi else From 1fa4cfa346aa7dddeb05f35596b575ba1571510d Mon Sep 17 00:00:00 2001 From: Carlos Garcia Bautista Date: Wed, 24 Dec 2025 16:02:33 +0100 Subject: [PATCH 4/4] Add pipefail option to SSH command --- DHIS2/backups/backup_tomcat_instance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DHIS2/backups/backup_tomcat_instance.sh b/DHIS2/backups/backup_tomcat_instance.sh index 4f705d3a..19197136 100644 --- a/DHIS2/backups/backup_tomcat_instance.sh +++ b/DHIS2/backups/backup_tomcat_instance.sh @@ -288,7 +288,7 @@ expand_backup_in_remote() { log "expand files backup into ${DB_REMOTE_DEST_SERVER}..." - ssh ${DB_REMOTE_DEST_SERVER} "${remote_expand_script} -i \"${files_path}\" -o \"${dump_remote_dest_folder}\" 2>&1 | tee -a \"${remote_log}\" " + ssh ${DB_REMOTE_DEST_SERVER} "set -o pipefail; ${remote_expand_script} -i \"${files_path}\" -o \"${dump_remote_dest_folder}\" 2>&1 | tee -a \"${remote_log}\" " } copy_backup_to_remote() {