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

Scritps: Fix load backup and add create backup #2837

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions scripts/create-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

set -e

OUTPUT_DIR="."
if [ "$#" -eq 1 ]; then
OUTPUT_DIR="$(readlink -f $1)/"
fi

# Check output directory
if [ ! -d "$OUTPUT_DIR" ]; then
echo "usage: $0 [OUTPUT_DIR]"
exit 1
fi

# Generating the backup
echo "Generating the backup... (it can take some time)"
docker exec osrd-postgres pg_dump -d osrd -F c -Z 9 -f /tmp/osrd.backup > /dev/null

# Get metadata
echo Collecting backup information...
SHA1=$(docker exec osrd-postgres sha1sum /tmp/osrd.backup | cut -d' ' -f1)
SIZE=$(docker exec osrd-postgres du -sh /tmp/osrd.backup | cut -f1)
echo " sha1: ${SHA1}"
echo " size: ${SIZE}"

DATE=$(date "+%G_%m_%d")

FILE_PATH="${OUTPUT_DIR}/osrd_${DATE}_sha1_${SHA1}.backup"
docker cp osrd-postgres:/tmp/osrd.backup "${FILE_PATH}"
echo "Your backup is ready here: '${FILE_PATH}'"
File renamed without changes.
74 changes: 74 additions & 0 deletions scripts/load-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/sh

set -e

# Check arguments
if [ "$#" -ne 1 ]; then
echo "usage: $0 osrd.backup"
exit 1
fi

# Check sha1 is matching
echo Checking backup integrity...
BACKUP_PATH="$1"
BACKUP_FILENAME=$(basename -s ".backup" "$BACKUP_PATH")
echo "$BACKUP_FILENAME"
EXPECTED_SHA1=$(echo "$BACKUP_FILENAME" | grep -o -E '[0-9a-f]{40}' || echo "renamed")
CURRENT_SHA1=$(sha1sum "$BACKUP_PATH" | cut -d' ' -f1)
if [ "$EXPECTED_SHA1" == "$CURRENT_SHA1" ]; then
echo " ✔ The backup is valid"
elif [ "$EXPECTED_SHA1" == "renamed" ]; then
echo " ✘ Invalid backup file name (you may have renamed the file)"
echo " Found: '${BACKUP_FILENAME}'"
echo " Expected: 'osrd_full_01_01_2000_sha1_0000000000000000000000000000000000000000.backup' "
echo " └───┬───┘ └───┬────┘ └─────────────────────┬─────────────────────┘ └─┬──┘ "
echo " name date sha1 of the backup extension"
exit 2
else
echo " ✘ Corrupted backup (you should download it again)"
echo " Expected sha1: ${EXPECTED_SHA1}"
echo " Current sha1: ${CURRENT_SHA1}"
exit 2
fi

# Check if the database exists
echo "Checking database exists..."
DB_EXISTS="$(docker exec osrd-postgres psql -c "SELECT EXISTS (SELECT FROM pg_stat_database WHERE datname = 'osrd');")"
DB_EXISTS="$(echo "$DB_EXISTS" | grep -o -E '[tf]$')"

if [ $DB_EXISTS == 't' ]; then
echo " Database 'osrd' found"
else
echo " Database 'osrd' not found"
fi

if [ $DB_EXISTS == 't' ]; then
# Check that no service is connected to the database
echo "Checking database availability..."
DB_CONN="$(docker exec osrd-postgres psql -c "SELECT numbackends FROM pg_stat_database WHERE datname = 'osrd';")"
DB_CONN="$(echo "$DB_CONN" | grep -o -E '[0-9]+$')"

if [ $DB_CONN -ne 0 ]; then
echo " The database can not cleared."
echo " You must only have your postgres container runnning!"
exit 2
fi

# Drop database
echo Deleting osrd database...
docker exec osrd-postgres psql -c 'DROP DATABASE osrd;' > /dev/null
fi

echo Initialize new database...
# Here I remove the first line of the script cause the user already exists
docker exec osrd-postgres sh -c 'cat /docker-entrypoint-initdb.d/init.sql | tail -n +2 > /tmp/init.sql'
docker exec osrd-postgres psql -f /tmp/init.sql > /dev/null

# Copy needed files to the container
docker cp "$BACKUP_PATH" osrd-postgres:/tmp/backup-osrd

# restoring the backend can partialy fail, and that's sometimes ok
echo Restore backup...
flomonster marked this conversation as resolved.
Show resolved Hide resolved
docker exec osrd-postgres pg_restore -d osrd -x /tmp/backup-osrd > /dev/null

echo Done !
16 changes: 0 additions & 16 deletions scripts/load-database.sh

This file was deleted.