diff --git a/scripts/Linux/db-dump.sh b/scripts/Linux/db-dump.sh new file mode 100755 index 00000000..e933168a --- /dev/null +++ b/scripts/Linux/db-dump.sh @@ -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." \ No newline at end of file diff --git a/scripts/Linux/install-docker-env.sh b/scripts/Linux/install-docker-env.sh index 40a35c5f..df0fcef1 100644 --- a/scripts/Linux/install-docker-env.sh +++ b/scripts/Linux/install-docker-env.sh @@ -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 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 with your desired schedule):" +echo " ${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 \ No newline at end of file