Wp Backup Restore
WordPress Backup and Restore Procedures
These procedures have been tested at least once on an ansible managed Ubuntu 16.04 server. The backup and restore procedures are very good automation targets.
Automation Constraints
- URL name length is different than mysql user, database, password maximum length
- Use
sudo -u www-data do-stuff
to maintain ownership underapache2
'swww-data
user. - It looks like a lot of PHP5 stuff might break on PHP7. PHP versioning could be very important.
- PHP versioning creates tons of security concerns. Requires compartmentalization/virtualization
WordPress Backup Procedure
This backup methodology is generic across projects and databases. The written protocol is specific for apache2
, MySQL
, and WordPress
.
- Backup the database
$ sudo mysql -e "show databases; \q"
$ sudo mysqldump mydbname > ~/mydbname.dump.sql
- Back up www project root directory, e.g.
/var/www/websites/www.example.com
- Back up the whole wordpress install - it can be used as a complete snapshot
- The wordpress install is a specific version and is tiny anyways
$ cd /var/www/websites/
- Prune any big logs in
logs
:access.log
,error.log
$ tar -czvf ~/www.example.com.bad.YYYYMMDD.HHMM.tar.gz www.example.com
- Move the backups from ~ to the appropriate archival location
WordPress Restore Procedure
- Backup the bad database and files. Include
bad
in the backup filenames.- Use the
WordPress Backup Procedure
- Use the
- Disable the site in apache2
sudo a2dissite www.example.com
sudo service apache2 reload
- Delete the database being overwritten in mysql - feel free to login and combine steps. These are added this way for atomicity."
$ sudo mysql -e "DROP DATABASE mydbname; \q"
$ sudo mysql -e "CREATE DATABASE wwwthesketchycom; \q"
$ sudo mysql mydbname < mydbname.bkp.sql
- User grants stay intact when a database is dropped; no need to modify users
- Assess and restore the file backup to see which files will be restored.
- Typically the restore will be the entire
public_html
directory - Delete the old directory that will be replaced.
- As
www-data
, restore the backup.cd /var/www/websites
rm -rf www.example.com
- use extra caution, write the command then go back and add the
-rf
flags.
- use extra caution, write the command then go back and add the
sudo -u www-data tar -xzvf ~/www.thesketchy.com.bkp.YYYYMMDD.HHMM.tar.gz
- Typically the restore will be the entire
- Re-enable the site:
sudo a2ensite www.example.com
sudo service apache2 reload