Main menu

Mar 5 2011

When taking down a website, we want to be sure all our backups are ok so we could restore the website some day, or retrieve the data without much hustle.

Below some command to backup your website and restore it locally. We do the local restore just to check the backup.

1. Backup live database

// Dump the live database locally
mysqldump -u username -p -h mysqlhost db_name > db_name_backup.sql

2. Restore database locally

//  Create a new database on your local machine
mysql –u username -p -e "create database website_backup;"

// See if the database is created 
mysql -u username -p -e "show databases"

// Import the website dump in the new database on your local machine
mysql –u username -p website_backup < website_backup.sql

// Get an idea if everything is alright
// Give an overview of all tables
mysql -u username -p -e "show tables in website_backup"
// Show the number of posts
mysql -u username -p website_backup -e "select count(id) from  tbl_post"

3. Backup live codebase

// ssh to server
ssh username@hostname
// Change to a private folder where we can store backups
cd ../private
// Create a backup of the website by taring and compressing it
tar czfv website-backup.tar.gz ../website-document-root

4. Migrate codebase to local machine

// Close the ssh connection
 exit
// Go to your local server web documents root
// I’m using MAMP
 cd /Applications/mamp/htdocs
// Copy the files form the server to your local machine
 scp username@hostname:private/website-backup.tar.gz /Applications/mamp/htdocs

5. Restore

// Extract the files
 tar xvzf website-backup.tar.gz

// Change the website’s configuration
// to connect with the database on your local machine
nano website-backup/protected/config/main.php	
		// change db-settings

6. Testing

Test the website on your local machine in the browser.