(i) Find the “root” of your site. The easiest way to do is to create a php file (called phpinfo.php) and upload it to your site containing the following:
The contents of the php file should be:
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
Now when you access phpinfo.php in your browser, it will show all information relating to the server inclusing the
_SERVER["DOCUMENT_ROOT"]
which may be
/var/www/html/mydomain.com
Now in putty (or another SSH client), go to the root folder with this command:
cd /var/www/html/mydomain.com
To see the contents of the folder, type
ls
Now copy all the files and convert it into a tar file with the command:
tar -cpzf backup_060412.tar.gz *
You can name the file with a descriptive title and date so you know how old it is. The (*) sign indicates that all files have to be copied into the tar file. If there are already any backup files in the folder, move/ delete them as otherwise they will also get include and bloat the file size.
If you make an error, you will get the cryptic/ whimsical message:
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.
If you did it right, in a couple of seconds/ minutes (depending on the size) you should have a tar file by the specified name.
Step II: Make A Back-Up Of the Database:
To make a backup of ALL databases, run the following command:
mysqldump --all-databases -uuser -ppassword >database060412.sql
‘uuser’ mean ‘u + name of the user’ while ‘ppassword’ means ‘p + user’s password’
You can name the file in a descriptive manner to aid identification.
Step III: Download the tar and database back up files (optional) to your computer:
This may not be feasible if the file sizes are high and you have a slow connection.
Step IV: Transfer the files to the new server:
(i) Open a SSH session for your new server
(ii) locate the root of the site in the manner done earlier and go there
(iii) Download the tar and database file from the old server to the new server’s root folder with this command
wget http://www.myolddomain.com/backup_060412.tar.gz
and
wget http://www.myolddomain.com/database060412.sql
Check the contents of the site’s root folder with the command ls
(iv) Unzip the tar file in the site’s root folder with the command:
tar -xzf backup_060412.tar.gz
(v) Put the database.sql backup into the database (if you haven’t already installed mysql server and phpmyadmin, then do that first) with the command:
mysql -uuser -ppassword< database060412.sql
With this, your new site will be a replica of your old site. Now, at the domain registrar, ensure that the IP address of your domain is changed to show the new site. As soon as the domain name propagates, your new site will be visible.
Many thanks for the detailed explanation.
Just what i was looking for….
brilliant, thanks so much for this, exactly what i needed… and fool proof too! Bit of a noob when it comes to ssh so the step by step tutorial was exactly what i needed. Quite exciting how easy and quick it is using ssh when you've been used to doing it the hard way with ftp!
[…] way, if things go wrong (e.g. if you get locked out of your server) you are not stranded (see the tutorial here for backing up the […]