Server Migration Process
This is a tutorial for moving of server data from one server to another. i consider you have setup the server using guide http://zain.pk/setup-of-ubuntu-server-or-ubuntu-desktop-on-virtualbox/ The link shows how to install server on virtualbox. Production server is not much different then running server on virtual machine. Things we will consider moving from server are user home directory, databases, websites, svn. i have divided server migration tasks into two main categories.
- Commands will run on old server which will take backup of data.
- Commands will run on new server which will import data.
OLD SERVER
We will clean all unnecessary data from ‘/home’, ‘/var/www/ directory or any other directory which has backups or garbage files.
Now its time to take backup of databases but before we move forward its good to repair all databases.
mysqlcheck --repair -uUSERNAME -p --all-databases
Database Backup Option1: Take backup of all databases except few mysql specific databases. Replace USERNAME, PASSWORD in the command below.
mysql -uUSERNAME -pPASSWORD --batch --skip-column-names --execute="SHOW DATABASES" | grep -v "mysql" | grep -v "information_schema" | grep -v "performance_schema" | grep -v "phpmyadmin" | xargs mysqldump -port=3306 --opt --hex-blob --routines --add-locks --disable-keys --quick --no-autocommit --skip-add-locks --max_allowed_packet=128M -uUSERNAME -pPASSWORD --databases | gzip > /home/backups/databases-dump-2014-05-25.sql.gz;
Database Backup Option2: You can take backup of databases by listing databases in commands. Replace USERNAME, DATABASE_NAME with real information.
mysqldump -uUSERNAME -p DATABASE_NAME, DATABASE_NAME2, DATABASE_NAME3 | gzip > /home/backups/database-dump-2015-05-25.sql.gz
Database Backup Option3: If your mysql version is same and you don’t take about importing of mysql internal tables you can run this command to backup databases. Replace USERNAME with real username.
mysqldump -uUSERNAME -p --all-databases | gzip > /home/backups/database-dump-2015-05-25.sql.gz
Now we need to take backup of cronjobs. Cronjobs are user specific and we will take backup of each user cronjobs. Replace USERNAME with logged in user.
crontab -l > /home/backup-2014-05-26/crontab-USERNAME.backup
Change the user with command below and run the above command again for that user.
su USERNAME
We need to take backup of svn database.
svnadmin dump /svn/ > /home/backup-2014-05-26/2014-05-26-repo.svn-dump;
Now we will zip the svn repo
cd /home/backup-2014-05-26/; tar -czf 2014-05-26-repo.svn-dump.tar.gz 2014-05-26-repo.svn-dump
Delete the svn dump file as we would be moving all things from folder /home/backup-2014-05-26 and svn dump without zip will increase folder size.
rm /home/backup-2014-05-26/2014-05-26-repo.svn-dump
NEW SERVER
We will start rsync from newly created server and import data. You can learn more about rsync on “rsync command examples”
You should have rsync installed on both machines to get rsync working
apt-get install rsync
Copy data website’s data which in my case is in /var/www/
rsync -avz --progress username@domian-name.com:/var/www/* /var/www/
Copy backup of old server data which include database sql zipped file, cronjobs, etc.
rsync -avz --progress username@domain-name:/home/backup-2014-05-26/* /home/backup-2014-05-26/
Unzip sql file on new server
gunzip /home/backup-2014-05-26/databases-dump-2014-05-26.sql.gz
Import database file into MySql
mysql -uUSERNAME -p < /home/backup-2014-05-26/databases-dump-2014-05-26.sql;
We will now import cronjob for each user. Change the user with command below.
su USERNAME
Run the command to import cronjob for that user. Replace USERNAME with actual user.
crontab /home/backup-2014-05-26/crontab-USERNAME.backup
Repeat the process for each user cronjob.
Once done you should restart cron.
/etc/init.d/cron start
Create a virtual host file for the websites by creating a file in apache sites available folder
/etc/apache2/sites-available/domain-name.com.conf
<VirtualHost *:80> DocumentRoot "/var/www/domain-name.com" ServerName domain-name.com <Directory "/var/www/domain-name.com"> allow from all AllowOverride all Options +Indexes </Directory> ServerAlias www.domain-name.com UseCanonicalName off </VirtualHost>
You now need to activate apache virtual hosts
sudo a2ensite lolwave.com.conf
Now reload apache server to load new configurations
service apache2 reload
Now you need to change domian dns to see the effect but you can replicate that scenario by changing your machine host file. You can bluff your system to point a domain to different server IP. Open file on windows machine C:\Windows\System32\drivers\etc\host and add a line. First segment will be your IP and second is your domain.
111.111.111.111 domain-name.com
Check if your website is working properly and not giving any errors. If all is good. Comment the line above or delete it.
Now go to your domain registrar and under manage DNS change A record for your domain-name.com to new server IP. It will take some time to propagate. you can open command prompt and ping your domain to see new IP change.
you can check status on website “Whats My DNS” for A recrod change. https://www.whatsmydns.net/ or open command prompt and type the following command to see IP change.
ping domain-name.com
All websites are live and one thing is left to imported which is svn. Run the command to unzip svn data.
cd /home/backup-2014-05-26/; gunzip 2014-05-26-repo.svn-dump.tar.gz; tar -xvf 2014-05-26-repo.svn-dump.tar;
Now we will import data into svn repo
sudo svnadmin load /svn/REPO_NAME < /home/backup-2014-05-26/2014-05-26-repo.svn-dump