Skip to content

Commit

Permalink
Add CKAN backup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mjanez committed Jul 27, 2023
1 parent cb4c7d2 commit 5fbd5e6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ PostgreSQL offers the command line tools [`pg_dump`](https://www.postgresql.org/
POSTGRES_USER="ckan"
POSTGRES_PASSWORD="your_postgres_password"
BACKUP_DIRECTORY="/path/to/your/backup/directory"
DATE=`date +%Y%m%d%H%M%S`
DATE=$(date +%Y%m%d%H%M%S)
# 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
Expand Down Expand Up @@ -477,6 +477,9 @@ PostgreSQL offers the command line tools [`pg_dump`](https://www.postgresql.org/

The cronjob is now set up and will backup your CKAN PostgreSQL database daily at midnight using the custom format. The backups will be stored in the specified directory with the timestamp in the filename.

>**Info**<br>
> Sample scripts for backing up CKAN: [`doc/scripts`](doc/scripts)

### Restore a backup
If need to use a backup, restore it:

Expand Down
24 changes: 24 additions & 0 deletions doc/scripts/ckan_backup-zip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Set the necessary variables
CONTAINER_NAME="db"
DATABASE_NAME="ckan"
POSTGRES_USER="ckan"
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 $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
12 changes: 12 additions & 0 deletions doc/scripts/ckan_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Set the necessary variables
CONTAINER_NAME="db"
DATABASE_NAME="ckan"
POSTGRES_USER="ckan"
POSTGRES_PASSWORD="your_postgres_password"
BACKUP_DIRECTORY="/path/to/your/backup/directory"
DATE=$(date +%Y%m%d%H%M%S)

# Run the backup command
docker exec -e PGPASSWORD=$POSTGRES_PASSWORD $CONTAINER_NAME pg_dump -U $POSTGRES_USER -Fc $DATABASE_NAME > $BACKUP_DIRECTORY/ckan_backup_$DATE.dump

0 comments on commit 5fbd5e6

Please sign in to comment.