Backup WordPress Blog on Ubuntu - A Step-by-Step Guide

Creating a Local Copy of Your WordPress Blog for Development
Developing and testing changes for your blog requires a safe environment. Modifying a live, visitor-facing blog directly can be risky, particularly for ad-supported sites. This guide details the necessary steps to replicate your live WordPress blog onto a local Ubuntu machine – a process applicable to most Debian-based Linux distributions.
Backing Up Your Live Blog
The initial phase involves creating a complete copy of your existing WordPress installation. Access to your hosting provider’s console or a provided backup system is essential. If you utilize an alternative backup method, you can proceed directly to the next step.
A WordPress blog comprises both the core WordPress files and the database, which stores all post content. Additionally, any uploaded images or files reside within your WordPress file structure, necessitating a backup of both components.
Database Backup
1) Begin by backing up the database. Connect to your server via SSH and execute the following command:
mysqldump -uUSERNAME -pPASSWORD -hSERVER DATABASENAME > dbbackup.bak
Remember to replace the uppercase placeholders with the correct credentials for your specific installation.
File Structure Backup
2) Next, create a backup of the file structure:
tar -cf sitebackup.tar SITEROOTDIRECTORY
Substitute the uppercase text with the root directory of your website.
Transferring Files to Your Local Machine
3) Transfer the backup files to your local Ubuntu machine. Utilize your preferred method – scp, ftp, or any other suitable protocol.
Setting Up Your Local Environment
4) Ensure that apache, php, and mysql are installed on your Ubuntu system. If not, use the following commands at a terminal prompt:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get mysql-server
sudo apt-get php5-mysql
sudo /etc/init.d/apache2 restart
Database Creation and Import
5) Create a new database and import the previously backed-up database. Navigate to the directory where you downloaded the files and run these commands:
mysqladmin create DATABASENAME
mysql -uroot DATABASENAME
Configuring the Local Webserver
6) Configure your local webserver to point to the copied WordPress files. First, extract the tar archive:
tar xvf sitebackup.tar
This will recreate a directory structure mirroring that of your live server. Assuming your site root directory is now located at /home/username/wordpress/, adjust the path accordingly if you extracted it elsewhere. Add an alias to Apache by opening the following file:
/etc/apache2/conf.d/alias
Paste these lines, modifying the paths to match your system and desired test blog URL:
Alias /wordpress /home/username/wordpress
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Enabling mod_rewrite for Permalinks
7) If you utilize Permalinks, replicate the configuration locally and enable the mod_rewrite module:
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
Updating the wp-config File
8) Edit the wp-config file to connect to the local database. If you’ve followed the previous steps, these settings should work with the appropriate database name:
// ** MySQL settings ** //
define('WP_CACHE', false); //Added by WP-Cache Manager
define('DB_NAME', 'DATABASENAME'); // The name of the database
define('DB_USER', 'root'); // Your MySQL username
define('DB_PASSWORD', ''); // ...and password
define('DB_HOST', 'localhost'); // ...and the server MySQL is running on
WP-Cache Configuration (If Applicable)
9) If you are using the WP-Cache caching module, navigate to your blog root directory and execute these commands:
rm wp-content/advanced-cache.php
ln -s wp-content/plugins/wp-cache/wp-cache-phase1.php wp-content/advanced-cache.php
rm wp-content/cache/*
chmod -R 777 *
Fixing Database URLs
10) Finally, update the URLs within the database to reflect the local environment. Create a new text file named fixsettings.sql, copy the following lines into it, and replace 'localhost' with your desired IP address if you intend to allow external connections:
update wp_options set option_value='http://localhost/wordpress/' where option_id=1;
update wp_options set option_value='http://localhost/wordpress/' where option_id=40;
Import this file into the database:
mysql -uroot
Retain this file for future updates. To synchronize your local copy with the latest blog posts, simply import the database backup and re-run fixsettings.sql. Consider scheduling a cron job to automate backups and synchronization.
Accessing Your Local WordPress Installation
You should now be able to access your development copy of the blog by navigating to http://localhost/wordpress/wp-admin/ and logging in with your usual credentials.