Archive

Posts Tagged ‘bash’

Скрипт синхронизации баз, сайтов и конфигов

November 24th, 2010

С помощью данного скрипта можно синхронизировать папки сайтов, базы MySQL и конфиги виртуал-хостов апача и nginx с одного сервера на другой. Синхронизация баз происходит без остановку мускула.

#!/bin/bash

param=" --progress -plzuogthr --compress-level=9 --delete-after"
sitesfromserver1="site1.ru site2.ru site3.ru"
basesfromserver1="basesite1 basesite2 basesite3"

### Apache vhosts - Not need "update" parametr
rsync -e "ssh -p222"  --progress -plzogthr --compress-level=9 --delete-after /etc/httpd/vhosts root@server2.domain.com:/etc/httpd

### Nginx vhosts - Not need "update" parametr
rsync -e "ssh -p222"  --progress -plzogthr --compress-level=9 --delete-after /etc/nginx/vhosts root@server2.domain.com:/etc/nginx

### Sites sync
for site in $sitesfromserver1
    do
    rsync -e "ssh -p222" $param /usr/local/www/$site root@server2.domain.com:/usr/local/www
done

### MySQL bases sync
echo "Lock bases on Server1 and Server2"
echo "FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON;" | /usr/bin/mysql -u backup -pPasWord
ssh -p222 root@server2.domain.com 'echo "FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON;" | /usr/bin/mysql -u backup -pPasWord'

echo "Sleep 15s"
sleep 15s

# Bases from Server1
for base in $basesfromserver1
    do
    rsync -e "ssh -p222" $param /var/lib/mysql/$base root@server2.domain.com:/var/lib/mysql
done

echo "Unlock bases"
echo "SET GLOBAL read_only = OFF; UNLOCK TABLES;" | /usr/bin/mysql -u backup -pPasWord
ssh -p222 root@server2.domain.com 'echo "SET GLOBAL read_only = OFF; UNLOCK TABLES;" | /usr/bin/mysql -u backup -pPasWord'

/etc/init.d/mysqld restart
ssh -p222 root@server2.domain.com /etc/init.d/mysqld restart

linux ,