Skip to content

Commit

Permalink
PL-372 Clean up sanitized dumps (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jifeon authored Feb 1, 2019
1 parent c7ab2b1 commit 9f5d8ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
85 changes: 45 additions & 40 deletions backups/src/pg_backup_rotated.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

# see https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux

echo "Running backups..."
log () {
echo "[pg_backup_rotated.sh]> $@"
}

SKIPPING=1
log "Running backups..."

function perform_backups()
{
Expand All @@ -16,24 +18,34 @@ function perform_backups()
backup_file_path="${BACKUP_DIR}${backup_date}${suffix}".backup
backup_roles_file_path="${backup_file_path}_roles.out"

if [ -e "${backup_file_path}" ]; then
echo "${backup_file_path} already exists, skipping dump"
return ${SKIPPING}
if [[ -e ${backup_file_path} ]]; then
log "${backup_file_path} already exists, skipping dump"
return 1
fi

echo -e "\n\nPerforming custom backup for ${DB_NAME} database to ${backup_file_path}"
log "Dumping custom backup for ${DB_NAME} database to ${backup_file_path}"

if ! pg_dump -Fc -h "$db_host" -p "$db_port" -U postgres ${DB_NAME} -f ${backup_file_path}.in_progress; then
echo "[!!ERROR!!] Failed to produce custom backup database ${DB_NAME}"
log "[ERROR] Failed to produce custom backup database ${DB_NAME}"
else
pg_dumpall -r -h "$db_host" -p "$db_port" -U postgres -f ${backup_roles_file_path}.in_progress
cat ${backup_roles_file_path}.in_progress | grep -v ${IGNORE_DUMP_ROLES} > "${backup_roles_file_path}"
mv ${backup_file_path}.in_progress ${backup_file_path}
rm -f ${backup_roles_file_path}.in_progress
echo -e "\nDatabase backup complete!"
log "Database backup complete!"
fi
}

function clean_up() {
local time_to_keep=$1
local suffix=$2

find ${BACKUP_DIR} \
-maxdepth 1 -mtime +${time_to_keep} \
-name "*${suffix}*" \
-exec rm -rf '{}' ';'
}

function dump_database()
{
local db_host=$1
Expand All @@ -42,13 +54,14 @@ function dump_database()
# MONTHLY BACKUPS

local day_of_month=`date +%d`
if [ ${day_of_month} -eq 1 ];
if [[ ${day_of_month} -eq 1 ]];
then
# Delete all expired monthly backups
local suffix="-${db_host}-${db_port}-monthly.backup"
log "Deleting all expired monthly backups"
local suffix="-${db_host}-${db_port}-monthly"
local days_to_keep_monthly_backup=`expr $(( ${MONTHS_TO_KEEP_MONTHLY} * 30 ))`
find ${BACKUP_DIR} -maxdepth 1 -mtime +${days_to_keep_monthly_backup} -name "*${suffix}" -exec rm -rf '{}' ';'
clean_up ${days_to_keep_monthly_backup} ${suffix}

log "Dumping monthly backup for ${db_host}:${db_port}"
perform_backups ${suffix} ${db_host} ${db_port}
fi

Expand All @@ -57,55 +70,47 @@ function dump_database()
local backup_time=`date +%H:%M`

local day_of_week=`date +%u` #1-7 (Monday-Sunday)
if [ ${day_of_week} = ${DAY_OF_WEEK_TO_KEEP} ];
if [[ ${day_of_week} = ${DAY_OF_WEEK_TO_KEEP} ]];
then
# Delete all expired weekly backups
log "Deleting all expired weekly backups"
local days_to_keep_weekly_backups=`expr $(( (${WEEKS_TO_KEEP_WEEKLY} * 7) + 1 ))`
local suffix="-${db_host}-${db_port}-weekly.backup"
find ${BACKUP_DIR} -maxdepth 1 -mtime +${days_to_keep_weekly_backups} -name "*${suffix}" -exec rm -rf '{}' ';'
local suffix="-${db_host}-${db_port}-weekly"
clean_up ${days_to_keep_weekly_backups} ${suffix}

log "Dumping weekly backup for ${db_host}:${db_port}"
perform_backups ${suffix} ${db_host} ${db_port}
fi

# if weekly backups already exists need to try perform hourly backup
backups_result=$?
if [ "$backups_result" -eq ${SKIPPING} ]; then
perform_backups "-${backup_time}-${db_host}-${db_port}-hourly" ${db_host} ${db_port}
fi
# DAILY BACKUPS

return 0;
fi
log "Deleting all expired daily backups"
local suffix="-${db_host}-${db_port}-daily"
clean_up ${DAYS_TO_KEEP_DAILY} ${suffix}

# DAILY AND HOURLY BACKUPS
log "Dumping daily backup for ${db_host}:${db_port}"
perform_backups ${suffix} ${db_host} ${db_port}

# Delete expired daily and hourly backups
find ${BACKUP_DIR} \
-maxdepth 1 -mtime +${DAYS_TO_KEEP_DAILY} \
-name "*-${db_host}-${db_port}-daily.backup" \
-exec rm -rf '{}' ';'
# HOURLY BACKUPS

find ${BACKUP_DIR} \
-maxdepth 1 -mtime +${DAYS_TO_KEEP_HOURLY} \
-name "*-${db_host}-${db_port}-hourly.backup" \
-exec rm -rf '{}' ';'
log "Deleting all expired hourly backups"
suffix="-${db_host}-${db_port}-hourly"
clean_up ${DAYS_TO_KEEP_HOURLY} ${suffix}

perform_backups "-${db_host}-${db_port}-daily" ${db_host} ${db_port}
backups_result=$?
if [ "$backups_result" -eq ${SKIPPING} ]; then
perform_backups "-${backup_time}-${db_host}-${db_port}-hourly" ${db_host} ${db_port}
fi
log "Dumping hourly backup for ${db_host}:${db_port}"
perform_backups "-${backup_time}-${db_host}-${db_port}-hourly" ${db_host} ${db_port}
}

db_number=1
eval "db_config=\$DB_CONFIG_${db_number}"
while [ "${db_config}" ]; do
while [[ ${db_config} ]]; do
if [[ "${db_config}" =~ ^([^:]+):([[:digit:]]+):.*$ ]];
then
DB_HOST=${BASH_REMATCH[1]}
DB_PORT=${BASH_REMATCH[2]}

dump_database ${DB_HOST} ${DB_PORT}
else
echo "Wrong db config ${db_config}, should match this format: 'host:port:db:user:password'";
log "Wrong db config ${db_config}, should match this format: 'host:port:db:user:password'";
exit 1;
fi

Expand Down
2 changes: 1 addition & 1 deletion backups/src/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sleep 5

db_number=1
eval "db_config=\$DB_CONFIG_${db_number}"
while [ "${db_config}" ]; do
while [[ ${db_config} ]]; do
echo ${db_config} >> /root/.pgpass
let "db_number += 1"
eval "db_config=\$DB_CONFIG_${db_number}"
Expand Down
4 changes: 2 additions & 2 deletions publisher/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ backup_file=${backup_date}${DUMP_NAME_SUFFIX}
backup_roles_file=${backup_file}_roles.out

log "Looking for ${backup_file} inside container"
if [ $(docker exec publishing_db_container test -e "/files/${backup_file}" && echo $?) ];
if [[ $(docker exec publishing_db_container test -e "/files/${backup_file}" && echo $?) ]];
then
# we preparing 2 docker images, one contains only needed roles - it's "empty" state of DB
# another one contains data and can be used by developer and tools running tests on real data
Expand Down Expand Up @@ -68,7 +68,7 @@ EORESTORE

echo ""

if [ "$db_initialized" -eq 0 ]
if [[ "$db_initialized" -eq 0 ]]
then
log "Sanitizing DB was not initialized after 1 min, stopping..."
docker kill publishing_db_container
Expand Down

0 comments on commit 9f5d8ce

Please sign in to comment.