Chinaunix首页 | 论坛 | 博客
  • 博客访问: 320823
  • 博文数量: 32
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-30 10:47
文章分类

全部博文(32)

文章存档

2016年(2)

2015年(16)

2014年(14)

分类: LINUX

2014-02-19 16:04:46

Rsync

复制目录跟复制目录斜杠有区别的。

[root@localhost home]# rsync -av cacti  /tmp

building file list ... done

cacti/

cacti/.bash_logout

cacti/.bash_profile

cacti/.bashrc

cacti/.emacs

cacti/.kde/

cacti/.kde/Autostart/

cacti/.kde/Autostart/.directory

cacti/.mozilla/

cacti/.mozilla/extensions/

cacti/.mozilla/plugins/

 

sent 1752 bytes  received 166 bytes  3836.00 bytes/sec

total size is 1229  speedup is 0.64

[root@localhost home]# ls -ld /tmp

drwxrwxrwt 7 root root 4096 06-14 06:10 /tmp

[root@localhost home]# ls /tmp

cacti                     tcmalloc        tcmalloc.22393  tcmalloc.22815

gconfd-root               tcmalloc.22391  tcmalloc.22394  tcmalloc.22816

scim-panel-socket:0-root  tcmalloc.22392  tcmalloc.22814  tcmalloc.22817

[root@localhost home]# rsync -av cacti/  /tmp

building file list ... done

./

.bash_logout

.bash_profile

.bashrc

.emacs

.kde/

.kde/Autostart/

.kde/Autostart/.directory

.mozilla/

.mozilla/extensions/

.mozilla/plugins/

 

sent 1746 bytes  received 166 bytes  3824.00 bytes/sec

total size is 1229  speedup is 0.64

[root@localhost home]# ls /tmp

cacti                     tcmalloc        tcmalloc.22393  tcmalloc.22815

gconfd-root               tcmalloc.22391  tcmalloc.22394  tcmalloc.22816

scim-panel-socket:0-root  tcmalloc.22392  tcmalloc.22814  tcmalloc.22817

远程shell,把复制的目录拷贝到另一个系统上。

[root@localhost home]# rsync -av cacti 192.168.1.13:test

The authenticity of host '192.168.1.13 (192.168.1.13)' can't be established.

RSA key fingerprint is 3b:80:e1:a5:4c:c4:f9:32:f1:7a:c9:28:32:99:4c:3e.

Are you sure you want to continue connecting (yes/no)? Yes

Rsyncls命令相似?

[root@localhost home]# rsync -a 192.168.1.13:test

root@192.168.1.13's password: 

drwxr-xr-x        4096 2013/05/30 07:48:40 test

drwx------        4096 2013/04/12 18:55:20 test/cacti

一.实验:rsync容错实验

A192.168.1.163

B192.168.1.13

A的配置:默认情况下没有/etc/rsyncd.conf,需要手动创建;

uid = nobody //备份以什么身份进行,用户ID

gid = nobody //备份以什么身份进行,组ID
#注意这个用户ID和组ID,可以设置成root,这样rsync几乎就可以读取任何文件及目录,但是也带来安全隐患。建议设置成只能读取你要备份的目录和文件即可。

use chroot = no //是否限制在指定目录,为了安装,一般需要启动

max_connections = 10 //最大连接数,0表示没有限制

strict modes = yes //是否检查口令文件的权限
port = 873  //默认端口873

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

#定义模块

[name]   //这里是认证的模块名,在client端需要指定

path = /tmp //需要备份的目录,不可缺少

comment = name file  //这个模块的注释信息

ignore errors #忽略i/o错误

read only = on

write only = on

hosts allow = *

hosts deny = 192.168.1.14

list = false //需要建立隐藏文件设置false(不允许列文件)
#exclude = test/ test.php   //设置不同步的目录或文件用空格隔开 

uid = root

gid = root

auth users = cacti
#auth users = msyn  //认证的用户名,如果没有这行,则表明是匿名

secrets file = /etc/server.pass //认证文件名,用来存放密码      

创建/etc/server.pass;配置rsync密码(在上边的配置文件中已经写好路径) server.pas(名字随便写,只要和上边配置文件里的一致即可),格式(一行一个用户)
账号:密码
cacti:123

#需要将rsyncd.secrets更改权限。将rsyncd.secrets这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!
#执行命令:chmod 600 /etc/rsyncd/server/rsyncd.secrets

需要开启:rsyncd的守护进程

[root@localhost home]# /usr/local/bin/rsync --daemon

[root@localhost home]# ps -ef |grep rsync

root     31575     1  0 06:57 ?        00:00:00 /usr/local/bin/rsync --daemon
启动使rsync生效:/usr/local/bin/rsync --daemon  --config=/etc/rsyncd.conf;

Tip:这里的启动方式比较特殊,如果你要重启,需要kill掉rsync的进程,再重新运行!
另外还有另外一种启动rsync的方式,CentOS 默认以 xinetd 方式运行 rsync 服务。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync 
要配置以 xinetd 运行的 rsync 服务需要执行如下的命令: 


#配置文件xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon   --config=/etc/rsyncd.conf #这里添加rsync的服务选项
        log_on_failure  += USERID
}
~         

B:配置

[root@localhost home]# rsync -vzrtopg --delete --progress --exclude "*access" --exclude "debug*" cacti@192.168.1.163::name /home/tmp --password-file=/etc/server.pass

[root@localhost home]#chmod /etc/server.pass 

[root@localhost home]#chmod 600 /etc/server.pass 

同步结果:/home/tmp



-v --verbose详细模式输出;

-z  --compress,对传输时对备份的文件进行压缩处理;

-r --recursive 对子目录以递归模式处理 

-t --time 保持文件时间的信息

-o --owner 用来保持文件属主信息

-p --perms 用来保持文件权限

-g --group  用来保持文件的属组信息

--delete rsync服务器端为基准进行数据镜像同步、cs的目录要一直;

--progress 用于显示数据镜像同步的过程;

--exclude 用于排除不需要传输的文件类型

cacti@192.168.1.163::name 表示对服务器192.168.1.163中的name模块进行备份,也就是指定备份的模块。Cacti表示使用“cacti”这个用户对该模块进行备份。

/home/tmp 用于指定备份文件在客户端的存放路径

 

 

二.Rsyncinotify实时同步系统2.6.13以上的支持inotify

[root@localhost tmp]# uname -r

2.6.18-194.el5xen

[root@localhost tmp]# ll /proc/sys/fs/inotify

总计 0

-rw-r--r-- 1 root root 0 06-14 07:50 max_queued_events

-rw-r--r-- 1 root root 0 06-14 07:50 max_user_instances

-rw-r--r-- 1 root root 0 06-14 07:50 max_user_watches

安装文件:inotify

[root@localhost home]# tar -zxvf inotify-tools-3.14.tar.gz 

[root@localhost inotify-tools-3.14]# ./configure

[root@localhost inotify-tools-3.14]# make && make install 

安装之后会生成两个文件:

[root@localhost inotify-tools-3.14]# ll /usr/local/bin/inotifywa*

-rwxr-xr-x 1 root root 37236 06-14 08:06 /usr/local/bin/inotifywait

-rwxr-xr-x 1 root root 35426 06-14 08:06 /usr/local/bin/inotifywatch

Inotifywait用于等待文件或文件集上的一个特定事件,可以监控任何文件和目录设置,并且可以递归地监控整个目录树;inotifywait用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。

 

&配置内容发布节点;

#!/bin/bash

#!/bin/bash

host1=192.168.1.12

host2=192.168.1.13

host3=192.168.1.14

src=/web/wwwroot/

dst1=web1

dst2=web2

dst3=web3

user1=web1user

user2=web2user

user3=web3user

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,

delete,create,attrib $src \ 

 while read files

do

/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1

/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$host2::$dst2

/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user3@$host3::$dst3

echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

done     

阅读(2020) | 评论(0) | 转发(0) |
0

上一篇:NGINX的搭建

下一篇:lighttpd 搭建

给主人留下些什么吧!~~