Synchronization (XII)
We will copy all our webs to a secondary host that acts as a backup server. We will do that by regularly pulling the data from the main web server.
SSH keys
Create a key and copy it to the main web server:
(root@backup)_$: ssh-keygen -b 4096
(root@backup)_$: ssh-copy-id -i .ssh/id_rsa.pub userdaemon@web.example.com
sudoers file
We need to have rsync permissions in the main web server for the userdaemon
user.
(root@web)_$: visudo
...
# Allow userdaemon to sync things
userdaemon ALL= NOPASSWD: /usr/bin/rsync
...
Synchronization
(root@backup)_$: vi synchronize-web.sh
/root/synchronize-web.sh
------------------------
USER="userdaemon"
HOST="web.example.com"
# Synchronization function
synchronize() {
# Arguments: user, host, source and destination
rsync -av -e "ssh" --rsync-path="sudo rsync" $1@$2:$3 $4
}
# Web (except awstats)
printf "Synchronizing web pages..."
for dir in <website 1> <website 2> <website N>
do
printf " - $dir\n"
SRC="/var/www/$dir/"
DST="/var/www/$dir"
synchronize $USER $HOST $SRC $DST
printf "\n"
done
# MySQL
printf "\n\n"
printf "Synchronizing MySQL databases...\n"
SRC="/var/mysql-backups/"
DST="/var/mysql-backups"
synchronize $USER $HOST $SRC $DST
mysql -u <user> -p <password> <database> < /var/mysql-backups/<database>
# PostgreSQL
printf "\n\n"
printf "Synchronizing PostgreSQL databases...\n"
SRC="/var/postgres-backups/"
DST="/var/postgres-backups"
synchronize $USER $HOST $SRC $DST
# Finished
printf "\n\n"
printf "...DONE\n"
(root@backup)_$: chmod 0700 ./synchronize-web.sh
(root@backup)_$: ./synchronize-web.sh