Shell script to back up a Ghost CMS site (depends on gdrive-upload to upload to Google Drive). Has been running in production for more than a year.
Pull requests welcome.
- Install
zip(sudo apt install zip)
Set up gdrive-upload
- Open Node.js Quickstart and click 'Enable the Drive API'.
- Give your project a name, and choose
TVs and limited input devicesin the dropdown. - Save
credentials.jsontogdrive-upload/srcand in that directory runnode client.jsto set up your credentials.
Change defaults in backup.sh to match your site
- Change
backupDirto the directory thatbackup-ghost-siteis located in (e.g. /home/david/repos/backup-ghost-site) - Change
domainto the address of your site, e.g.https://davidlane.io. - Change
ghostImagesFolderto the path of your images directory (e.g. /var/www/ghost/content/images/) - (Optional) Change
backupNameto customize the name of the backup archive
Tell backup-ghost-site where gdrive-upload is
- In backupToDrive.js, enter the path to
gdrive-upload, for example:...require('../gdrive-upload'). You can also usenpm linkto makegdrive-uploadavailable globally.
Enter your Ghost login credentials in credentials.sh
GHOSTUSER="Your username here"
GHOSTPASSWORD="Your password here"Run backup.sh to manually backup (or test and make sure it's working).
To run daily, add it to your crontab. Mine looks like this (backing up every night at 3 am):
* * 3 * * /bin/bash /home/david/repos/backup-ghost-site/backup.sh > /home/david/repos/backup-ghost-site/cron.logbackup.sh uploads the backup with the line node $backupDir/backupToDrive.js "$backupDir/$filename". Change this to fit your needs (e.g. replace with something to backup to Dropbox or another server).
Database backup
- Using
curl, saves a JSON dump of the database from the/ghost/api/v3/admin/db/endpoint. - Calls
stripDBTimes.jsto strip values that change whenever we access the API - Runs
diffand to only backup when something has changed (e.g. a new or updated page). Currently clears values (last log in time, etc) for the first Ghost user - if you log in as someone else, you updatestripDBTimes.js.
Backup images
- Maintains a copy of Ghost's
imagesfolder and usesrsyncto sync images. - Only includes images in backup if there are changes.