From 49b9be0c3b4057b17ae5e7370a61c2f4e2fe6d00 Mon Sep 17 00:00:00 2001 From: mjanez <96422458+mjanez@users.noreply.github.com> Date: Thu, 23 Nov 2023 11:33:09 +0100 Subject: [PATCH] Fix bash script to backup ckan db --- README.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c53032b2..c54d3c60 100644 --- a/README.md +++ b/README.md @@ -443,19 +443,31 @@ PostgreSQL offers the command line tools [`pg_dump`](https://www.postgresql.org/ 2. Add the following code to the script, replacing the placeholders with your actual values: - ```sh + ```bash #!/bin/bash # Set the necessary variables - POSTGRESQL_CONTAINER_NAME="db" - DATABASE_NAME="ckan" - POSTGRES_USER="ckan" + CONTAINER_NAME="db" + DATABASE_NAME="ckandb" + POSTGRES_USER="postgres" POSTGRES_PASSWORD="your_postgres_password" BACKUP_DIRECTORY="/path/to/your/backup/directory" DATE=$(date +%Y%m%d%H%M%S) + MONTH=$(date +%m) + YEAR=$(date +%Y) + + # Create the monthly backup directory if it doesn't exist + mkdir -p "$BACKUP_DIRECTORY/monthly/$YEAR-$MONTH" # Run the backup command - docker exec -e PGPASSWORD=$POSTGRES_PASSWORD $POSTGRESQL_CONTAINER_NAME pg_dump -U $POSTGRES_USER -Fc $DATABASE_NAME > $BACKUP_DIRECTORY/ckan_backup_$DATE.dump + docker exec -e PGPASSWORD=$POSTGRES_PASSWORD $CONTAINER_NAME pg_dump -U $POSTGRES_USER -Fc $DATABASE_NAME > "$BACKUP_DIRECTORY/monthly/$YEAR-$MONTH/ckan_backup_$DATE.dump" + + # Compress the dump files into a zip archive + cd "$BACKUP_DIRECTORY/monthly/$YEAR-$MONTH" || exit + zip "backup_${YEAR}-${MONTH}.zip" *.dump + + # Remove the original dump files + rm -f *.dump ``` 3. Replace the following placeholders with your actual values: