使用Linux的rsync命令实现 多服务器镜像同步(4)

2019-06-11 09:42

察看防火墻是否打開了873這個port

Step4:通过rsync客户端来同步数据

(1)列出rsync 服务器上的所提供的同步内容 #rsync --list-only linuxsir@linuxsir.org::

+++++++++++++++++++++++++++++++++ +++ +test227 rsync 2007-2008+++

+++++++++++++++++++++++++++++++++

oraclepackages oracle home

注: 前面是rsync 所提供的数据源,也就是我们在rsyncd.conf 中所写的[oraclepackages]模块。而―oracle home‖是由[oraclepackages]模块中的 comment =oracle home 提供的;为什么没有把beinan 数据源列出来呢?因为我们在[oraclehome]中已经把list=no了 (2)客戶端手工同步數據:

#rsync -avzP root@root.org::oraclepackages

Password: 这里要输入root的密码,是服务器端提供的,在前面的例子中,我们用的是root,输入的密码并不显示出来;输好后就回车;

注: 这个命令的意思就是说,用linuxsir 用户登录到服务器上,把linuxsirhome数据,同步到本地目录linuxsirhome上。当然本地的目录是可以你自己定义的,比如 linuxsir也是可以的;当你在客户端上,当前操作的目录下没有linuxsirhome这个目录时,系统会自动为你创建一个;当存在 linuxsirhome这个目录中,你要注意它的写权限

命令參數说明:

-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;

-z 传输时压缩; -P 传输进度;

-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;

如:$ rsync -avzP --delete linuxsir@linuxsir.org::linuxsirhome linuxsirhome 表示客户端上的数据要与服务器端完全一致,如果 linuxsirhome目录中有服务器上不存在的文件,则删除。最终目的是让linuxsirhome目录上的数据完全与服务器上保持一致

$rsync -avzP --delete --password-file=rsync.password linuxsir@linuxsir.org::linuxsirhome linuxsirhome

选项 --password-file=rsync.password ,这是当我们以linuxsir用户登录rsync服务器同步数据时,密码将读取 rsync.password 这个文件。这个文件内容只是linuxsir用户的密码,我们要如下做;

$ touch rsync.password $ chmod 600 rsync.passwod $ echo \

$ rsync -avzP --delete --password-file=rsync.password linuxsir@linuxsir.org::linuxsirhome linuxsirhome

注:这样就不需要密码了;其实这是比较重要的,因为服务器通过crond计划任务还是有必

要的;

(3)让rsync 客户端自动与服务器同步数据 第一步:创建同步脚本和密码文件

[root@test228 data]# mkdir /etc/cron.daily.rsync [root@test228 data]# cd /etc/cron.daily.rsync

[root@test228 cron.daily.rsync]# touch root.sh oracle.sh

[root@test228 cron.daily.rsync]# chmod 755 /etc/cron.daily.rsync/*.sh [root@test228 cron.daily.rsync]# mkdir /etc/rsyncd/

[root@test228 cron.daily.rsync]# touch /etc/rsyncd/rsyncroot.passwd

[root@test228 cron.daily.rsync]# touch /etc/rsyncd/rsyncoracle.passwd [root@test228 cron.daily.rsync]# chmod 600 /etc/rsyncd/rsync*

注: 我们在 /etc/cron.daily/ 中创建了两个文件oracle.sh和root.sh ,并且是权限是 755的。创建了两个密码文件,root用户用的是 rsyncroot.passwd ,而oracle用户用的是 rsyncoracle.password ,权限是600

[root@test228 cron.daily.rsync]# vi /etc/cron.daily.rsync/root.sh #!/bin/sh

#root.org home backup

/usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsyncroot.passwd root@10.161.32.227::oraclepac kages /u01/packages/$(date +'%m-%d-%y')

[root@test228 cron.daily.rsync]# vi /etc/cron.daily.rsync/oracle.sh #!/bin/sh

#oracle.org home backup

/usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsyncoracle.passwd oracle@10.161.32.227::oracl epackages /u01/packages/$(date +'%m-%d-%y')

注:你可以把root.sh 和oracle.sh 的内容合并到一个文件中,比如都写到 root.sh 中; 接着我们修改 /etc/rsyncd/rsyncroot.passwd 和 rsyncoracle.passwd的内容 [root@test228 cron.daily.rsync]# echo \

[root@test228 cron.daily.rsync]# echo \

然后我们再/home目录下创建root 和oracle两个目录(也可以在需要同步到目的文件夾下創建相關文件目錄),意思是服务器端的oraclepackages数据同步到备份服务器上的 /home/roothome下, oraclepackages(或相關的文件)数据同步到 /home/oraclehome/目录下。并按年月日归档创建目录;每天备份都存档; [root@test228 cron.daily.rsync]# mkdir /u01/packages

第二步:修改crond服务器的配置文件 [root@test228 cron.daily.rsync]# crontab -e

# Run daily cron jobs at 4:10 every day backup linuxsir data:

10 4 * * * /usr/bin/run-parts /etc/cron.daily.rsync 1> /dev/null 注:

第一行是注释,是说明内容,这样能自己记住。

第二行表示在每天早上4点10分的时候,运行 /etc/cron.daily.rsync 下的可执行脚本任务;

第三步:重启crond服务器;

配置好后,要重启crond 服务器

[root@test228 cron.daily.rsync]# killall crond 注:杀死crond 服务器的进程 [root@test228 cron.daily.rsync]# ps aux|grep crond 注:查看一下是否被杀死; root 8094 0.0 0.0 4016 616 pts/1 S+ 18:45 0:00 grep crond

[root@test228 cron.daily.rsync]# /usr/sbin/crond 注:启动 crond 服务器

[root@test228 cron.daily.rsync]# ps aux|grep crond 注:查看一下是否启动了? root 8096 0.0 0.0 6052 988 ? Ss 18:45 0:00 /usr/sbin/crond root 8098 0.0 0.0 3952 620 pts/1 S+ 18:46 0:00 grep crond

6 在 Windows 上使用 rsync 同步文件和目录

rsync 是一个 Unix 系统下的自由的计算机程序,它可以用来从一个地方同步文件和目录到另外一个地方,而且它使用一种 delta 编码方式来使得所需要传输的数据是尽可能的少。它被大量使用于站点镜像上。

这里介绍如何在 Windows 系统下来部署 rsync 服务器,并使用客户端同步文件和目录。

一.安装和部署服务器端

首先去下载一个为 Windows 平台开 发的 rsync server:cwRsync Server ,下载后安装和一般的 Windows 安装程序一样,一直点那个―下一步‖(Next)就完事了。然后修改一下配置,默认安装情况下,配置文件在 C:\\Program Files\\cwRsyncServer\\rsyncd.conf ,默认配置内容如下:

use chroot = false strict modes = false hosts allow = *

log file = rsyncd.log

pid file = rsyncd.pid

# Module definitions

# Remember cygwin naming conventions : c:work becomes /cygwin/c/work #

[test]

path = /cygdrive/c/work read only = false

transfer logging = yes

根 据这段配置你很容易做出自己的配置修改,特别注意的是要把路径 c:work 改成 /cygwin/c/work,修改完配置后,去 Windows 的服务管理器里面找到 RsyncServer 这一条,

把它的属性中的启动类型从―手动‖改为―自动,这样下次开机就会自动运行了,你也可以立即将该服务启动起来。

注意 rsync server 会监听 873 端口,所以如果你有开启防火墙,不要忘了开放 873 端口。

二.安装客户端

然后你可以安装一个客户端,cwRsync,也可以在刚才下载 cwRsync Server 的网站上找到,安装同样很简单,安装完成后建议你把 C:Program FilescwRsyncbin (默认安装情况下)加入到操作系统环境变量的 PATH 参数中去,然后就可以在命令行里用命令 rsync 来做同步等操作了: 1.打印帮助:

C:\\>rsycn --help

rsync version 2.6.9 protocol version 29

Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.

Capabilities: 64-bit files, socketpairs, hard links, symlinks, batchfiles, inplace, no IPv6, 64-bit system inums, 64-bit internal inums

rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.

rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm.

Usage: rsync [OPTION]... SRC [SRC]... DEST

or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST or rsync [OPTION]... [USER@]HOST:SRC [DEST] or rsync [OPTION]... [USER@]HOST::SRC [DEST]

or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect to an rsync daemon, and require SRC or DEST to start with a module name.

Options

-v, --verbose increase verbosity -q, --quiet suppress non-error messages

--no-motd suppress daemon-mode MOTD (see manpage caveat) -c, --checksum skip based on checksum, not mod-time & size -a, --archive archive mode; same as -rlptgoD (no -H) --no-OPTION turn off an implied OPTION (e.g. --no-D) -r, --recursive recurse into directories -R, --relative use relative path names

--no-implied-dirs don't send implied dirs with --relative

-b, --backup make backups (see --suffix & --backup-dir) --backup-dir=DIR make backups into hierarchy based in DIR --suffix=SUFFIX set backup suffix (default ~ w/o --backup-dir) -u, --update skip files that are newer on the receiver

--inplace update destination files in-place (SEE MAN PAGE) --append append data onto shorter files -d, --dirs transfer directories without recursing -l, --links copy symlinks as symlinks

-L, --copy-links transform symlink into referent file/dir --copy-unsafe-links only \--safe-links ignore symlinks that point outside the source tree -k, --copy-dirlinks transform symlink to a dir into referent dir -K, --keep-dirlinks treat symlinked dir on receiver as dir -H, --hard-links preserve hard links

-p, --perms preserve permissions

-E, --executability preserve the file's executability --chmod=CHMOD affect file and/or directory permissions -o, --owner preserve owner (super-user only) -g, --group preserve group

--devices preserve device files (super-user only) --specials preserve special files

-D same as --devices --specials -t, --times preserve times

-O, --omit-dir-times omit directories when preserving times --super receiver attempts super-user activities -S, --sparse handle sparse files efficiently

-n, --dry-run show what would have been transferred -W, --whole-file copy files whole (without rsync algorithm) -x, --one-file-system don't cross filesystem boundaries -B, --block-size=SIZE force a fixed checksum block-size

-e, --rsh=COMMAND specify the remote shell to use

--rsync-path=PROGRAM specify the rsync to run on the remote machine --existing skip creating new files on receiver

--ignore-existing skip updating files that already exist on receiver --remove-source-files sender removes synchronized files (non-dirs) --del an alias for --delete-during

--delete delete extraneous files from destination dirs --delete-before receiver deletes before transfer (default) --delete-during receiver deletes during transfer, not before --delete-after receiver deletes after transfer, not before

--delete-excluded also delete excluded files from destination dirs --ignore-errors delete even if there are I/O errors --force force deletion of directories even if not empty --max-delete=NUM don't delete more than NUM files


使用Linux的rsync命令实现 多服务器镜像同步(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:中外电影史试卷(B)_参考答案(新)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: