Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pm 663 control dump scratch area location #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
21 changes: 14 additions & 7 deletions bin/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -e

setup.sh

max_pg_wait_count=120
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this configurable via env var and default to 120?


for i in {1..5}; do
export HOSTNAME_VAR="HOSTNAME_$i"
export PGHOST_VAR="PGHOST_$i"
Expand All @@ -28,23 +30,28 @@ for i in {1..5}; do
echo "Dumping database cluster $i: $PGUSER@$PGHOST:$PGPORT"

# Wait for PostgreSQL to become available.
COUNT=0
count=0
until psql -l > /dev/null 2>&1; do
if [[ "$COUNT" == 0 ]]; then
if [[ "$count" == 0 ]]; then
echo "Waiting for PostgreSQL to become available..."
fi
(( COUNT += 1 ))
(( count += 1 ))
[ $count -lt $max_pg_wait_count ] || break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why [] here instead of [[]]? Can we stick to [[]], or even if [[ ... ]]; then ... fi (as above) for consistency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly because I pretty much never use [[ in my own scripts - its utility is generally tiny and it is less portable. I'll tweak this for consistency though.

sleep 1
done
if (( COUNT > 0 )); then
echo "Waited $COUNT seconds."
if (( count > 0 )); then
echo "Waited $count seconds."
psql -l > /dev/null 2>&1 || {
echo "PostgreSQL still not available, trying next backup."
continue
}
fi

mkdir -p "/pg_dump"

# Dump individual databases directly to restic repository.
DBLIST=$(psql -d postgres -q -t -c "SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'rdsadmin', 'template0', 'template1')")
for dbname in $DBLIST; do
dblist=$(psql -d postgres -q -t -c "SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'rdsadmin', 'template0', 'template1')")
for dbname in $dblist; do
echo "Dumping database '$dbname'"
pg_dump --file="/pg_dump/$dbname.sql" --no-owner --no-privileges --dbname="$dbname" || true # Ignore failures
done
Expand Down
4 changes: 3 additions & 1 deletion bin/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

set -e

ok=1
for var in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY RESTIC_PASSWORD RESTIC_REPOSITORY; do
eval [[ -z \${$var+1} ]] && {
>&2 echo "ERROR: Missing required environment variable: $var"
exit 1
ok=
}
done
[ $ok ] || exit 1

if ! restic unlock; then
restic init
Expand Down