rsync -vazu /var/www/ root@192.168.31.130:/var/www/ --delete rsync 配置手记 1、记得检查防火墙是否放行 873端口 2、检查selinux 是否关闭 3、检查rsync 是否 --daemon 启动模式 rsync命令的用法 在配置完rsync服务器后,就可以从客户端发出rsync命令来实现各种同步的操作。rsync有很多功能选项,下面就对介绍一下常用的选项: rsync的命令格式可以为: 1. rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST 2. rsync [OPTION]... [USER@]HOST:SRC DEST 3. rsync [OPTION]... SRC [SRC]... DEST 4. rsync [OPTION]... [USER@]HOST::SRC [DEST] 5. rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST 6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] rsync有六种不同的工作模式: 1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。 2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。 3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。 4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。 5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。 6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。 下面以实例来说明: # rsync -vazu -progress terry@192.168.100.21:/terry/ /home v详细提示 a以archive模式操作,复制目录、符号连接 z压缩 u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时 -progress指显示 以上命令是保持客户机192.168.100.90上的/home/terry目录和rsync服务器上的terry目录同步。该命令执行同步之前会要求你输入terry账号的密码,这个账号是我们前面在rsyncd.secrets文件中定义的。如果想将这条命令写到一个脚本中,然后定时执行它的话,可以使用--password-file选项,具体命令如下: # rsync -vazu -progress --password-file=/etc/rsync.secret terry@192.168.100.21:/terry/ /home 要使用--password-file选项,就得先建立一个存放密码的文件,这里指定为/etc/rsync.secret。其内容很简单,如下: terry:12345 同样要修改文件属性如下: # chmod 600 /etc/rsyncd.secrets 利用rsync保持Linux服务器间的文件同步实例 现在假设有两台Linux服务器A(192.168.100.21)和B(192.168.100.90),服务器A中的/home/terry和服务器B中的/home/terry这两个目录需要保持同步,也就是当服务器A中文件发生改变后, 服务器B中的文件也要对应去改变。 我们按上面的方法,在服务器A上安装rsync,并将其配置为一台rsync服务器,并将/home/terry目录配置成rsync共享出的目录。然后在服务器B上安装rsync,因为B只做客户端,所以无需配置。然后在服务器B,建立以下脚本: #!/bin/bash /usr/loca/rsync/bin/rsync -vazu -progress --delete --password-file=/etc/rsync.secret terry@192.168.100.21:/terry/ /home 将这个脚本保存为AtoB.sh,并加上可执行属性: # chmod 755 /root/AtoB.sh 然后,通过crontab设定,让这个脚本每30分钟运行一次。执行命令: # crontab -e 输入以下一行: 0,30 * * * * /root/AtoB.sh 保存退出,这样服务器B每个小时的0分和30分时都会自动运行一次AtoB.sh,AtoB.sh是负责保持服务器B和服务器A同步的。这样就保证了服务器A的所有更新在30钟后,服务器B也一样取得了和服务器A一样的最新的资料。 其它应用 rsync除了同步文件及目录之外,还可以利用它来实现对远程网站的远程备份。如果再结合脚本和Crontab就能实现定时自动远程备份。其可以实现与商业化的备份和镜象产品的类似效果,但完全免费 10.152上的/data/www/123/sns/* 下所有文件 覆盖到 本地/data/www/123/sns/* rsync -av -e ssh support@192.168.10.152:/data/www/123/sns/* /data/www/123/sns/
设置免登陆即可用rsync 通过守护进程方式同步数据 1. 定义主机的角色 rsync服务器端: IP 192.168.1.110 rsync 客户端: IP 192.168.1.112 2. Rsync 的安装 (Server 端 ) 先查看系统是否安装了 rsync 的软件包 [root@liyao ~]# rpm -qa | grep rsync rsync-2.6.8-3.1 # 这里证明我是已将安装好了得 如果没有安装的话,将光盘放入光驱,然后执行下面的命令 [root@liyao ~]# mount /dev/cdrom /mnt/ mount: block device /dev/cdrom is write-protected, mounting read-only [root@liyao ~]# [root@liyao ~]# rpm -ivh /mnt/CentOS/rsync-2.6.8-3.1.i386.rpm 3. 创建 /etc/rsyncd.conf 配置文件(安装好了该软件包之后是没有配置文件的,需要自己手动建立) [root@liyao ~]# vi /etc/rsyncd.conf ##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [liyao] path = /liyao/ ignore errors read only = false list = false hosts allow = 192.168.1.0/24 hosts deny = 0.0.0.0/32 auth users = backuser secrets file = /etc/rsync.password #_________________ 4. 如果配置中的 path = /liyao/ 提到的路径不存在则需要创建,命令为: [root@liyao ~]# mkdir /liyao [root@liyao ~]# chmod -R 755 /liyao/ 5. 配置用于rsync同步的账号、密码及账号文件权限
[root@liyao ~]# echo "backuser:123" >> /etc/rsync.password # 这里的 backuser 是 rsync 的账号,并没有登录系统的权限。123 是该用户的密码, 要注意的是:用户名和密码之间用分号隔开 [root@liyao ~]# chmod -R 600 /etc/rsync.password # 这个文件的权限必须为 600 6. 以守护进程方式来启动rsync服务,并将此服务设置成为开机自启动 [root@liyao ~]# rsync –daemon
[root@liyao ~]# netstat -npl | grep 873 # rsync 默认监听 873 端口,可以使用 netsat 查看该端口是否监听 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3863/rsync tcp 0 0 :::873 :::* LISTEN 3863/rsync 或者 [root@liyao ~]# ps -ef|grep rsync root 3863 1 0 05:57 ? 00:00:00 rsync --daemon
echo "/usr/bin/rsync --daemon" >>/etc/rc.local 或者 chkconfig rsync on 也没有问题 rsync客户端 (这些的操作都需要从 Client 端完成了) 7. 配置rsync账号及账号文件权限 [root@liyao ~]# echo "123" >> /etc/rsync.password # Client 只需输入密码,不需要输入用户名。但是必须保证密码与 Server 端一致 [root@liyao ~]# chmod -R 600 /etc/rsync.password # 这个文件的权限必须为 600
到此为止rsync 服务器配置已经完成了 ########################################################################## ##########################################################################
现在的事情就是在客户端进行测试了
推送(即从客户端拷贝文件到服务器端) 从客户端推动 /var 目录到服务器端 rsync 指定的目录 /liyao (在客户端操作)
[root@liyao ~]# rsync -avz --progress /var backuser@192.168.1.110::liyao/ --password-file=/etc/rsync.password building file list ... 399 files to consider var/ var/mail -> spool/mail var/account/ var/account/pacct 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=395/399) var/cache/ var/cache/coolkey/ var/cache/cups/ ####### 省略若干行######################### 这时可以到服务端指定的rscyn备份目录/oliyao下面已经有了var
[root@liyao ~]# ls -l /liyao/ total 4 drwxr-xr-x 22 root root 4096 Jul 3 21:42 var [root@liyao ~]#
[root@liyao ~]# rsync -avz --progress /var backuser@192.168.1.110::liyao/ --password-file=/etc/rsync.password @ERROR: auth failed on module liyao rsync error: error starting client-server protocol (code 5) at main.c(1296) [sender=2.6.8] # 出现这种问题的原因有以下几个 (1).auth users = backuser 的用户名和 /etc/rsync.password 中的用户名不一致
(2)secrets file = /etc/rsync.password 和存放用户名和密码的文件 /etc/rsync.password 的文件名不一致 (3) Server 和 Client 端 /etc/rsync.password 中的密码不一致
命令的详细说明: -avz : 保存文件原有属性的若干参数 --progress:显示出详细的进度情况 /var 要推送的目录 backuser@192.168.1.110::liyao/ backuser 为 rsync的用户后面为要推送的目的地的(rsync服务端)ip及目录,注意IP和目录间有两个冒号 --password-file=/etc/rsync.password 指定密码文件,确保无须交互式输入验证密码
拉取(即从服务器端拷贝文件到客户端) (从客户端操作) [root@liyao ~]# rsync -avz --progress backuser@192.168.1.110::liyao/ /mnt/ --password-file=/etc/rsync.password receiving file list ... 400 files to consider ./ var/ var/mail -> spool/mail var/account/ var/account/pacct 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=395/400) var/cache/ var/cache/coolkey/ var/cache/cups/ var/cache/fontconfig/ var/cache/fontconfig/0251a5afa6ac727a1e32b7d4d4aa7cf0-x86.cache-2 120 100% 117.19kB/s 0:00:00 (xfer#2, to-check=390/400) var/cache/fontconfig/1e88f1f1e0efe03f58dd40df4dd7a0ea-x86.cache-2 64328 100% 30.67MB/s 0:00:00 (xfer#3, to-check=389/400) ############### 省略若干行 ###############################
查看下 Client 的 /mnt 目录 [root@liyao ~]# ls -l /mnt/ total 4 drwxr-xr-x 22 root root 4096 Jul 3 21:42 var [root@liyao ~]#
#####################################################################################################################################
测试拉去文件方式同步并排除指定目录和文件 1. 在服务器端创建 a b c d 目录,目录下分别创建文件 1 2 3 4 /mnt目录为 rsync 服务指定的备份目录 [root@liyao ~]# rm -rf a b c d [root@liyao ~]# cd /liyao/ [root@liyao liyao]# mkdir a b c d [root@liyao liyao]# touch a/1 b/2 c/3 d/4 [root@liyao liyao]# ls -l a total 0 -rw-r--r-- 1 root root 0 Aug 16 07:31 1 [root@liyao liyao]# ls -l b total 0 -rw-r--r-- 1 root root 0 Aug 16 07:31 2 [root@liyao liyao]# ls -l c total 0 -rw-r--r-- 1 root root 0 Aug 16 07:31 3 [root@liyao liyao]# ls -l d total 0 -rw-r--r-- 1 root root 0 Aug 16 07:31 4 [root@liyao liyao]# (1) 测试拉取文件方式同步并排除指定目录和文件 在rsync客户端 ,拉取文件(即从服务器端拷贝文件到客户端) 我们要排除a c 目录(包括内部文件)及b目录下的2文件,其余的都同步。 方法一: [root@liyao ~]# rsync --exclude=a --exclude=b/2 --exclude=c -avz --progress backuser@192.168.1.110::liyao/ /mnt/ --password-file=/etc/rsync.password receiving file list ... 403 files to consider ./ b/ d/ d/4 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=399/403)
sent 168 bytes received 8894 bytes 18124.00 bytes/sec total size is 35584452 speedup is 3926.78 [root@liyao ~]#
[root@liyao ~]# ls -l /mnt/ total 12 drwxr-xr-x 2 root root 4096 Aug 16 07:31 b drwxr-xr-x 2 root root 4096 Aug 16 07:31 d drwxr-xr-x 22 root root 4096 Jul 3 21:42 var [root@liyao ~]#
[root@liyao ~]# tree /mnt/ /mnt/ |-- b |-- d | `-- 4 通过tree命令可以清晰的看出仅 b目录本身 和d目录及内部的文件同步过来了。这正式我们想要的结果。 方法二: 方法二:通过在服务器端配置rsyncd.conf参数实现 vi /etc/rsyncd.conf 修改内容为如下配置 [root@liyao liyao]# vi /etc/rsyncd.conf
##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [liyao] path = /liyao/ ignore errors read only = false list = false hosts allow = 192.168.1.0/24 hosts deny = 0.0.0.0/32 auth users = backuser secrets file = /etc/rsync.password exclude= a c b/2 # 仅增加了一行 内容为:exclude=a c b/2 ,exclude为排除目录或文件的参数,表示排除a c 目录及b目录的2文件 #____________________________end 保存配置,重新启动 rsync 服务 客户端清空 /mnt 下的文件,重新执行拉去命令 [root@liyao mnt]# rsync -avz --progress backuser@192.168.1.110::liyao/ /mnt/ --password-file=/etc/rsync.password receiving file list ... 4 files to consider ./ b/ d/ d/4 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/4)
sent 145 bytes received 201 bytes 692.00 bytes/sec total size is 0 speedup is 0.00 [root@liyao mnt]#
[root@liyao mnt]# tree . |-- b `-- d `-- 4
2 directories, 1 file
[root@liyao mnt]# # 同样可以实现
#####################################################################################################################################
我们再来测试一下推送
方法一:通过在服务器端配置rsyncd.conf参数实现
其实就是以上方法二 rsyncd.conf已经配置了exclude=a c b/2参数 客户端执行推送 [root@liyao mnt]# rsync -avzrtopg --progress /mnt backuser@192.168.1.110::liyao/ --password-file=/etc/rsync.password building file list ... 4 files to consider mnt/ mnt/b/ mnt/d/ mnt/d/4 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/4)
sent 138 bytes received 56 bytes 388.00 bytes/sec total size is 0 speedup is 0.00 [root@liyao mnt]#
上面 rsync -avzrtopg --progress /mnt/ ...中的/mnt/ 结尾带"/"斜线和不带"/"斜线,区别非常大。
#如使用/mnt 表示将mnt整个目录同步过去,而使用 /mnt/ 表示将mnty内的文件和目录同步,mnt目录本身不同步
在 Server 端查看
[root@liyao liyao]# tree . |-- b `-- d `-- 4
2 directories, 1 file [root@liyao liyao]#
方法二:通过rsync客户端加参数实现推送文件排除目录及文件同步。
#依然要排除a c 目录(包括内部文件)及b目录下的2文件,其余的都同步
1.取消或注释掉rsyncd.conf中的exclude=a c b/2 一行,然后保存重起rsync守护进程。
2.删除/oldboy目录下全部内容,方便观察。 root@liyao liyao]# vi /etc/rsyncd.conf
##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [liyao] path = /liyao/ ignore errors read only = false list = false hosts allow = 192.168.1.0/24 hosts deny = 0.0.0.0/32 auth users = backuser secrets file = /etc/rsync.password #exclude= a c b/2 #____________________________end
###############################################################################
~ [root@liyao mnt]# rsync --exclude=a --exclude=b/2 --exclude=c -avzrtopg --progress /mnt backuser@192.168.1.110::liyao/ --password-file=/etc/rsync.password building file list ... 4 files to consider mnt/ mnt/b/ mnt/d/ mnt/d/4 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/4)
sent 138 bytes received 56 bytes 129.33 bytes/sec total size is 0 speedup is 0.00 [root@liyao mnt]#
再来到 Server 端查看 [root@liyao liyao]# tree . `-- mnt |-- b `-- d `-- 4
3 directories, 1 file [root@liyao liyao]#
scp(ssh key)实现网站数据定时备份方案
网站的目录为 /var/www/html [root@liyao ~]# tree /var/www/html/ /var/www/html/ |-- access_20110810.log |-- access_20110811.log |-- access_20110812.log |-- access_20110813.log `-- access_20110814.log
0 directories, 5 files 实验要求:
假定有两台服务器 一台是web服务器(有日志和程序) 一台是备份服务器。 1.实现每日0点推送网站访问日志到日志备份服务器(自己指定服务器即可),本地仅保留7天访问日志。防止硬盘被撑暴。 2.实现每日0点备份网站程序,并把程序推送到自己指定服务器同上即可,要求按天生成备份文件)。
实验中的地址分配: Apache 服务器: 192.168.1.110 Backup server: 192.168.1.112
在备份服务器上生成 Public key(公钥) 和 Private key (私钥) Private key
[root@liyao ~]# ssh-keygen -t dsa Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_dsa. Your public key has been saved in /root/.ssh/id_dsa.pub. The key fingerprint is: 76:68:39:7c:00:b3:f5:a7:af:8c:bc:fd:36:60:0a:dc root@liyao
将公钥拷贝到 apache 服务器端
[root@liyao .ssh]# ssh-copy-id -i id_dsa.pub root@192.168.1.110 10 The authenticity of host '192.168.1.110 (192.168.1.110)' can't be established. RSA key fingerprint is 94:5f:69:fb:78:5c:a4:01:4b:b4:0d:4e:c2:68:43:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.110' (RSA) to the list of known hosts. Address 192.168.1.110 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.110's password: Now try logging into the machine, with "ssh 'root@192.168.1.110'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@liyao .ssh]#
测试 1.112 是否可以不用输入密码可以登录到 1.110
[root@liyao .ssh]# ssh 192.168.1.110 Address 192.168.1.110 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! Last login: Thu Aug 18 06:13:53 2011 from 192.168.1.102 [root@liyao ~]#
OK 我们成功过了
Backup 服务器同样的把公钥也发给自己一份
[root@liyao .ssh]# ssh-copy-id -i id_dsa.pub root@192.168.1.112 10 The authenticity of host '192.168.1.112 (192.168.1.112)' can't be established. RSA key fingerprint is 94:5f:69:fb:78:5c:a4:01:4b:b4:0d:4e:c2:68:43:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.112' (RSA) to the list of known hosts. Address 192.168.1.112 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.112's password: Now try logging into the machine, with "ssh 'root@192.168.1.112'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@liyao .ssh]#
在把私钥通过 scp 命令拷贝到 apache 服务器端的 /root/.ssh 目录下,让apache端也拥有私钥
[root@liyao .ssh]# scp id_dsa root@192.168.1.110:/root/.ssh/ Address 192.168.1.110 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! id_dsa 100% 668 0.7KB/s 00:00 [root@liyao .ssh]#
这样我们从 1.110 登录 1.112 也是不需要密码的
[root@liyao .ssh]# ssh 192.168.1.112 The authenticity of host '192.168.1.112 (192.168.1.112)' can't be established. RSA key fingerprint is 94:5f:69:fb:78:5c:a4:01:4b:b4:0d:4e:c2:68:43:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.112' (RSA) to the list of known hosts. Address 192.168.1.112 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! Last login: Thu Aug 18 06:14:09 2011 from 192.168.1.102 [root@liyao ~]#
到此为止我们已经完成了免密码的交互式登录
用crontab –e 按题目要求编辑定时任务
[root@liyao ~]# crontab -e
00 00 * * * scp -P22 -r /var/www/html root@192.168.1.112:/tmp >/dev/null 2>&1 # 实现每日0点推送网站访问日志到日志备份服务器 59 23 * * * find /var/www/html -type f -mtime +7 |xargs rm -f >/dev/null 2>&1
sersync 配置 |
|