Cloning My Website

One disadvantage of having a reliable hosting provider is that you tend to forget about backups. In my ten years with Plus hosting there was not a single case of data loss. Regardless I wanted to go "better safe than sorry" route and make automated backups. And, while I am at it, I might as well use it to make "production replica" environment for testing.

My website environment is based on CentOS 6, MySQL database, and Apache. Ideally I would need exactly the same environment. But in this case I decided to upgrade all components to latest versions. In case of Linux that meant going with CentOS 7.1 (minimal).

Installation for CentOS is as simple as it gets. I basically click on Next until there is no button to press. :) Only possibility of error is forgetting to enable the Ethernet network adapter - not a catastrophic mistake; just annoying one. Once install was done, additional packages were in order:

yum install httpd mariadb-server php php-mysql rsync

To connect to my website I created new SSH keys:

ssh-keygen -b 4096

I appended newly created .ssh/id_rsa.pub key to .ssh/authorized_keys on my web server. That meant I could login and copy files without any passwords - great for scripting.

Setting up MySQL/MariaDB came next. It is just a basic setup followed by user and database creation:

mysql_install_db
chown -R mysql:mysql /var/lib/mysql/
service mariadb start
chkconfig mariadb on
mysql -e "CREATE USER 'mydbuser_wp'@'localhost' IDENTIFIED BY 'mydbpassword';"
mysql -e "CREATE DATABASE mydatabase_wordpress"

For data replication (after making sure /home/myuser directory exists) I created simple /root/replicate.sh script with following content:

#!/bin/bash

ssh myuser@myhost.com "mysqldump -u mydbuser_wp -pmydbpassword --opt mydatabase_wordpress" > /var/tmp/mysql.dump
mysql mydatabase_wordpress < /var/tmp/mysql.dump
rm /var/tmp/mysql.dump

scp -r myuser@myhost.com:/home/myuser/* /home/myuser
#rsync -avz -e ssh myuser@myhost.com:/home/myuser /home/myuser

First three lines ensure I have a fresh MySQL database import and SCP is tasked with file copy. Better approach would be rsync but I kept getting Out of memory errors. As my site is not huge, I opted for dummy copy instead of troubleshooting.

Once I ran script once to verify all is working as expected, i added it to crontab (crontab -e) so it runs every midnight:


00 00 * * * /root/replicate.sh

For Apache I edited /etc/httpd/conf/httpd.conf file to change its root:


DocumentRoot "/home/myuser/public_html"
<Directory "/home/myuser/public_html">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

<IfModule mime_module>

AddType text/html .php .phps
</IfModule>

Opening filewall was next task:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save

And all remaining was to start Apache:

chown -R apache:apache /home/myuser/public_html
restorecon -R /home/myuser/public_html
service httpd start
chkconfig httpd on

PS: Do notice that I didn't describe security setup for this machine. Unless there are some mitigating circumstances, you pretty much want your backup as secure as a real thing.

Leave a Reply

Your email address will not be published. Required fields are marked *