Skip to main content

Migrate website files/databases to new server

This article describes how you can migrate a website from one server to another with as little interruption as possible. Please replace 'YOURDOMAIN' with the domain / directory you wish to migrate. This guide assumes that the virtual host configuration already exists on the target system.

Preparation

To ensure that the migration can be carried out as quickly and with as little downtime as possible, we recommend to prepare the following configurations:

.htaccess

If a .htaccess file already exists in the directory of the domain to be migrated, we recommend creating a copy of it.

Source system

cp /home/www-data/YOURDOMAIN/.htaccess /home/www-data/YOURDOMAIN/.htaccess_backup

DNS

We recommend setting the TTL ("Time To Live", validity period of the entry) of your DNS entries to a low value, e.g. 300 seconds, ideally two days in advance. This speeds up the distribution of the DNS entries that need to be adjusted later.

Maintenance page

During data migration, it should be ensured that no data is changing. We recommend delivering a maintenance page for the duration of the data migration.

If the software you are using does not support a maintenance function, please create a file called maintenance.html in the 'DocumentRoot' of the domain to be migrated.

The following content can be stored as an example:

(EN) We are currently improving the user experience of our website and look forward to welcoming you again soon.
(DE) Wir verbessern derzeit das Benutzererlebnis unserer Webseite und freuen uns, Sie in Kürze wieder begruessen zu dürfen.

HTML version

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Under Maintenance</title>
<style type="text/css">
body {
text-align: center;
padding: 150px;
}

h1 {
font-size: 50px;
}

body {
font:
20px Helvetica,
sans-serif;
color: #333;
}

main {
display: block;
text-align: left;
width: 650px;
margin: 0 auto;
}

a {
color: #dc8100;
text-decoration: none;
}

a:hover {
color: #333;
text-decoration: none;
}
</style>
</head>

<body>
<main>
<h1>We&rsquo;re back soon</h1>
<article>
<p>
(EN) We are currently improving the user experience of our website and
look forward to welcoming you again soon.
</p>

<p></p>
<p>
(DE) Wir verbessern derzeit das Benutzererlebnis unserer Webseite und
freuen uns, Sie in Kürze wieder begruessen zu dürfen.
</p>
</article>
</main>
</body>
</html>

To activate the maintenance page at a later time, prepare the necessary .htaccess file with the following content:

Source system

/home/www-data/.htaccess_maintenance :

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]

Proxying to the new server

Despite careful preparation and a reduction in the TTL of a domain's DNS entries, it may take some time for end users to receive the updated the DNS entries.

To avoid accesses on the source system, prepare another .htaccess file that allows the source system to act as a reverse proxy. This ensures that all accesses are processed on the target system, while the DNS changes are propagated on the Internet.

The server subdomain of the target system is used as the target of the reverse proxy configuration.

To do this, please create an .htaccess file with the name /home/www-data/.htaccess_proxy and the following content:

Source system

RewriteEngine on
ProxyPreserveHost on
RewriteBase /
RewriteRule ^(.*)$ http(s)://YOURDOMAIN.NEWSERVERNAME.nine.ch/$1 [P]
ProxyPassReverse / http(s)://YOURDOMAIN.NEWSERVERNAME.nine.ch/

Migration

Before you start the migration, first activate the prepared maintenance page. This ensures that no more changes can be made to the data on the source system.

Source system

cp /home/www-data/.htaccess_maintenance /home/www-data/YOURDOMAIN/.htaccess
nine-flush-fpm

nine-flush-fpm empties the PHP OpCode cache. This ensures that the modified .htaccess is read immediately.

If the maintenance page appears as intended, you can start the data synchronization to the target system:

Note: We recommend carrying out an initial synchronization a few days before the planned migration if you have a large volume of data. This drastically reduces the time required for the data synchronization.

The --delete option deletes data from the /home/www-data/YOURDOMAIN/ directory on the target system that does not (or no longer) exist on the source system. This ensures that the data deleted on the source system after a previous data synchronization is also removed on the target system.

rsync -avzH --delete /home/www-data/YOURDOMAIN/ \
NEWSERVER:/home/www-data/YOURDOMAIN

During the copying process, you can take care of the database migration. If an up-to-date version of the database is required, please proceed as follows.

Please replace the placeholder values YOURDATABASEUSER, DATABASENAME, SSHUSER und NEWSERVER with the corresponding values.

Source system

mysqldump --opt --quote-names --single-transaction --user=YOURDATABASEUSER --password DATABASENAME | \
ssh SSHUSER@NEWSERVER mysql --user=YOURDATABASEUSER --password DATABASENAME

The password prompt requires you to enter the password of the SSH user with whom you want to connect to the target system.

Nine creates nightly backups of your databases. If the previous night's backup is sufficient, you will find the necessary information in this article.

Once the data synchronization and the import of the database have been completed, you can replace the file /home/www-data/YOURDOMAIN/.htaccess with your saved .htaccess. The .htaccess used to activate the maintenance page was also copied by the data synchronization.

Target system

cp /home/www-data/YOURDOMAIN/.htaccess_backup /home/www-data/YOURDOMAIN/.htaccess

Under the URL "http(s)://YOURDOMAIN.NEWSERVERNAME.nine.ch/" you can access and test your website on the target system.

Please note that many CMS redirect to the domain used. It may therefore be necessary to adapt the CMS before the migration or change the hosts entries on your PC for the test phase. A guide for adapting the hosts file can be found here.

After you have checked the correct functioning on the target system, you can activate the prepared proxy configuration on the source system.

Source system

cp /home/www-data/.htaccess_proxy /home/www-data/YOURDOMAIN/.htaccess

All accesses are now processed by the target system.

To complete the migration, the DNS entries must be adjusted. Adjust all DNS A entries that point to the source system so that they point to the target system. Depending on the TTL of the entries and the geographical location of the accesses, this adjustment may take effect with a delay of up to 24 hours.

A few days after the DNS adjustment, make sure that it has been carried out successfully. The easiest way to achieve this is to deactivate the .htaccess file that provides the reverse proxy configuration:

Source system

mv /home/www-data/YOURDOMAIN/.htaccess /home/www-data/YOURDOMAIN/.htaccess_disabled

If the DNS adjustment has been made correctly, all access to the website should continue to function without error.