1、備份與恢復(fù)
(1)備份
創(chuàng)建備份目錄
1
[root@mail ~]# mkdir /home/bak
MYSQL備份
1
[root@mail ~]# mysqldump -uroot -p123456 extmail> /home/bak/extmail.sql
郵件備份
1
2
[root@mail ~]# tar -zcvf /home/bak/yourmail.tar.gz /home/domains/yourmail.com
[root@mail ~]# tar -zcvf /home/bak/seconed.tar.gz /home/domains/seconed.cn
WEB備份
1
[root@mail ~]# tar -zcvf /home/bak/extsuite.tar.gz /var/www/extsuite
DSPAM備份
1
2
[root@mail ~]# mysqldump -uroot -p123456 dspam> /home/bak/dspam.sql
[root@mail ~]# tar -zcvf /home/bak/dspam_data.tar.gz /usr/local/dspam/var/dspam/data
配置備份
1
2
[root@mail ~]# cp /etc/postfix/*.cf /home/bak/
[root@mail ~]# cp /etc/httpd/conf.d/ /home/bak/
打包備份文件,可轉(zhuǎn)儲至其他地方
1
[root@mail ~]# tar -zcvf /home/bak20141229.tar.gz /home/bak
(2)還原
MYSQL恢復(fù)
1
[root@mail ~]# mysql -uroot -p123456 extmail< /home/bak/extmail.sql
郵件恢復(fù)
1
2
[root@mail ~]# tar zxvf /home/bak/yourmail.tar.gz -C /home/domains
[root@mail ~]# tar zxvf /home/bak/seconed.tar.gz -C /home/domains
修改權(quán)限
1
[root@mail ~]# chown -R vmail.vmail /home/domains
(3)遠程備份
說明:scp命令基于SSH,可以在linux之間復(fù)制文件和目錄。
在遠程服務(wù)器上創(chuàng)建備份目錄
1
[root@mail ~]# mkdir /home/mailbak
在主服務(wù)器復(fù)制備份文件到遠程服務(wù)器
1
[root@mail ~]# scp /home/bak/yourmail.tar.gz root@10.188.1.86:/home/mailbak/
或者將所有備份的壓縮包復(fù)制過去
1
[root@mail ~]# scp /home/bak20141229.tar.gz root@10.188.1.86:/home/mailbak/
也可以直接將原目錄復(fù)制過去,但原目錄文件正在被操作的話就不好了
1
[root@mail ~]# scp -r /home/domains/yourmail.com root@10.188.1.86:/home/mailbak/
看到這里,就有人會想添加計劃任務(wù)實現(xiàn)定時備份了,下文有更好的實時備份方案。
2、郵箱轉(zhuǎn)移
說明:在前面的各種測試了,已經(jīng)提到了另一個郵箱服務(wù)器 mail.eplantstore.com(10.188.1.86);
同樣使用postfix+extmail搭建的,不需要太復(fù)雜,完成前臺+后臺+多域名配置就行了。
任務(wù):將原服務(wù)器1.83中的seconed.cn域轉(zhuǎn)移到1.86上。
(1)先來看看原服務(wù)器上的seconed.cn
結(jié)論:目前seconed.cn域中只有一個test2用戶,有3封郵件。
(2)轉(zhuǎn)移用戶賬號
1
2
3
[root@mail ~]# mysqldump -uroot -p123456 -t -w "domain='seconed.cn'" extmail > /home/seconed.sql
mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `manager`
WHERE domain='seconed.cn'': Unknown column 'domain' in 'where clause' (1054)
說明:
-t表示不增加create table和drop table語句,導(dǎo)入時只新增備份中的數(shù)據(jù);
-w表示添加條件 where,篩選出domain列的值為seconed.cn的數(shù)據(jù);
報錯意思是manager表中沒有domain列,沒關(guān)系,不用理會;
可以查看seconed.sql文件內(nèi)容,確定沒有drop table,且數(shù)據(jù)都是seconed.cn域的。
遠程復(fù)制到目標(biāo)服務(wù)器上:
1
2
3
[root@mail ~]# scp /home/seconed.sql root@10.188.1.86:/home/
root@10.188.1.86's password:
seconed.sql 100% 2208 2.2KB/s 00:00
(以下是1.86上的操作)
謹(jǐn)慎起見,先備份下目標(biāo)服務(wù)器上的數(shù)據(jù)庫:
1
[root@mail ~]# mysqldump -uroot -p123456 extmail> /home/extmail.sql
在目標(biāo)服務(wù)器上導(dǎo)入數(shù)據(jù):
1
[root@mail ~]# mysql -uroot -p123456 extmail < /home/seconed.sql
了解原理并清楚的知道不會破壞目標(biāo)服務(wù)器的數(shù)據(jù)庫后,可以一步到位的轉(zhuǎn)移:
1
[root@mail ~]# mysqldump -uroot -p123456 -t -w "domain='seconed.cn'" extmail | mysql --host=10.188.1.86 -C extmail
說明:-C表示壓縮傳輸,目標(biāo)服務(wù)器數(shù)據(jù)庫extmail必須已存在。
(3)登陸eplantstore.com后臺頁面檢查結(jié)果
結(jié)論:賬號轉(zhuǎn)移成功,如果你想再次導(dǎo)入seconed.sql是不行的,因為已經(jīng)存在,除非先刪除。
(4)轉(zhuǎn)移郵件目錄
1
[root@mail ~]# scp -r /home/domains/seconed.cn root@10.188.1.86:/home/domains/
(5)轉(zhuǎn)移配置文件
前提是目標(biāo)服務(wù)器配置了多域名訪問,參考我前面的博文:
http://ywzhou.blog.51cto.com/2785388/1595764主要是目標(biāo)服務(wù)器的CGI文件替換:
1
2
3
4
5
6
[root@mail ~]# cd /var/www/extsuite/extmail
[root@mail extmail]# wget http://www.mzone.cc/wp-content/uploads/2011/04/extmail-cgi-ModifyBy-www.mzone_.cc.zip
[root@mail extmail]# unzip extmail-cgi-ModifyBy-www.mzone_.cc.zip
[root@mail extmail]# mv cgi cgi-bak
[root@mail extmail]# mv extmail-cgi-ModifyBy-www.mzone.cc cgi
[root@mail extmail]# chmod 755 -R cgi
原服務(wù)器進行配置文件轉(zhuǎn)移:
1
2
3
4
[root@mail ~]# scp /var/www/extsuite/extmail/seconed.cf root@10.188.1.86:/var/www/extsuite/extmail/
[root@mail ~]# scp /var/www/extsuite/extmail/globabook-seconed.cf root@10.188.1.86:/var/www/extsuite/extmail/
[root@mail ~]# scp -r /var/www/extsuite/extmail/lang/seconed root@10.188.1.86:/var/www/extsuite/extmail/lang/
[root@mail ~]# scp -r /var/www/extsuite/extmail/html/seconed root@10.188.1.86:/var/www/extsuite/extmail/html/
說明:如果沒有配置多域名訪問就不用轉(zhuǎn)移配置文件了,一切使用默認(rèn)配置。
(6)修改目標(biāo)服務(wù)器文件屬主
如果目標(biāo)服務(wù)器上的用戶不是vmail,而是vuser.vgroup或其他的,請修改成正確的。
1
2
[root@mail ~]# chown vmail.vmail -R /var/www/extsuite/extmail/cgi
[root@mail ~]# chown vmail.vmail -R /home/domains/seconed.cn
(7)測試訪問結(jié)果
說明:通過IP訪問只會應(yīng)用默認(rèn)配置。
(8)修改域名解析
我用的是內(nèi)部DNS服務(wù)器,你如果沒有可以修改你的電腦HOST文件。
結(jié)論:只有通過域名訪問才能應(yīng)用到指定配置文件,在CMD中執(zhí)行nslookup mail.seconed.cn以確認(rèn)解析是否指向目標(biāo)服務(wù)器10.188.1.86。
3、主從熱備(rsync)
rsync的特點:定時的同步目錄的更新文件,而不是每次全部的同步;
保持文件的屬主,時間,大小等屬性;同步速度快。
(1)服務(wù)端(mail.yourmail.com 10.188.1.83)
安裝rsync同步工具:
1
[root@mail ~]# yum install -y rsync
創(chuàng)建用于同步的系統(tǒng)用戶:
1
2
3
[root@mail ~]# useradd beifen
[root@mail ~]# passwd beifen
123456
創(chuàng)建同步配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@mail ~]# vi /etc/rsyncd.conf
uid = beifen
gid = beifen
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[domains]
#同步郵件目錄
path = /home/domains/
auth users = beifen
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
read only = no
hosts allow = 10.188.1.86
[mysql]
#同步mysql的extmail數(shù)據(jù)庫文件(賬號信息)
path = /var/lib/mysql/extmail/
auth users = beifen
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
read only = no
hosts allow = 10.188.1.86
說明:在模塊[]中設(shè)置的uid\gid必須對path定義的目錄有權(quán)限,否則日志中報錯:
1
rsync: change_dir "/" (in mysql) failed: Permission denied (13)
創(chuàng)建rsyncd的密碼文件:
1
2
[root@mail ~]# vi /etc/rsyncd.secrets
beifen:123456
修改密碼文件權(quán)限:
1
[root@mail ~]# chmod 600 /etc/rsyncd.secrets
如果是root用戶運行rsync進程,該文件屬主必須是root,否則報錯
1
secrets file must be owned by root when running as root (see strict modes)
添加域名解析:
1
[root@mail ~]# echo "10.188.1.86 mail.eplantstore.com" >> /etc/hosts
不添加域名解析會報錯
1
name lookup failed for 10.188.1.86: Name or service not known
設(shè)置防火墻:
1
2
[root@mail ~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT
[root@mail ~]# service iptables save
啟動程序:
1
2
3
4
5
6
7
8
[root@mail ~]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
[root@mail ~]# echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local
[root@mail ~]# ps aux|grep rsync
root 43015 0.0 0.0 107616 672 ? Ss 11:22 0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
root 43017 0.0 0.0 103248 836 pts/1 S+ 11:22 0:00 grep rsync
[root@mail ~]# netstat -tnlp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 43015/rsync
tcp 0 0 :::873 :::* LISTEN 43015/rsync
(2)客戶端(mail.eplantstore.com 10.188.1.86)
安裝rsync同步工具:
1
[root@mail ~]# yum install -y rsync
創(chuàng)建密碼文件:
1
2
3
4
[root@mail ~]# vi /etc/rsync.secrets
#只存放了一個密碼
123456
[root@mail ~]# chmod 600 /etc/rsync.secrets
設(shè)置計劃任務(wù),每10分鐘更新:
1
2
3
[root@mail ~]# crontab -e
*/10 * * * * rsync -vzrtopg --progress --password-file=/etc/rsync.secrets beifen@mail.yourmail.com::domains /home/domains
*/10 * * * * rsync -vzrtopg --progress --password-file=/etc/rsync.secrets beifen@mail.yourmail.com::mysql /var/lib/mysql/extmail
參數(shù)說明:
-v表示verbose詳細顯示
-z表示壓縮
-r表示recursive遞歸
-t表示保持原文件創(chuàng)建時間
-o表示保持原文件屬主
-p表示保持原文件的參數(shù)
-g表示保持原文件的所屬組
這里使用的用戶是beifen,和服務(wù)端配置文件中設(shè)置的一致,如果寫成其他的會報錯:
1
auth failed on module domains from mail.eplantstore.com (10.188.1.86): unauthorized user
(3)手動執(zhí)行同步
1
2
3
4
[root@mail extmail]# rsync -vzrtopg --progress --password-file=/etc/rsync.secrets beifen@mail.yourmail.com::domains /home/domains
#顯示一大堆文件傳輸過程
sent 4104 bytes received 9643567 bytes 1135020.12 bytes/sec
total size is 14251668 speedup is 1.48
可以看到第一次同步傳輸了9643567字節(jié)。
1
2
3
4
5
[root@mail extmail]# rsync -vzrtopg --progress --password-file=/etc/rsync.secrets beifen@mail.yourmail.com::domains /home/domains
receiving incremental file list
yourmail.com/test/Maildir/tmp/
sent 140 bytes received 14820 bytes 2720.00 bytes/sec
total size is 14251668 speedup is 952.65
可以看到第二次同步只更新了test用戶的臨時目錄,確認(rèn)是增量備份。
(4)查看日志
1
2
3
4
5
[root@mail ~]# tailf /var/log/rsyncd.log
2014/12/29 13:49:14 [43498] connect from mail.eplantstore.com (10.188.1.86)
2014/12/29 13:49:14 [43498] rsync on domains/ from beifen@mail.eplantstore.com (10.188.1.86)
2014/12/29 13:49:15 [43498] building file list
2014/12/29 13:49:17 [43498] sent 9643584 bytes received 4105 bytes total size 14251668
(5)檢查同步結(jié)果
1
2
3
4
5
[root@mail extmail]# ll /home/domains/
總用量 0
drwx------. 4 vmail vmail 34 11月 26 15:15 eplantstore.com
drwxr-xr-x. 3 vmail vmail 18 12月 11 16:58 seconed.cn
drwxr-xr-x. 4 vmail vmail 34 12月 1 16:24 yourmail.com
數(shù)據(jù)庫文件同步最好先刪除客戶端的再執(zhí)行同步,否則會因為占用問題部分同步不過來:
1
[root@mail extmail]# rm -f /var/lib/mysql/extmail/*
同步完成后,extman后臺就得用root@yourmail.com賬號登陸了,且eplantstore.com域沒有了;
現(xiàn)在完全跟服務(wù)器的數(shù)據(jù)一致了。
重要說明:最近發(fā)現(xiàn)郵件同步?jīng)]問題,但數(shù)據(jù)庫同步不行,還是得使用Mysql二進制文件同步的方法,接下來的文章將發(fā)布:Nginx負(fù)載均衡+(Nginx+FCGI)取代(Apahce+FCGI)(增加頁面緩存功能)+Unison雙向文件同步+Mysql同步,實現(xiàn)高性能、高可用性完美架構(gòu),有條件甚至可以郵箱和數(shù)據(jù)庫分離。