I had the following criteria when configuring the backup for my WordPress installation:
- Shell Script
- Protected from the webserver user
- Works with Dreamhost Enhanced User Security
This approach is based on the script from the WordPress Backup Guide at theme.fm.
In addition to your webserver_user
, create a backup_user
account. The backup_user
will have read access to the website files; however, the webserver_user
will not have read access to the backups.
Configure the Accounts
Login as backup_user
and run ssh-keygen -t rsa -b <keysize>
. Keysize should be 2048 or 4096 (pick the bigger size for more security). Accept all of the defaults. Run cat ~/.ssh/id_rsa.pub
and copy the output. Create ~/backup
and ~/scripts
. Run chmod go-rw ~/backup
.
Login as webserver_user
and edit/create ~/.ssh/authorized_keys
. Add the key copied from id_rsa.pub
.
Script Files
Create the following files and make the *.sh files executable:
/home/webserver_user/scripts/opts
[client] host=<mysql_server> user=<mysql_username> password=<mysql_password>
/home/webserver_user/scripts/save.sh
#!/bin/bash # Make sure we're working in the scripts directory cd /home/webserver_user/scripts # Backup the datbase mysqldump --defaults-file=/home/webserver_user/scripts/opts <wp_datbase> > db.sql # tar the database backup and all of the WordPress files # --transform is not necessary, but removes home/webserver_user from the path when extracting tar czf website-$(date +%Y-%m-%d).tar.gz --transform s,^home/webserver_user/www,www, db.sql /home/webserver_user/www # Cleanup by removing the uncompressed database backup rm db.sql
/home/backup_user/scripts/backup.sh
#!/bin/bash # Set the working directory cd /home/backup_user/backup # Run the save.sh script as the webserver_user ssh webserver_user@webhost.com /home/webserver_user/scripts/save.sh # Copy the backup to the backup_user account scp webserver_user@webhost.com:/home/webserver_user/scripts/*.tar.gz ./ # Remove the copy of the backup from the webserver_user ssh webserver_user@webhost.com 'rm /home/webserver_user/scripts/*.tar.gz' # cleanup the backup directory and only keep the 3 most recent backups while [ "$(ls -1t | wc -l)" -gt 3 ]; do rm "$(ls -t1r | head -n 1)" done
Panel Configuration
- Go to you Dreamhost Web Panel
- Login
- Go to Main menu → Goodies → Cron Jobs
- Click "Add New Cron Job"
- Select User
backup_user
- Title: backup
- Email address if you want notification of the script running
- Command to run:
/home/backup_user/scripts/backup.sh
- When to run: daily or weekly
- Select User