Skip to content

Instantly share code, notes, and snippets.

@serafdev
Created July 21, 2023 16:17
Show Gist options
  • Save serafdev/aac1d4b8083d75bcfd7d49eeb2c86318 to your computer and use it in GitHub Desktop.
Save serafdev/aac1d4b8083d75bcfd7d49eeb2c86318 to your computer and use it in GitHub Desktop.
Script to backup Ghost blog
#/usr/bin/sh
WORKDIR=/home/$USER/ghost
cd $WORKDIR
_date=$(date --iso-8601)
_backup_file_root=./backups/$_date
_backup_file_content=$_backup_file_root/content
mkdir -p $_backup_file_content
echo Backing up content into $_backup_file_content/{images, files, media}
docker compose cp ghost:/var/lib/ghost/content/images $_backup_file_content/images
docker compose cp ghost:/var/lib/ghost/content/files $_backup_file_content/files
docker compose cp ghost:/var/lib/ghost/content/media $_backup_file_content/media
_db_dump_fp="/db-dump-$_date.sql"
echo Backing up database into $_backup_file_root$_db_dump_fp
docker compose exec db \
sh -c "mysqldump -u root -p\$MYSQL_ROOT_PASSWORD ghost --result-file=$_db_dump_fp"
docker compose cp db:$_db_dump_fp $_backup_file_root/
@serafdev
Copy link
Author

seraf@localhost:~/ghost> crontab -l
@daily /home/$USER/ghost/backup.sh >> /home/$USER/ghost/logs/$(date --iso-8601).log 2>&1

@serafdev
Copy link
Author

serafdev commented Aug 3, 2023

I have a second isolated deployment (example-app.qa.seraf.dev) where I deploy daily the backups to make sure everything works well, core command:

seraf@localhost:~/ghost-backup> docker compose exec dbb sh -c 'mysql -u root -p$MYSQL_ROOT_PASSWORD -e "show databases; drop database if exists ghost; create database ghost; use ghost; source /docker-entrypoint-initdb.d/init.sql;"'
WARN[0000] The "bds" variable is not set. Defaulting to a blank string. 
WARN[0000] The "bds" variable is not set. Defaulting to a blank string. 
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| ghost              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

This will wipe what's deployed and replace the sql database with the newest backup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment