zipperer / migrate_database_with_michael

0 stars 0 forks source link

Scheduled backups in place and confirm occurring #24

Closed zipperer closed 5 months ago

zipperer commented 7 months ago

See pg_dump info on #23 .

One alternative for scheduled backups: use cron That is, set up a scheduled job in cron to perform pg_dump on a schedule.

One alternative for confirm occurring: wait a few days and confirm backups are stored in intended place and a restore from the backup works.

zipperer commented 7 months ago

For cron backup, at first try to do as bash command/script so don't need other dependencies (e.g. python)

zipperer commented 7 months ago

Plan:

zipperer commented 7 months ago

Check whether cron installed determine how to install cron determine how to put timestamp in filename of output

zipperer commented 7 months ago

Shell interactions while I experimented: 20231130-shell-interactions-0.txt

Example crontab from docker container: 20231130_example_crontab_from_docker_container.txt

Example /root/.pgpass from docker container: example_slash_root_slashdot_pgpass.txt

I will distill these into steps.

steps

debug

psql command in crontab file

psql options

when use psql -Fc ... to dump, use pg_restore to restore

example formatting cron action as script

crontab file

* * * * * /my_postgres_dump_script.sh 2>> /my_script_output.txt

my_postgres_dump_script.sh

#!/bin/bash

TIMESTAMP=$(date "+%Y%m%d_%H_%M_%S")
echo "script start: ${TIMESTAMP}" >> /my_script_output.txt
POSTGRES_USERNAME=postgres
POSTGRES_DBNAME=postgres
POSTGRES_BACKUP_DIRECTORY=my_postgres_backups
POSTGRES_BACKUP_FILE_PREFIX=postgres_backup_
POSTGRES_BACKUP_FILE_SUFFIX=
PG_DUMP_OPTIONS=-Fc
if [[ $PG_DUMP_OPTIONS -eq '-Fc' ]]; then
        POSTGRES_BACKUP_FILE_SUFFIX=_Fc_format
fi
POSTGRES_BACKUP_FILE_EXTENSION=.sql
pg_dump ${PG_DUMP_OPTIONS} --username=$POSTGRES_USERNAME --dbname=$POSTGRES_DBNAME > /${POSTGRES_BACKUP_DIRECTORY}/${POSTGRES_BACKUP_FILE_PREFIX}${TIMESTAMP}${POSTGRES_BACKUP_FILE_SUFFIX}${POSTGRES_BACKUP_FILE_EXTENSION}

#pg_dump -Fc --username=postgres --dbname=postgres > /my_postgres_backups/postgres_backup_${TIMESTAMP}_Fc_format.sql

echo 'script done' >> /my_script_output.txt
echo $(date) >> /my_script_output.txt
zipperer commented 7 months ago

Aside to think about: how to put crontab file under version control?

Can I make the official crontab file a symlink to a file under version control?

zipperer commented 5 months ago

Aside to think about: how to put crontab file under version control? Can I make the official crontab file a symlink to a file under version control?

One option to advance toward a crontab file under version control:

Executing $ crontab example_crontab_file.txt makes example_crontab_file.txt the crontab file for the current user.

This does not guarantee that cron will see the current state of the file in the repository, because the user could call crontab other-file with another file or crontab -e and edit it.

zipperer commented 5 months ago

Aside to think about: how to put crontab file under version control? Can I make the official crontab file a symlink to a file under version control?

Another option under Ubuntu (which has /etc/cron.d/):

zipperer commented 5 months ago

On 20240120 we followed the steps here to set up a cronjob to perform

pg_dump -Fc --username=postgres --dbname=grace_and_truth_db > /my_postgres_backups/postgres_backup_${TIMESTAMP}_Fc_format.sql