Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
71d763b
wip: script to backup postgres database
paigewilliams Jun 24, 2026
6b82fc0
Merge branch 'main' into backup-cron-jobs
paigewilliams Jun 24, 2026
4ba064e
add script to set up cron job for sql dumps
paigewilliams Jun 24, 2026
3392b0e
refactor db backup cron to just dump the db
paigewilliams Jun 25, 2026
9cb57df
add flags to pg_dump
paigewilliams Jun 25, 2026
fa2ceb5
add instructions on setting up the backup cron job in install-docker-…
paigewilliams Jun 26, 2026
a06d019
fix name of db dump script to run
paigewilliams Jun 29, 2026
8c648f2
copy instead of move .env.prod file
paigewilliams Jun 29, 2026
de8c67b
Merge branch 'main' into backup-cron-jobs
paigewilliams Jun 29, 2026
ca33bea
use path to docker in db-dump
paigewilliams Jun 29, 2026
e97fa9c
temporarily use backup-cron-jobs branch in install script
paigewilliams Jun 29, 2026
12a10d3
check docker locations
paigewilliams Jun 29, 2026
6840198
update TEKDB_ROOT dir
paigewilliams Jun 30, 2026
f61d806
store backups one directory out
paigewilliams Jun 30, 2026
eba4e3e
use docker group in install-docker-env.sh
paigewilliams Jun 30, 2026
8e2853a
use sudo for docker commands
paigewilliams Jun 30, 2026
4e2c476
include datetime in db-dump logs
paigewilliams Jun 30, 2026
34b3e28
fix syntax for date parsing
paigewilliams Jun 30, 2026
a5c65f8
include datetime in all db-dump logs
paigewilliams Jun 30, 2026
8b3d04c
switch to main branch in install-docker-env.sh
paigewilliams Jun 30, 2026
cff10e1
use sudo for crontab
paigewilliams Jun 30, 2026
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
37 changes: 37 additions & 0 deletions scripts/Linux/db-dump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -euo pipefail

ENV_FILE="${1:-}"

if [ -f "$ENV_FILE" ]; then
set -a
echo "$(date +"%Y-%m-%d %H:%M:%S") - Loading environment variables from $ENV_FILE..."
source "$ENV_FILE"
set +a
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Error: File "$ENV_FILE" not found. Please provide a valid environment file as an argument."
exit 1
fi

CONTAINER_NAME=db
SQL_USER=${SQL_USER:?SQL_USER is not set}
SQL_DATABASE=${SQL_DATABASE:?SQL_DATABASE is not set}
BACKUP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)/backups/postgres"

BACKUP_FILE="${BACKUP_DIR}/${SQL_DATABASE}.sql"

mkdir -p "${BACKUP_DIR}"

# Use full path to docker for cron compatibility
# Find docker location
if [ -x "/usr/bin/docker" ]; then
DOCKER_CMD="/usr/bin/docker"
elif [ -x "/usr/local/bin/docker" ]; then
DOCKER_CMD="/usr/local/bin/docker"
else
DOCKER_CMD="docker"
fi

echo "$(date +"%Y-%m-%d %H:%M:%S") - Dumping database '${SQL_DATABASE}' from container '${CONTAINER_NAME}' to '${BACKUP_FILE}'..."
"${DOCKER_CMD}" exec -t "${CONTAINER_NAME}" pg_dump -b -c -n public -O --quote-all-identifiers --no-acl -w -U "${SQL_USER}" "${SQL_DATABASE}" > "${BACKUP_FILE}"
echo "$(date +"%Y-%m-%d %H:%M:%S") - Database dump completed successfully."
24 changes: 20 additions & 4 deletions scripts/Linux/install-docker-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,35 @@ echo "Moving the .env.prod file to the Docker directory..."
ln -s $ENV_FILE $PWD/docker/.env.prod

echo "Pulling the latest Docker image..."
docker pull ghcr.io/ecotrust/tekdb/web:latest
sudo docker pull ghcr.io/ecotrust/tekdb/web:latest

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

needed to change to sudo to prevent error of permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock


echo "Building the proxy image..."
docker compose --env-file docker/.env.prod -f docker/docker-compose.prod.local.yaml build proxy
sudo docker compose --env-file docker/.env.prod -f docker/docker-compose.prod.local.yaml build proxy

echo "Starting the Docker containers..."
docker compose --env-file docker/.env.prod -f docker/docker-compose.prod.local.yaml up -d
sudo docker compose --env-file docker/.env.prod -f docker/docker-compose.prod.local.yaml up -d

echo "Verifying that the containers are running..."
if [ "$(docker container inspect -f '{{.State.Status}}' "tekdb_web" 2>/dev/null)" = "running" ] && [ "$(docker container inspect -f '{{.State.Status}}' "db" 2>/dev/null)" = "running" ] && [ "$(docker container inspect -f '{{.State.Status}}' "tekdb_proxy" 2>/dev/null)" = "running" ]; then
if [ "$(sudo docker container inspect -f '{{.State.Status}}' "tekdb_web" 2>/dev/null)" = "running" ] && [ "$(sudo docker container inspect -f '{{.State.Status}}' "db" 2>/dev/null)" = "running" ] && [ "$(sudo docker container inspect -f '{{.State.Status}}' "tekdb_proxy" 2>/dev/null)" = "running" ]; then
echo "TEKDB containers are running successfully."
else
echo "Failed to start TEKDB containers. Please check the Docker logs for more information."
exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEKDB_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")/tekdb/TEKDB"

echo "Please follow these steps to create a database backup at the cadence of your choosing using crontab:"
echo "1. Determine what timezone your server is in by running the command: timedatectl"
echo "2. Determine the desired backup schedule (e.g., daily at 9 PM) considering your server's timezone. https://crontab.guru/ is a helpful resource for determining the correct cron schedule syntax."
echo "3. Ensure you have the correct permissions to run the backup script by running: chmod +x ${TEKDB_ROOT}/scripts/Linux/db-dump.sh"
echo "4. Add to your crontab by running: sudo crontab -e"
echo "5. Add to your cron tab (replace <your cron schedule> with your desired schedule):"
echo " <your cron schedule> ${TEKDB_ROOT}/scripts/Linux/db-dump.sh ${TEKDB_ROOT}/docker/.env.prod >> $HOME/tekdb_db_dump.log 2>&1"
echo ""
echo " Example for daily backups at 9 PM:"
echo " 0 21 * * * ${TEKDB_ROOT}/scripts/Linux/db-dump.sh ${TEKDB_ROOT}/docker/.env.prod >> $HOME/tekdb_db_dump.log 2>&1"
echo ""
echo "Note: Log file will be created at $HOME/tekdb_db_dump.log. You can monitor it with: tail -f $HOME/tekdb_db_dump.log"
exit 0