Dec 26

[原]同步文件和数据库 阴

linuxing , 13:46 , 数据库 » Mysql , 评论(0) , 引用(0) , 阅读(22708) , Via 本站原创 | |
 为了防止blog主机出现故障导致文件丢失,我在整理了另外一台后备的服务器,下面就写写如何做它们的同步。
 遇到的问题有两个:1、主机是FreeBSD,备机是Linux,使用的命令和路径有不同;2、不单要同步文件,而且要同步mysql数据。

一、同步文件
 我使用rsync服务,主机做rsync服务器,备机做rsync客户机。关于rsync的详细命令可以参考[原]使用rsync命令同步数据
主机:
 FreeBSD系统,可以使用port安装rsync,安装完后的路径在/usr/local/下。
 修改/usr/local/etc/rsyncd.conf文件,增加:
引用
[test]
path = /usr/local/www/test/
comment = my blog data
uid = www
gid = www
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

  增加密码验证文件:

# cat /usr/local/etc/rsyncd.secrets
linuxing:test
# chown root:wheel /usr/local/etc/rsyncd.secrets
# chmod 600 /usr/local/etc/rsyncd.secrets

 启动服务
# rsync --daemon

# /usr/local/etc/rc.d/rsyncd.sh start

 查看是否已经打开服务(端口873)
# sockstat |grep 873
root     rsync      1147  4  tcp4   *:873                 *:*

# netstat -aln | grep 873
tcp4       0      0  *.873                  *.*                    LISTEN

 确认都正常的话,使到rsync服务开机自动启动,在/etc/rc.conf,增加rsyncd_enable="YES"。

备机:
 使用的是红旗DC Server 4.1 for x86系统,默认已经安装好rsync客户端,路径在/usr/bin/中。
 首先,添加密码验证文件
# mkdir /etc/rsyncd
# vi /etc/rsyncd/test.secrets
test

 然后,使用命令同步即可:
rsync -azv rsync://linuxing@www.linuxfly.org/test /var/www/html/test/ --password-file=/etc/rsyncd/test.secrets


二、同步mysql数据库
 因为Mysql服务使用Cache和缓冲区来提供对存储在磁盘上的数据库文件更新的效率,所以文件的内容和当前数据库的内容可能并不完全一致。而标准的备份程序仅仅包括对系统和数据文件的拷贝,这种对Mysql数据文件的备份并不能完全满足我们的需要,因为它不能保证拷贝的文件在系统崩溃时能够正常地使用。
 Mysql中的工具可以对数据进行实时的备份,而且不会影响服务的效率。
 Mysql数据库版本:5.0
  关于mysql 5.0使用日志恢复更详细的介绍,可以参考mysql 5.0更新日志的问题

主机:
1) 打开mysql的更新日志记录
# cp my-huge.cnf /var/db/mysql/my.cnf
# vi /var/db/mysql/my.cnf

 修改/var/db/mysql/my.cnf文件,把
引用
log-bin=mysql-bin

 改为
引用
log-bin=/usr/backups/mysql/mysql-bin

 建立拜访更新日志的路径:
# mkdir /usr/backups
# mkdir /usr/backups/mysql
# chown mysql:mysql /usr/backups/mysql

 重新启动mysql服务,使到其记录更新日志生效:
# /usr/local/etc/rc.d/mysql-server.sh restart

 这样数据库的更新日志就会放在/usr/backups/mysql目录中,我们下面就会使用它们。
 为了方便更新,我们在/usr/local/rsyncd.conf中建立第二个同步节点:
引用
[mysql]
path = /usr/backups/mysql/
comment = my blog mysql data
uid = mysql
gid = mysql
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

2)备份mysql数据库
# mysqldump -uroot -ppassword test > /usr/backups/mysql/test.sql
其中password是linuxing登陆mysql的密码,test是需要备份的数据库;

备机:
1)建立备份服务器的主数据库
# mkdir /root/mysql
# mysqladmin -uroot -ppassword create test
# rsync -azv rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets
# mysql -uroot -ppassword test < /root/mysql/test.sql

 到此,备份服务器已经建立了test数据库,而且内容为主服务器的内容。后面,我们只需要使用日志更新即可。
2)使用更新日志建立差异备份
 这样的操作是经常性的,所以我们可以放到计划任务里面。这里我假设一天备份一次:
# cd /etc/cron.daily
# vi mydata.cond
rsync -az rsync://linuxing@www.linuxfly.org/exblog /var/www/html/test --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
if [ $? -eq 0 ]; then
 echo `date` "rsync exblog data successfully!" >> /tmp/mydata.log
 chown -R apache.apache /var/www/html/test
 rsync -az rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
 if [ $? -eq 0 ]; then
 echo `date` "rsync mysql data successfully!" >> /tmp/mydata.log
 chown -R root.root /root/mysql
 mysql -ulinuxing -ppassword test < /root/mysql/test.sql
   if [ $? -eq 0 ]; then
   echo `date` "exblog backup database recover ok!" >> /tmp/mydata.log
   find /root/mysql -name "mysql-bin.0*"|xargs mysqlbinlog -d test|mysql -ulinuxing -ppassword
     if [ $? -eq 0 ]; then
     echo `date` "test database bin log recover ok!" >> /tmp/mydata.log
     fi
   fi
 fi
fi


※2005-12-26 第一次编写完成
※2005-12-27 修改使用mysqlbinlog实现日志更新恢复
 为了防止blog主机出现故障导致文件丢失,我在整理了另外一台后备的服务器,下面就写写如何做它们的同步。
 遇到的问题有两个:1、主机是FreeBSD,备机是Linux,使用的命令和路径有不同;2、不单要同步文件,而且要同步mysql数据。

一、同步文件
 我使用rsync服务,主机做rsync服务器,备机做rsync客户机。关于rsync的详细命令可以参考[原]使用rsync命令同步数据
主机:
 FreeBSD系统,可以使用port安装rsync,安装完后的路径在/usr/local/下。
 修改/usr/local/etc/rsyncd.conf文件,增加:
引用
[test]
path = /usr/local/www/test/
comment = my blog data
uid = www
gid = www
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

  增加密码验证文件:

# cat /usr/local/etc/rsyncd.secrets
linuxing:test
# chown root:wheel /usr/local/etc/rsyncd.secrets
# chmod 600 /usr/local/etc/rsyncd.secrets

 启动服务
# rsync --daemon

# /usr/local/etc/rc.d/rsyncd.sh start

 查看是否已经打开服务(端口873)
# sockstat |grep 873
root     rsync      1147  4  tcp4   *:873                 *:*

# netstat -aln | grep 873
tcp4       0      0  *.873                  *.*                    LISTEN

 确认都正常的话,使到rsync服务开机自动启动,在/etc/rc.conf,增加rsyncd_enable="YES"。

备机:
 使用的是红旗DC Server 4.1 for x86系统,默认已经安装好rsync客户端,路径在/usr/bin/中。
 首先,添加密码验证文件
# mkdir /etc/rsyncd
# vi /etc/rsyncd/test.secrets
test

 然后,使用命令同步即可:
rsync -azv rsync://linuxing@www.linuxfly.org/test /var/www/html/test/ --password-file=/etc/rsyncd/test.secrets


二、同步mysql数据库
 因为Mysql服务使用Cache和缓冲区来提供对存储在磁盘上的数据库文件更新的效率,所以文件的内容和当前数据库的内容可能并不完全一致。而标准的备份程序仅仅包括对系统和数据文件的拷贝,这种对Mysql数据文件的备份并不能完全满足我们的需要,因为它不能保证拷贝的文件在系统崩溃时能够正常地使用。
 Mysql中的工具可以对数据进行实时的备份,而且不会影响服务的效率。
 Mysql数据库版本:5.0
  关于mysql 5.0使用日志恢复更详细的介绍,可以参考mysql 5.0更新日志的问题

主机:
1) 打开mysql的更新日志记录
# cp my-huge.cnf /var/db/mysql/my.cnf
# vi /var/db/mysql/my.cnf

 修改/var/db/mysql/my.cnf文件,把
引用
log-bin=mysql-bin

 改为
引用
log-bin=/usr/backups/mysql/mysql-bin

 建立拜访更新日志的路径:
# mkdir /usr/backups
# mkdir /usr/backups/mysql
# chown mysql:mysql /usr/backups/mysql

 重新启动mysql服务,使到其记录更新日志生效:
# /usr/local/etc/rc.d/mysql-server.sh restart

 这样数据库的更新日志就会放在/usr/backups/mysql目录中,我们下面就会使用它们。
 为了方便更新,我们在/usr/local/rsyncd.conf中建立第二个同步节点:
引用
[mysql]
path = /usr/backups/mysql/
comment = my blog mysql data
uid = mysql
gid = mysql
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

2)备份mysql数据库
# mysqldump -uroot -ppassword test > /usr/backups/mysql/test.sql
其中password是linuxing登陆mysql的密码,test是需要备份的数据库;

备机:
1)建立备份服务器的主数据库
# mkdir /root/mysql
# mysqladmin -uroot -ppassword create test
# rsync -azv rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets
# mysql -uroot -ppassword test < /root/mysql/test.sql

 到此,备份服务器已经建立了test数据库,而且内容为主服务器的内容。后面,我们只需要使用日志更新即可。
2)使用更新日志建立差异备份
 这样的操作是经常性的,所以我们可以放到计划任务里面。这里我假设一天备份一次:
# cd /etc/cron.daily
# vi mydata.cond
rsync -az rsync://linuxing@www.linuxfly.org/exblog /var/www/html/test --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
if [ $? -eq 0 ]; then
 echo `date` "rsync exblog data successfully!" >> /tmp/mydata.log
 chown -R apache.apache /var/www/html/test
 rsync -az rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
 if [ $? -eq 0 ]; then
 echo `date` "rsync mysql data successfully!" >> /tmp/mydata.log
 chown -R root.root /root/mysql
 mysql -ulinuxing -ppassword test < /root/mysql/test.sql
   if [ $? -eq 0 ]; then
   echo `date` "exblog backup database recover ok!" >> /tmp/mydata.log
   find /root/mysql -name "mysql-bin.0*"|xargs mysqlbinlog -d test|mysql -ulinuxing -ppassword
     if [ $? -eq 0 ]; then
     echo `date` "test database bin log recover ok!" >> /tmp/mydata.log
     fi
   fi
 fi
fi


※2005-12-26 第一次编写完成
※2005-12-27 修改使用mysqlbinlog实现日志更新恢复
 为了防止blog主机出现故障导致文件丢失,我在整理了另外一台后备的服务器,下面就写写如何做它们的同步。
 遇到的问题有两个:1、主机是FreeBSD,备机是Linux,使用的命令和路径有不同;2、不单要同步文件,而且要同步mysql数据。

一、同步文件
 我使用rsync服务,主机做rsync服务器,备机做rsync客户机。关于rsync的详细命令可以参考[原]使用rsync命令同步数据
主机:
 FreeBSD系统,可以使用port安装rsync,安装完后的路径在/usr/local/下。
 修改/usr/local/etc/rsyncd.conf文件,增加:
引用
[test]
path = /usr/local/www/test/
comment = my blog data
uid = www
gid = www
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

  增加密码验证文件:

# cat /usr/local/etc/rsyncd.secrets
linuxing:test
# chown root:wheel /usr/local/etc/rsyncd.secrets
# chmod 600 /usr/local/etc/rsyncd.secrets

 启动服务
# rsync --daemon

# /usr/local/etc/rc.d/rsyncd.sh start

 查看是否已经打开服务(端口873)
# sockstat |grep 873
root     rsync      1147  4  tcp4   *:873                 *:*

# netstat -aln | grep 873
tcp4       0      0  *.873                  *.*                    LISTEN

 确认都正常的话,使到rsync服务开机自动启动,在/etc/rc.conf,增加rsyncd_enable="YES"。

备机:
 使用的是红旗DC Server 4.1 for x86系统,默认已经安装好rsync客户端,路径在/usr/bin/中。
 首先,添加密码验证文件
# mkdir /etc/rsyncd
# vi /etc/rsyncd/test.secrets
test

 然后,使用命令同步即可:
rsync -azv rsync://linuxing@www.linuxfly.org/test /var/www/html/test/ --password-file=/etc/rsyncd/test.secrets


二、同步mysql数据库
 因为Mysql服务使用Cache和缓冲区来提供对存储在磁盘上的数据库文件更新的效率,所以文件的内容和当前数据库的内容可能并不完全一致。而标准的备份程序仅仅包括对系统和数据文件的拷贝,这种对Mysql数据文件的备份并不能完全满足我们的需要,因为它不能保证拷贝的文件在系统崩溃时能够正常地使用。
 Mysql中的工具可以对数据进行实时的备份,而且不会影响服务的效率。
 Mysql数据库版本:5.0
  关于mysql 5.0使用日志恢复更详细的介绍,可以参考mysql 5.0更新日志的问题

主机:
1) 打开mysql的更新日志记录
# cp my-huge.cnf /var/db/mysql/my.cnf
# vi /var/db/mysql/my.cnf

 修改/var/db/mysql/my.cnf文件,把
引用
log-bin=mysql-bin

 改为
引用
log-bin=/usr/backups/mysql/mysql-bin

 建立拜访更新日志的路径:
# mkdir /usr/backups
# mkdir /usr/backups/mysql
# chown mysql:mysql /usr/backups/mysql

 重新启动mysql服务,使到其记录更新日志生效:
# /usr/local/etc/rc.d/mysql-server.sh restart

 这样数据库的更新日志就会放在/usr/backups/mysql目录中,我们下面就会使用它们。
 为了方便更新,我们在/usr/local/rsyncd.conf中建立第二个同步节点:
引用
[mysql]
path = /usr/backups/mysql/
comment = my blog mysql data
uid = mysql
gid = mysql
ignore errors
read only = yes
list = yes
auth users = linuxing
secrets file = /usr/local/etc/rsyncd.secrets

2)备份mysql数据库
# mysqldump -uroot -ppassword test > /usr/backups/mysql/test.sql
其中password是linuxing登陆mysql的密码,test是需要备份的数据库;

备机:
1)建立备份服务器的主数据库
# mkdir /root/mysql
# mysqladmin -uroot -ppassword create test
# rsync -azv rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets
# mysql -uroot -ppassword test < /root/mysql/test.sql

 到此,备份服务器已经建立了test数据库,而且内容为主服务器的内容。后面,我们只需要使用日志更新即可。
2)使用更新日志建立差异备份
 这样的操作是经常性的,所以我们可以放到计划任务里面。这里我假设一天备份一次:
# cd /etc/cron.daily
# vi mydata.cond
rsync -az rsync://linuxing@www.linuxfly.org/exblog /var/www/html/test --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
if [ $? -eq 0 ]; then
 echo `date` "rsync exblog data successfully!" >> /tmp/mydata.log
 chown -R apache.apache /var/www/html/test
 rsync -az rsync://linuxing@www.linuxfly.org/mysql /root/mysql --password-file=/etc/rsyncd/test.secrets >> /tmp/mydata.log
 if [ $? -eq 0 ]; then
 echo `date` "rsync mysql data successfully!" >> /tmp/mydata.log
 chown -R root.root /root/mysql
 mysql -ulinuxing -ppassword test < /root/mysql/test.sql
   if [ $? -eq 0 ]; then
   echo `date` "exblog backup database recover ok!" >> /tmp/mydata.log
   find /root/mysql -name "mysql-bin.0*"|xargs mysqlbinlog -d test|mysql -ulinuxing -ppassword
     if [ $? -eq 0 ]; then
     echo `date` "exblog database bin log recover ok!" >> /tmp/mydata.log
     fi
   fi
 fi
fi


※2005-12-26 第一次编写完成
※2005-12-27 修改使用mysqlbinlog实现日志更新恢复
※2005-12-31 修改日志更新脚本,使支持多日志文件
Tags: , , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]