Skip to content

Commit

Permalink
build(docker): add db-tools doker for backup/restore
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxvd committed Oct 17, 2024
1 parent c1d14c5 commit dc64e6c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BACKUP_ZIP_PASSWORD=<zip-password>
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.npm/
.idea/
node_modules/
bower_components/
Expand All @@ -9,3 +10,6 @@ public/css/main.css*
uploads/
public/*_stats.json
public/organizations.json
.env
backups/
restore/
17 changes: 16 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ services:

testdb:
image: postgres:9.4
restart: no
environment:
POSTGRES_PASSWORD: 'testpass'
POSTGRES_USER: 'testuser'
Expand All @@ -97,13 +98,27 @@ services:

dbadmin:
image: adminer
restart: no
links:
- db
- testdb
ports:
- "8080:8080"
profiles:
- tools

db-tools:
build: docker/db-tools
restart: no
environment:
DATABASE_HOST: db
DATABASE_PORT: 5432
DATABASE_NAME: smartbirds
DATABASE_PASSWORD: 'secret'
DATABASE_USER: 'smartbirds'
BACKUP_ZIP_PASSWORD: ${BACKUP_ZIP_PASSWORD}
volumes:
- ./backups:/backups
- ./restore:/restore

volumes:
public:
17 changes: 17 additions & 0 deletions docker/db-tools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM postgres:9.4

COPY entrypoint.sh /entrypoint.sh

RUN chmod +x /entrypoint.sh

RUN echo "deb http://archive.debian.org/debian stretch main\ndeb http://archive.debian.org/debian-security stretch/updates main" > /etc/apt/sources.list
RUN rm /etc/apt/sources.list.d/pgdg.list

RUN apt-get update && apt-get -y install apt-transport-https

RUN echo "deb https://apt-archive.postgresql.org/pub/repos/apt/ stretch-pgdg main" > /etc/apt/sources.list.d/pgdg.list

RUN apt-get update && apt-get -y install zip && rm -rf /var/lib/apt/lists/*p

ENTRYPOINT ["/entrypoint.sh"]
CMD ["help"]
93 changes: 93 additions & 0 deletions docker/db-tools/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

set -e

function help {
echo "Available commands:"
echo " backup [<backup-file>] - Backup the database to a file"
echo " restore <backup-file> <dbname> - Restore the database from a file"
exit 1
}

[ $# -eq 0 ] && help

cmd=$1

case $cmd in
backup)
shift
(
#check if backup file name is provided
BACKUP_FILE=$1
if [ -z "$1" ]; then
echo "Backup file not provided. Using default file name"
BACKUP_FILE="backup-$(date +%Y-%m-%d-%H-%M-%S).dump"
fi

if [ -z "$BACKUP_ZIP_PASSWORD" ]; then
echo "Backup zip password not provided"
exit 1
fi

echo "Creating ${BACKUP_FILE} backup file ..."
export PGPASSWORD=$DATABASE_PASSWORD
pg_dump -w -Fc -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" "$DATABASE_NAME" \
| zip -q --password "$BACKUP_ZIP_PASSWORD" > /backups/"$BACKUP_FILE"
echo "Backup completed."
)
;;
restore)
shift
(
#check if backup file name is provided
BACKUP_FILE=$1
if [ -z "$1" ]; then
echo "Backup file not provided"
exit 1
fi

DB_NAME=$2
if [ -z "$2" ]; then
echo "Database name not provided"
exit 1
fi

if [ -z "$BACKUP_ZIP_PASSWORD" ]; then
echo "Backup zip password not provided"
exit 1
fi

echo "Restoring ${BACKUP_FILE} backup file into ${DB_NAME} database ..."
export PGPASSWORD=$DATABASE_PASSWORD

# restore from backup file
createdb -w -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" "${DB_NAME}-temp" \
&&
unzip -p -P "$BACKUP_ZIP_PASSWORD" /restore/"$BACKUP_FILE" \
| pg_restore -w -x -O --clean --if-exists -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" --role "$DATABASE_USER" -d "${DB_NAME}-temp"

if [ $? -eq 0 ]; then
# Drop the original database
dropdb -w -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" "$DB_NAME" &&\
psql -w -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" -d postgres -c "ALTER DATABASE \"${DB_NAME}-temp\" RENAME TO \"$DB_NAME\";"
else
echo "Restore failed."
exit 1
fi

if [ $? -eq 0 ]; then
echo "Restore completed."
else
echo "Restore failed."
exit 1
fi
)
;;
help)
help
;;
*)
echo "Invalid command $cmd"
help
;;
esac

0 comments on commit dc64e6c

Please sign in to comment.