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
This commit is contained in:
Huang, Sophie (sh879n) 2019-10-02 09:21:28 -05:00
parent b401a2283b
commit f7168620ae
2 changed files with 14 additions and 18 deletions

View File

@ -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

View File

@ -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