From f7168620ae858daf29120b709a3f5e6e05f2d0ae Mon Sep 17 00:00:00 2001 From: "Huang, Sophie (sh879n)" Date: Wed, 2 Oct 2019 09:21:28 -0500 Subject: [PATCH] Database backup fix This patch set fixes the calculation of how long a database backup has be taken. In the existing code, the time difference is rounded to days, even a second less than 4 days will be rounded to 3 days. This effectively allows archives to be kept for one additional day. The new calculation and comparison is based on seconds. Change-Id: I5547e923538ddb83f409b1e7df936baf664e717a --- mariadb/templates/bin/_backup_mariadb.sh.tpl | 16 +++++++--------- .../templates/bin/_backup_postgresql.sh.tpl | 16 +++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/mariadb/templates/bin/_backup_mariadb.sh.tpl b/mariadb/templates/bin/_backup_mariadb.sh.tpl index cd719d617..eef0b9b8c 100644 --- a/mariadb/templates/bin/_backup_mariadb.sh.tpl +++ b/mariadb/templates/bin/_backup_mariadb.sh.tpl @@ -24,21 +24,19 @@ MYSQL="mysql \ MYSQLDUMP="mysqldump \ --defaults-file=/etc/mysql/admin_user.cnf" -days_difference() { +seconds_difference() { archive_date=$( date --date="$1" +%s ) if [ "$?" -ne 0 ] then - day_delta=0 + second_delta=0 fi current_date=$( date +%s ) - date_delta=$(($current_date-$archive_date)) - if [ "$date_delta" -lt 0 ] + second_delta=$(($current_date-$archive_date)) + if [ "$second_delta" -lt 0 ] then - day_delta=0 - else - day_delta=$(($date_delta/86400)) + second_delta=0 fi - echo $day_delta + echo $second_delta } DBNAME=( $($MYSQL --silent --skip-column-names -e \ @@ -108,7 +106,7 @@ if [ $ARCHIVE_RET -eq 0 ] for archive_file in $(ls -1 $ARCHIVE_DIR/*.gz) do archive_date=$( echo $archive_file | awk -F/ '{print $NF}' | cut -d'.' -f 3) - if [ "$(days_difference $archive_date)" -gt "$MARIADB_BACKUP_DAYS_TO_KEEP" ] + if [ "$(seconds_difference $archive_date)" -gt "$(($MARIADB_BACKUP_DAYS_TO_KEEP*86400))" ] then rm -rf $archive_file fi diff --git a/postgresql/templates/bin/_backup_postgresql.sh.tpl b/postgresql/templates/bin/_backup_postgresql.sh.tpl index 8c9d6e294..9bc3f72ff 100755 --- a/postgresql/templates/bin/_backup_postgresql.sh.tpl +++ b/postgresql/templates/bin/_backup_postgresql.sh.tpl @@ -36,21 +36,19 @@ delete_files() { } #Get the day delta since the archive file backup -days_difference() { +seconds_difference() { archive_date=$( date --date="$1" +%s ) if [ "$?" -ne 0 ] then - day_delta=0 + second_delta=0 fi current_date=$( date +%s ) - date_delta=$(($current_date-$archive_date)) - if [ "$date_delta" -lt 0 ] + second_delta=$(($current_date-$archive_date)) + if [ "$second_delta" -lt 0 ] then - day_delta=0 - else - day_delta=$(($date_delta/86400)) + second_delta=0 fi - echo $day_delta + echo $second_delta } #Create backups directory if it does not exists. @@ -90,7 +88,7 @@ if [ $ARCHIVE_RET -eq 0 ] for archive_file in $(ls -1 $ARCHIVE_DIR/*.gz) do archive_date=$( echo $archive_file | awk -F/ '{print $NF}' | cut -d'.' -f 3) - if [ "$(days_difference $archive_date)" -gt "$POSTGRESQL_BACKUP_DAYS_TO_KEEP" ] + if [ "$(seconds_difference $archive_date)" -gt "$(($POSTGRESQL_BACKUP_DAYS_TO_KEEP*86400))" ] then rm -rf $archive_file fi