Skip to content

Commit

Permalink
finish db management
Browse files Browse the repository at this point in the history
  • Loading branch information
glencoden committed Jan 4, 2023
1 parent 08e7bc0 commit fbea23f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 50 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
cd /root/apps/wolke
git pull || git clone -b staging --single-branch git@github.com:glencoden/wolke.git ./
echo "${{ secrets.ENV_FILE }}" > .env
echo "\nHOST_ENV=${{ secrets.HOST_ENV_STAGING }}" >> .env
docker compose down --rmi local
docker image prune -f
docker compose up -d --build
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.env
.env*
.DS_Store
/.idea
/temp-pg-backups
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### Restore databases

npm run restore db_name=my-service host_env=develop commit=05657560fa3dd55c2c528b5134f6358bca3dc693
npm run restore db=my-service env=develop commit=05657560fa3dd55c2c528b5134f6358bca3dc693
54 changes: 26 additions & 28 deletions scripts/backup-databases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# check if the .env file exists
if test -e .env; then
# Export the environment variables from the .env file
# export the environment variables from the .env file
export $(grep -v '^#' .env | xargs)
fi

Expand All @@ -18,33 +18,31 @@ done

# make temporary backup directory
mkdir temp-pg-backups
cd temp-pg-backups

# clone remote backup remote
git clone git@github.com:glencoden/wolke-db-backups.git .

# TODO choose this depending on dev stage
dev_stage="develop"

# remove current backups for dev stage
rm -rf "$dev_stage"
mkdir -p "$dev_stage"

# loop over the array of database names
for db_name in "${db_names[@]}"
do
# check if the database exists
if docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database psql -U "$POSTGRES_USER" -lqt | cut -d \| -f 1 | grep -wq "$db_name"; then
# if the database exists, make a backup
docker exec -t postgres-database pg_dump -U glencoden "$db_name" > "$dev_stage/$db_name".sql
fi
done

# make a git commit and push backups to remote
git add .
git commit -m "backup_`date +%d-%m-%Y"_"%H_%M_%S`"
git push origin main
(
cd temp-pg-backups

# clone remote backup remote
git clone git@github.com:glencoden/wolke-db-backups.git .

# remove current backups for dev stage
rm -rf "$HOST_ENV"
mkdir -p "$HOST_ENV"

# loop over the array of database names
for db_name in "${db_names[@]}"
do
# check if the database exists
if docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database psql -U "$POSTGRES_USER" -lqt | cut -d \| -f 1 | grep -wq "$db_name"; then
# if the database exists, make a backup
docker exec -t postgres-database pg_dump -U glencoden "$db_name" > "$HOST_ENV/$db_name".sql
fi
done

# make a git commit and push backups to remote
git add .
git commit -m "backup_`date +%d-%m-%Y"_"%H_%M_%S`"
git push origin main
)

# remove remote backup repo
cd ..
rm -rf temp-pg-backups
6 changes: 3 additions & 3 deletions scripts/init-databases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# enable err exit mode
set -e

# Check if the .env file exists
# check if the .env file exists
if test -e .env; then
# Export the environment variables from the .env file
# export the environment variables from the .env file
export $(grep -v '^#' .env | xargs)
fi

Expand All @@ -24,7 +24,7 @@ for db_name in "${db_names[@]}"
do
# check if the database exists
if docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database psql -U "$POSTGRES_USER" -lqt | cut -d \| -f 1 | grep -wq "$db_name"; then
# the database exists, skip the iteration
# if the database exists, skip the iteration
continue
fi

Expand Down
74 changes: 57 additions & 17 deletions scripts/restore-databases.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#!/bin/bash

# Check if the .env file exists
# check if the .env file exists
if test -e .env; then
# Export the environment variables from the .env file
# export the environment variables from the .env file
export $(grep -v '^#' .env | xargs)
fi

# create an empty array
db_names=()

# loop over the environment variables and extract the values of the variables that match the "POSTGRES_DATABASE" pattern
for env_var in $(env | grep "POSTGRES_DATABASE" | cut -d "=" -f 1)
do
# add the value of the variable to the db_names array
db_names+=("$(eval echo "\$$env_var")")
done

# vars for restore configuration, which can be overwritten by args
db_name=all
host_env=develop
host_env=$HOST_ENV
commit=recent

# Iterate over the arguments passed to the script
# iterate over the arguments passed to the script
for arg in "$@"; do
# Split the argument at '='
# split the argument at '='
IFS='=' read -ra sublist <<< "$arg"
if [[ "${sublist[0]}" == "db" ]]; then
db_name="${sublist[1]}"
Expand All @@ -26,22 +36,52 @@ done

# make temporary backup directory
mkdir temp-pg-backups
cd temp-pg-backups
(
cd temp-pg-backups || exit

# clone remote backup remote
git clone git@github.com:glencoden/wolke-db-backups.git .
# clone remote backup remote
git clone git@github.com:glencoden/wolke-db-backups.git .

if [[ "$commit" != "recent" ]]; then
git checkout "$commit"
fi
if [[ "$commit" != "recent" ]]; then
git checkout "$commit"
fi

# function which restores the postgres database from remote backup
function restoreDatabase() {
echo "RESTORE DATABASE $1"
# drop and create database which should be restored
docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database dropdb -f -U "$POSTGRES_USER" "$1"
docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database createdb -U "$POSTGRES_USER" "$1"

# drop table which should be restored
docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database psql -U "$POSTGRES_USER" -c "DROP DATABASE $db_name"
docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database createdb -U "$POSTGRES_USER" "$db_name"
# restore database from backup file
docker exec -t postgres-database psql -U glencoden -w -d "$1" -f backups/"$host_env/$1".sql
}

# restore table from backup file
docker exec -t postgres-database psql -U glencoden -w -d "$db_name" -f backups/"$host_env/$db_name".sql
if [[ "$db_name" == "all" ]]; then
# loop over the array of database names
for current_db_name in "${db_names[@]}"
do
# check if the database exists
if docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-database psql -U "$POSTGRES_USER" -lqt | cut -d \| -f 1 | grep -wq "$current_db_name"; then
# if the database exists, restore it
restoreDatabase "$current_db_name"
fi
done
else
found=0
for i in "${db_names[@]}"; do
if [[ "$i" == "$db_name" ]]; then
found=1
break
fi
done
if [[ "$found" -eq 1 ]]; then
restoreDatabase "$db_name"
else
echo "unknown database name"
fi
fi
)

# remove remote backup repo
cd ..
rm -rf temp-pg-backups

0 comments on commit fbea23f

Please sign in to comment.