Backing Up WordPress Before Upgrade (and How to Restore)

For these instructions, wordpress_dir is your wordpress directory.

Find Database Username and Password
cat /var/www/wp-config.php | grep DB

or

cat /var/www/wordpress_dir/wp-config.php | grep DB
Backup Database
mysqldump --no-tablespaces -u wordpressuser -p wordpress_db | xz -9ec > wordpress_20191119.sql.xz
Move to WordPress Directory
cd /var/www
Move Config to WordPress Directory
sudo mv wp-config.php wordpress_dir
Archive WordPress Directory
tar Jcvf ~/wordpress_20191117.tar.xz wordpress_dir
Copy WordPress Directory
sudo cp -a wordpress_dir wordpress_dir.bak
Log in to MySQL
sudo mysql
Create New WordPress Database
CREATE DATABASE wordpress_20191119 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Grant Privileges to New Database
GRANT ALL ON wordpress_20191119.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
Load Backup to New Database
zxcat wordpress_20191119.sql.xz | mysql -u wordpressuser -p wordpress_20191119.sql.xz
Move to Web Directory to Restore Files
cd /var/www
Backup Up Current WordPress Directory
sudo mv wordpress_dir wordpress_dir_broke
Edit wp-config.php
sudo nano /var/www/wordpress_dir/wp-config.php
Edit “DB_Name” to wordpress_20191117

Control-o to save. Control-x to exit.

The website will now be restored

Move wp-config.php from wordpress_dir
sudo mv /var/www/wordpress_dir/wp-config.php /var/www/

Leave a comment

Your email address will not be published. Required fields are marked *