數據備份方案
1、需要備份的文件目錄有(原則上,只要運維人員寫入或更改的數據都需要備份)。/data,/etc/rc.local,/var/spool/cron/root等,根據不同都服務器做不同的調整
2、為了規范化,每台服務器進行本地備份時都備份到/backup目錄下
3、每台WEB服務器進行本地備份時,都備份到/backup目錄下以本機IP地址命名的目錄中
4、打的tar包中需要包含當天的日期
5、統一存儲數據備份的服務器統一采用Rsync daemon 方式提供存儲備份數據都目錄/backup
6、由於WEB服務器本地存儲空間有限,需要將超過7天的備份數據刪除
7、為了方便知道每次備份是否成功,我們需要做如下操作:
(1、在每台服務器上檢查備份是否成功
(2、在存儲備份數據的服務器上檢查備份數據是否推送成功,並發送郵件到管理員郵箱
8、因為備份服務器空間有限,需要刪除超過180天的備份數據,但每周六備份的數據要永久保留
做備份服務需要的基本步驟有:
1、 在Rsync備份服務器上安裝,配置Rsync服務,實現推送
2、 在客戶端服務器上,實現打包,推送,刪除,定時任務推送
3、 在Rsync備份服務器上,做檢查,發郵件給管理員
Rsync服務端搭建
1、查看服務是否存在
rpm -qa rsync
2、如果不存在,安裝rsync
yum install -y rsync
3、配置rsync的配置文件/etc/rsyncd.conf
vim /etc/rsyncd.conf
uid = rsync # 同步的用戶
gid = rsync # 同步的組
address =172.16.1.15 # 本機IP
port =873 # 本機端口
hosts allow =172.16.1.0/24 # 允許連接的客戶端
use chroot = no # 是否固定目錄
max connections = 50 # 允許的最大連接數
pid file =/var/run/rsyncd.pid # pid文件
lock file =/var/run/rsync.lock # lock文件
log file =/var/log/rsyncd.log # 日志文件
motd file =/etc/rsyncd.motd # 提示信息
[backup] # 模塊
path =/backup # 備份到的目錄
comment = used for web-data root
read only = false # 是否只讀
list = yes
auth users = rsyncuser # 同步的認證用戶
secrets file =/etc/rsyncd.passwd # 認證的用戶名和密碼
4、添加用戶名和密碼
echo "rsyncuser:password" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd # 只能是600權限
5、創建備份用戶,如果存在此步略
useradd rsync -s /sbin/nologin –M
6、啟動
rsync --daemon --config=/etc/rsyncd.conf
啟動完后可以看本機873端口是否存在。
Rsync客戶端搭建
安裝rsync步驟和上面一樣。
1、安裝,略
2、創建密碼文件
echo "password" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd
3、可以創建一個目錄進行測試
mkdir /backup
touch /backup/file{1..10}
rsync -avz /backup/ rsyncuser@172.16.1.15::backup --password-file=/etc/rsyncd.passwd
SeRsync實時同步
1、下載SeRsync
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
2、解壓安裝包
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
3、創建需要的目錄
mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/logs
mkdir /usr/local/sersync/bin
4、拷貝配置文件和啟動腳本到創建的目錄中
mv GNU-Linux-x86/sersync2 /usr/local/sersync/bin/
mv GNU-Linux-x86/confxml.xml /usr/local/sersync/conf/
5、配置配置文件confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<!-- 本機IP地址和端口 -->
<host hostip="172.16.1.16" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<!-- 監聽目錄,該目錄下文件發生變化,就觸發同步 -->
<localpath watch="/backup/">
<!-- 服務端IP和模塊名 -->
<remote ip="172.16.1.15" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!-- rsync的參數 -->
<commonParams params="-avrtuz"/>
<!-- 認證用戶和密碼文件 -->
<auth start="true" users="rsyncuser" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="true" port="873"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<!-- 以下配置可以不管 -->
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
6、啟動
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml