Skip to content

Commit

Permalink
Merge pull request #117 from stuartmaxwell:move-db-to-volument
Browse files Browse the repository at this point in the history
Change DB to run on volume
  • Loading branch information
stuartmaxwell authored Oct 8, 2024
2 parents 6e54693 + f2e80c6 commit 50b2665
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
13 changes: 11 additions & 2 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@

set -euf -o pipefail

DB_PATH="/app/$DB_NAME.sqlite3"
BACKUP_PATH="/app/backup"
DB_PATH="/app/db/$DB_NAME.sqlite3"
BACKUP_PATH="/app/db/backup"
HOUR=$(date +%H)
BACKUP_FILE="${BACKUP_PATH}/backup-${HOUR}.sqlite3"
TAR_FILE="${BACKUP_PATH}/backup-${HOUR}.tar.gz"

echo "Backing up SQLite database to ${TAR_FILE}..."

# Ensure the backup directory exists
echo "Ensuring backup directory exists..."
mkdir -p "${BACKUP_PATH}"

# Backup the SQLite database
echo "Backing up SQLite database..."
sqlite3 "${DB_PATH}" "VACUUM INTO '${BACKUP_FILE}'"

# Compress the backup
echo "Compressing backup..."
tar --gzip -cf "${TAR_FILE}" -C "${BACKUP_PATH}" "$(basename "${BACKUP_FILE}")"
echo "Remove backup file..."
rm "${BACKUP_FILE}"

# Upload to S3
echo "Uploading ${TAR_FILE} to S3: s3://$S3_BUCKET/backup-${HOUR}.tar.gz"
aws s3 cp "${TAR_FILE}" "s3://$S3_BUCKET/backup-${HOUR}.tar.gz" --endpoint-url $AWS_ENDPOINT_URL

echo "Backup complete."
2 changes: 1 addition & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
WSGI_APPLICATION = "config.wsgi.application"

# Database
DB_NAME = BASE_DIR / f"{env('DB_NAME')}.sqlite3" if "sqlite" in env("DB_ENGINE") else env("DB_NAME")
DB_NAME = BASE_DIR / "db" / f"{env('DB_NAME')}.sqlite3" if "sqlite" in env("DB_ENGINE") else env("DB_NAME")
SQLITE_OPTIONS = {
"init_command": (
"PRAGMA foreign_keys=ON;"
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
- "BLOG_TITLE=${BLOG_TITLE}"
- "POST_PREFIX=${POST_PREFIX}"
volumes:
- app_data:/app
- app_db:/app/db

volumes:
app_data:
app_db:
6 changes: 3 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ download_latest_backup() {
echo "No backup found in S3 bucket."
else
echo "Latest backup found: $latest_backup"
aws s3 cp s3://$S3_BUCKET/$latest_backup /app/latest-backup.tar.gz --endpoint-url $AWS_ENDPOINT_URL
tar --gzip -xf /app/latest-backup.tar.gz -O > /app/$DB_NAME.sqlite3
aws s3 cp s3://$S3_BUCKET/$latest_backup /app/db/latest-backup.tar.gz --endpoint-url $AWS_ENDPOINT_URL
tar --gzip -xf /app/db/latest-backup.tar.gz -O > /app/db/$DB_NAME.sqlite3
echo "SQLite database backup downloaded and extracted."
fi
}
Expand All @@ -30,7 +30,7 @@ fi

# Check if the database engine is SQLite and the database file doesn't exist
echo "Checking if SQLite database file exists"
if [ "$DB_ENGINE" = "django.db.backends.sqlite3" ] && [ ! -f "/app/$DB_NAME.sqlite3" ]; then
if [ "$DB_ENGINE" = "django.db.backends.sqlite3" ] && [ ! -f "/app/db/$DB_NAME.sqlite3" ]; then
download_latest_backup
fi

Expand Down

0 comments on commit 50b2665

Please sign in to comment.