Restoring a website backup on a local site

Step 1 — Download a full website backup

You’ll need to have a full backup of the website you are going to work on, usually taken from the live site.

Note: If you don’t have access to create a new backup file from the live site please ask the lead developer of your project or the project manager to do it for you.

Step 2 — Extract website files

Now it’s time to extract the website files to you local environment. You can do this in 2 ways:

a. Copying WordPress root folder, recommended if it’s the first time you restore the website to your computer

b. Copying the wp-content folder, recommended if you already have the site installed and only need to update it

Remember to replace <your_site_folder> with the name of the site folder used on your project.

Copying WordPress root folder

  1. Extract the backup file to any folder on your computer.
  2. Copy the contents of the WordPress root folder to your ~/Local Sites/<your_site_folder>/app/public/. The WordPress root folder is the one which has the file wp-config.php.
  3. Change the WordPress secret keys and database connection information on the wp-config.php file.

Copying wp-content folder

  1. Extract the backup file to any folder on your computer.
  2. Delete the wp-content folder from inside your project at ~/Local Sites/<your_site_folder>/app/public/wp-content.
  3. Copy the the wp-content folder from the extracted backup into your project at ~/Local sites/<your_site_folder>/app/public. The wp-content folder resides in the WordPress root folder.

Step 3 — Create local database for your project

You’ll need to create a new database for your project, unless you already have the website installed on your local environment in which case you can skip to Step 4 – Restore database using wp-cli.

On terminal, log into MySql, it will prompt for your MySql root password:

$ mysql -uroot -p

Check the existing databases:

mysql> show databases;

Create a new database for your project, replacing project_database_name with what is appropriate for your project:

mysql> CREATE DATABASE project_database_name;
mysql> exit;

You can find more information on how to manage databases at https://www.digitalocean.com/community/tutorials/how-to-create-and-manage-databases-in-mysql-and-mariadb-on-a-cloud-server

Step 4 – Restore database using wp-cli

Considering you have already changed the database connection information on the wp-config.php file, we can proceed with the database restore.

On terminal, navigate to your ~/Local sites/<your_site_folder>:

$ cd ~/"Local Sites"/<your_site_folder>/app/public

Reset the existing database:

$ wp db reset;

Import the database dump file, first locate the sql dump file and annotate it, usually the sql dump file comes in the WordPress root folder with the backup:

$ wp db import ~/"Local Sites"/<your_site_folder>/app/public/database_dump.sql

It can take some time depending on the size of database. When completed you should see an output similar to:

Success: Imported from '~/Local Sites/<your_site_folder>/app/public/database_dump.sql'.

Step 5 – Replace live site references on your local environment

If you try to access the website on your local environment at this point you’ll probably be redirected to the live site. This happens because the WordPress options are still pointing to the original instance on the database. We’ll fix it in this step using wp search-replace.

Testing the replacements

First we’ll test our command before committing the changes to the database using the --dry-run argument, remember to replace <origin_site_url> with the actual website domain and <your_site_folder> with the name of the site folder used on your project:

$ wp search-replace 'https://projectname.com' 'https://projectname.local' --dry-run
$ wp search-replace 'https:\/\/projectname.com' 'https:\/\/projectname.local' --dry-run

You should see an output with the database tables and the replacements that would have been done, and a success message at the end similar to:

Success: 42550 replacements to be made.
Success: 235 replacements to be made.

Committing the replacements

Now that we tested the results of the replace command we can run it without the --dry-run attribute to make the changes effective:

$ wp search-replace 'https://projectname.com' 'https://projectname.local'
$ wp search-replace 'https:\/\/projectname.com' 'https:\/\/projectname.local'

You should see again an output with the database tables and the replacements this time with the number of changes that have been made to the each table, along with a success message at the end similar to:

Success: Made 42550 replacements.
Success: Made 235 replacements.

Step 6 – Create a .htaccess for your website

Create a .htaccess file inside your site folder with the code below and replace <your_site_folder> with the name of the site folder used on your project:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<your_site_folder>/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /<your_site_folder>/index.php [L]
</IfModule>
# END WordPress

Note: If an .htaccess file already exists you don’t need to replace the file entirely, just make sure the directives RewriteBase and RewriteRule contains the name of your site folder as in the code above.

Step 7 – Accessing the website on your local environment

Now you should be able to access the website running on your local environment.

Open your browser and navigate to:

https://projectname.local

To access the WordPress admin dashboard navigate to:

https://projectname.local/wp-admin/

You can use the same email and password from the origin site (usually the live site). If you can’t access the admin interface you can change the password of your user or create a new user with administrator rights following the documentation of wp user command at https://developer.wordpress.org/cli/commands/user/

Step 8 – Clone repositories for development (optional)

Now that the website is running on your local environment it’s time to clone the repositories for the theme and/or plugins you’ll work on for your project.

Themes

On terminal, navigate to the themes folder of your project replacing <your_site_folder> with the name of your project folder:

$ cd ~/"Local Sites"/<your_site_folder>/app/public/wp-content/themes

Delete your theme’s folder replacing <theme_folder> with the name of the plugin folder you are deleting, next we’ll download it again from the repository:

$ rm -Rf <theme_folder>

Clone the repository replacing <repository_name> with the name of the theme repository you are cloning:

$ git clone [email protected]:fluidweb-co/<repository_name>.git

Repeat the process for each other theme repository that is part of your project.

Plugins

On terminal, navigate to the plugins folder of your project replacing <your_site_folder> with the name of your project folder:

$ cd ~/"Local sites"/<your_site_folder>/app/public/wp-content/plugins

Delete your plugin’s folder replacing <plugin_folder> with the name of the plugin folder you are deleting, next we’ll download it again from the repository:

$ rm -Rf <plugin_folder>

Clone the repository replacing <repository_name> with the name of the plugin repository you are cloning:

$ git clone [email protected]:fluidweb-co/<repository_name>.git

Repeat the process for each other plugins that are part of your project.

Wrap up

You should now have the website running on your development environment and accessible as an administrator.

Please refer to the readme.md file in the project’s theme and plugin repositories to complete the installation of project specific dependencies and start working on the project.