Rsnapshot备份工具

Rsnapshot是用perl采用perl语言,利用rsync和ssh功能来实现本地或远程的备份工具,它具有增量备份功能。



1、安装
在RHEL/CentOS系统环境需要EPEL源支持
RHEL/CentOS 7 64 Bit

## RHEL/CentOS 7 64-Bit ##
# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm
# rpm -ivh epel-release-7-6.noarch.rpm
RHEL/CentOS 6 32-64 Bit

## RHEL/CentOS 6 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm

## RHEL/CentOS 6 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm
RHEL/CentOS 5 32-64 Bit

## RHEL/CentOS 5 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

## RHEL/CentOS 5 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

# yum install rsnapshot

Debian/Ubuntu/环境直接使用一下命令
# apt-get install rsnapshot

2、rsnapshot需要ssh密钥认证支持,所以需提前完成服务器间的密钥认证。

3、调整配置文件

# vi /etc/rsnapshot.conf
#修改默认备份目录
snapshot_root            /data/backup/
#去掉cmd_ssh前的注释,已使用ssh传输
cmd_ssh         /usr/bin/ssh

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################
#备份周期和份数
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 3

#ssh 端口
ssh_args        -p 22

#备份过滤
exclude_file    /data/backup/tecmint.exclude

#本机的备份,localhost位于定义的备份目录之内
backup      /tecmint/       localhost/
backup      /etc/           localhost/

#远程备份
backup       root@example.com:/home/        /data/backup/

# vi /data/backup/tecmint.exclude
#目录内的文件目录级别
+ /boot
+ /data
+ /tecmint
+ /etc
+ /home
+ /opt
+ /root
+ /usr
- /usr/*
- /var/cache
+ /var
- /*


#测试配置文件
# rsnapshot configtest

测试小时备份配置
# rsnapshot -t hourly
Sample Output

echo 2028 > /var/run/rsnapshot.pid 
mkdir -m 0700 -p /data/backup/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local /data/backup/hourly.0/localhost/ 
touch /data/backup/hourly.0/
Note: The above command tells rsnapshot to create an “hourly” backup. It actually prints out the commands that it will perform when we execute it really.

进行备份
# rsnapshot hourly

备份结果
# cd /data/backup
# ls -l

total 4
drwxr-xr-x 3 root root 4096 Oct 28 09:11 hourly.0

计划任务

0     */4    * * *    root    /usr/bin/rsnapshot hourly
30     3     * * *    root    /usr/bin/rsnapshot daily
0      3     * * 1    root    /usr/bin/rsnapshot weekly
30     2     1 * *    root    /usr/bin/rsnapshot monthly
Let me explain exactly, what above cron rules does:

Runs every 4 hours and creates an hourly directory under /backup directory.
Runs daily at 3:30am and create a daily directory under /backup directory.
Runs weekly on every Monday at 3:00am and create a weekly directory under /backup directory.
Runs every monthly at 2:30am and create a monthly directory under /backup directory.
To better understand on how cron rules works, I suggest you read our article that describes.


#备份报告
# cp /usr/share/doc/rsnapshot-1.3.1/utils/rsnapreport.pl /usr/local/bin
# chmod +x /usr/local/bin/rsnapreport.pl

#需要修改配置文件如下形式
vi /etc/rsnapshot.conf

verbose         4
rsync_long_args --stats --delete        --numeric-ids   --delete-excluded


#可以重新定义备份并发送报告,记得定义mailx发送命令,不然可能发不出去哦
0     */4    * * *    root    /usr/bin/rsnapshot hourly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Hourly Backup" yourname@email.com
30     3     * * *    root    /usr/bin/rsnapshot daily 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Daily Backup" yourname@email.com
0      3     * * 1    root    /usr/bin/rsnapshot weekly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Weekly Backup" yourname@email.com
30     2     1 * *    root    /usr/bin/rsnapshot monthly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Montly Backup" yourname@email.com

#报告样式如下
SOURCE           TOTAL FILES    FILES TRANS TOTAL MB    MB TRANS   LIST GEN TIME  FILE XFER TIME
--------------------------------------------------------------------------------------------------------
localhost/          185734     11853     2889.45    6179.18    40.661 second   0.000 seconds

制作本地yum源

制作本地yum源

###########rsync 5 6 7 centos
/usr/bin/rsync -vaHP  --exclude=2*/ --exclude=3*/ --exclude=4*/ --exclude=debug/ --exclude=testing/ --exclude=isos/ --exclude=SRPMS/ --exclude=ppc*/ --delete-excluded --numeric-ids --delete --delete-after --delay-updates rsync://rsync.mirrors.ustc.edu.cn/centos/ /var/www/html/yum/centos/ 

###########rsync 5 6 7 epel
/usr/bin/rsync -vaHP  --exclude=4/ --exclude=4AS/ --exclude=4ES/ --exclude=4WS/ --exclude=testing/ --exclude=debug/  --exclude=isos/ --exclude=SRPMS/ --exclude=ppc*/ --delete-excluded --numeric-ids --delete --delete-after --delay-updates rsync://rsync.mirrors.ustc.edu.cn/epel/ /var/www/html/yum/epel/

##########rsync 5 6 7 rpmforge
/usr/bin/rsync -vaHP  --exclude=ppc/ --exclude=el2.1/ --exclude=el3/ --exclude=el4/  --exclude=ppc64/ --exclude=el5/ --exclude=el5/  --exclude=debug/  --exclude=isos/ --exclude=SRPMS/ --exclude=ppc/ --delete-excluded --numeric-ids --delete --delete-after --delay-updates rsync://apt.sw.be/pub/freshrpms/pub/dag/redhat/ /var/www/html/yum/rpmforge/ 

repo示列

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - yum.tuotuode.com
baseurl=http://yum.tuotuode.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://yum.tuotuode.com/centos/RPM-GPG-KEY-CentOS-6
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - yum.tuotuode.com
baseurl=http://yum.tuotuode.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://yum.tuotuode.com/centos/RPM-GPG-KEY-CentOS-6
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - yum.tuotuode.com
baseurl=http://yum.tuotuode.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://yum.tuotuode.com/centos/RPM-GPG-KEY-CentOS-6
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - yum.tuotuode.com
baseurl=http://yum.tuotuode.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://yum.tuotuode.com/centos/RPM-GPG-KEY-CentOS-6
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - yum.tuotuode.com
baseurl=http://yum.tuotuode.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://yum.tuotuode.com/centos/RPM-GPG-KEY-CentOS-6

HP服务器硬件检测命令包下载地址

1、hp-health
http://downloads.linux.hpe.com/SDR/downloads/servicepackforproliant/RedHat/6/x86_64/current/hp-health-9.50-1628.32.rhel6.x86_64.rpm

[root@localhost ~]# rpm -ivh hp-health-9.50-1628.32.rhel6.x86_64.rpm 
warning: hp-health-9.50-1628.32.rhel6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 5ce2d476: NOKEY
Preparing...                ########################################### [100%]
   1:hp-health              ########################################### [100%]
Please read the Licence Agreement for this software at

         /opt/hp/hp-health/hp-health.license

By not removing this package, you are accepting the terms
of the "HP Proliant Essentials Software End User License Agreement".
The hp-health RPM has installed successfully.

[root@localhost ~]# /etc/init.d/hp-health start
  Using Proliant Standard
        IPMI based 1XX System Health Monitor
  Using standard Linux IPMI device driver
WARNING: Deprecated config file /etc/modprobe.conf, all config files belong into /etc/modprobe.d/.
WARNING: Deprecated config file /etc/modprobe.conf, all config files belong into /etc/modprobe.d/.
  Starting Proliant Standard
        IPMI based 1XX System Health Monitor (hpasmpld): 
                                                           [  OK  ]
[root@localhost ~]# hpasmcli -s 'show'         

Invalid Arguments
         SHOW ASR
         SHOW DIMM
         SHOW FANS
         SHOW HT
         SHOW NAME
         SHOW PORTMAP
         SHOW POWERMETER
         SHOW POWERSUPPLY
         SHOW SEL
         SHOW SERVER
         SHOW TEMP
         SHOW TPM
         SHOW UID

[root@localhost ~]# hpasmcli -s 'show dimm'

Cartridge #:    0
Processor #:    1
Module #:       3
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    1
Module #:       2
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    1
Module #:       6
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    1
Module #:       5
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    1
Module #:       9
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    1
Module #:       8
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       3
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       2
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       6
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       5
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       9
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A

Cartridge #:    0
Processor #:    2
Module #:       8
Present:        Yes
Form Factor:    fh
Memory Type:    5h
Size:           4096 MB
Speed:          1333 MHz
Status:         N/A


[root@localhost ~]# hpasmcli -s 'show temp'          

Sensor   Location              Temp       Threshold
------   --------              ----       ---------
#1        MEMORY_BD             -         78C/172F 
#2        MEMORY_BD            32C/89F    87C/188F 
#3        MEMORY_BD            32C/89F    87C/188F 
#4        MEMORY_BD             -         78C/172F 
#5        MEMORY_BD            32C/89F    78C/172F 
#6        MEMORY_BD            33C/91F    78C/172F 
#7        MEMORY_BD             -         78C/172F 
#8        MEMORY_BD            32C/89F    78C/172F 
#9        MEMORY_BD            32C/89F    78C/172F 
#10       MEMORY_BD            30C/86F    85C/185F 
#11       MEMORY_BD             -         78C/172F 
#12       MEMORY_BD            35C/95F    78C/172F 
#13       MEMORY_BD            34C/93F    78C/172F 
#14       MEMORY_BD             -         78C/172F 
#15       MEMORY_BD            36C/96F    78C/172F 
#16       MEMORY_BD            36C/96F    78C/172F 
#17       MEMORY_BD             -         78C/172F 
#18       MEMORY_BD            36C/96F    78C/172F 
#19       MEMORY_BD            37C/98F    78C/172F 
#20       MEMORY_BD            30C/86F    85C/185F 
#21       AMBIENT              26C/78F    42C/107F 
#22       SYSTEM_BD            40C/104F   70C/158F 
#23       SYSTEM_BD            39C/102F   63C/145F 
#24       SYSTEM_BD            37C/98F    71C/159F 
#25       SYSTEM_BD            28C/82F    58C/136F 
#26       SYSTEM_BD            31C/87F    68C/154F 
#27       SYSTEM_BD            36C/96F    66C/150F 
#28       SYSTEM_BD            31C/87F    58C/136F 
#29       SYSTEM_BD            32C/89F    58C/136F 
#30       SYSTEM_BD            36C/96F    76C/168F 
#31       SYSTEM_BD            37C/98F    76C/168F 
#32       SYSTEM_BD            34C/93F    76C/168F 
#33       SYSTEM_BD            30C/86F    71C/159F 
#34       SYSTEM_BD            30C/86F    71C/159F 
#35       SYSTEM_BD            30C/86F    71C/159F 
#36       SYSTEM_BD            34C/93F    68C/154F 
#37       SYSTEM_BD            31C/87F    68C/154F 
#38       SYSTEM_BD            44C/111F   110C/230F


[root@localhost ~]# hpasmcli -s 'show server'

System        : ProLiant DL160 G6  
Serial No.    : xxxxxxxxxx
ROM version   : O33 05/01/2012
iLo present   : No
Embedded NICs : 2
        NIC1 MAC: 18:a9:05:6f:fd:5c
        NIC2 MAC: 18:a9:05:6f:fd:5d

Processor: 0
        Name         : Intel Xeon
        Stepping     : 5
        Speed        : 2266 MHz
        Bus          : 532 MHz
        Core         : 4
        Thread       : 8
        Socket       : 1
        Level1 Cache : 256 KBytes
        Level2 Cache : 1024 KBytes
        Level3 Cache : 8192 KBytes
        Status       : Ok

Processor: 1
        Name         : Intel Xeon
        Stepping     : 5
        Speed        : 2266 MHz
        Bus          : 532 MHz
        Core         : 4
        Thread       : 8
        Socket       : 2
        Level1 Cache : 256 KBytes
        Level2 Cache : 1024 KBytes
        Level3 Cache : 8192 KBytes
        Status       : Ok

Processor total  : 2

Memory installed : 49152 MBytes
ECC supported    : Yes

[root@localhost ~]# hpasmcli -s 'show fans'  

Fan  Location        Present Speed  of max  Redundant  Partner  Hot-pluggable
---  --------        ------- -----  ------  ---------  -------  -------------
#1   SYSTEM          Yes     NORMAL  29%     Yes        0        No            
#2   SYSTEM          Yes     NORMAL  37%     Yes        0        No            
#3   SYSTEM          Yes     NORMAL  29%     Yes        0        No            
#4   SYSTEM          Yes     NORMAL  37%     Yes        0        No            
#5   SYSTEM          Yes     NORMAL  37%     Yes        0        No            
#6   SYSTEM          Yes     NORMAL  29%     Yes        0        No            
#7   SYSTEM          Yes     NORMAL  41%     Yes        0        No            
#8   SYSTEM          Yes     NORMAL  39%     Yes        0        No            
#9   SYSTEM          Yes     NORMAL  30%     Yes        0        No            
#10  SYSTEM          Yes     NORMAL  28%     Yes        0        No            
#11  SYSTEM          Yes     NORMAL  27%     Yes        0        No            
#12  SYSTEM          Yes     NORMAL  22%     Yes        0        No            


[root@localhost ~]# 

2、hpacucli
http://ftp.hp.com/pub/softlib2/software1/pubsw-linux/p1257348637/v80070/hpacucli-9.40-12.0.x86_64.rpm

#通过hpacucli ctrl all show config命令查看raid卡信息和磁盘状态

#通过hpacucli ctrl all show config detail命令详细地查看RAID和硬盘的信息

3、repo
https://github.com/y-ken/setup-server-hp

4、HP repo制作
http://downloads.linux.hpe.com/SDR/getting_started.html

Note : Please follow good mirroring and network usage practices, and use efficient tools like rsync or mirror to only transfer what has been updated..

For example you can mirror the entrie Software Delivery Repository with the following command:
   rsync -avH rsync.linux.hpe.com::SDR <MyLocalMirorrPrefix>

To rsync a specific (spp) repository:
   rsync -avH rsync.linux.hpe.com::SDR/repo/spp <MyLocalMirorrPrefix>

To rsync the Firmware Pack for ProLiant reposoitory (fwpp) (authentication TOKEN is required):
   rsync -avH <TOKEN>@rsync.linux.hpe.com::FIRMWARE/repo/fwpp <MyLocalMirorrPrefix>

基于ssh的rsync拷贝

rsync 常用的运行参数说明:

-v, –verbose 详细模式输出
-c, –checksum 打开校验开关,强制对文件传输进行校验
-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, –recursive 对子目录以递归模式处理
-p, –perms 保持文件权限
-o, –owner 保持文件属主信息
-g, –group 保持文件属组信息
-e, –rsh=COMMAND 指定替代rsh的shell程序
–delete 删除那些DST中SRC没有的文件
–progress 显示备份过程
-t, –times 保持文件时间信息
-z, –compress 对备份的文件在传输时进行压缩处理
–stats 给出某些文件的传输状态

测试rsync同步,将A主机的/opt/etc同步到B主机的/tmp/myetc目录下:

$ rsync -vzrtopg –progress –stats –delete -e ssh /opt/etc mybackup@192.168.11.11:/tmp/myetc

然后到B主机的/tmp/myetc目录下看看,是不是发现一个etc目录,并且这个etc目录下面的文件,跟A主机的/opt/etc目录下面的文件一样。

1 RSYNC 参数详解

-v, –verbose 详细模式输出
-q, –quiet 精简输出模式
-c, –checksum 打开校验开关,强制对文件传输进行校验
-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, –recursive 对子目录以递归模式处理
-R, –relative 使用相对路径信息
-b, –backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用–suffix选项来指定不同的备份文件前缀。
–backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, –update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, –links 保留软链结
-L, –copy-links 想对待常规文件一样处理软链结
–copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
–safe-links 忽略指向SRC路径目录树以外的链结
-H, –hard-links 保留硬链结
-p, –perms 保持文件权限
-o, –owner 保持文件属主信息
-g, –group 保持文件属组信息
-D, –devices 保持设备文件信息
-t, –times 保持文件时间信息
-S, –sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, –dry-run现实哪些文件将被传输
-W, –whole-file 拷贝文件,不进行增量检测
-x, –one-file-system 不要跨越文件系统边界
-B, –block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, –rsh=COMMAND 指定替代rsh的shell程序
–rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, –cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
–existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
–delete 删除那些DST中SRC没有的文件
–delete-excluded 同样删除接收端那些被该选项指定排除的文件
–delete-after 传输结束以后再删除
–ignore-errors 及时出现IO错误也进行删除
–max-delete=NUM 最多删除NUM个文件
–partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
–force 强制删除目录,即使不为空
–numeric-ids 不将数字的用户和组ID匹配为用户名和组名
–timeout=TIME IP超时时间,单位为秒
-I, –ignore-times 不跳过那些有同样的时间和长度的文件
–size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
–modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T –temp-dir=DIR 在DIR中创建临时文件
–compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 –partial
–progress 显示备份过程
-z, –compress 对备份的文件在传输时进行压缩处理
–exclude=PATTERN 指定排除不需要传输的文件模式
–include=PATTERN 指定不排除而需要传输的文件模式
–exclude-from=FILE 排除FILE中指定模式的文件
–include-from=FILE 不排除FILE指定模式匹配的文件
–version 打印版本信息
–address 绑定到特定的地址
–config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
–port=PORT 指定其他的rsync服务端口
–blocking-io 对远程shell使用阻塞IO
–stats 给出某些文件的传输状态
–progress 在传输时现实传输过程
–log-format=FORMAT 指定日志文件格式
–password-file=FILE 从FILE中得到密码
–bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, –help 显示帮助信息

2 中文编码问题

如果遇到两个系统的字符集编码不一致的问题是,可以使用–iconv参数来解决编码问题。

–iconv=from,to

用法如下:

rsync -vzrtopg –progress –stats –delete –iconv=GB2312,UTF-8 -e ssh /opt/home 192.168.0.223:/opt/

使用基于ssh的rsync远程同步数据

1、同步静态主机表文件

# 执行“推”复制同步(centos5 是可解析的远程主机名)
[root@soho ~]# rsync /etc/hosts centos5:/etc/hosts
# 执行“拉”复制同步(soho 是可解析的远程主机名)
[root@centos5 ~]# rsync soho:/etc/hosts /etc/hosts

2、同步用户的环境文件

# 执行“推”复制同步
[osmond@soho ~]$ rsync ~/.bash* centos5:
# 执行“拉”复制同步
[osmond@cnetos5 ~]$ rsync soho:~/.bash* .

3、同步站点根目录

# 执行“推”复制同步
[osmond@soho ~]$ rsync -avz –delete /var/www root@192.168.0.101:/var/www
# 执行“拉”复制同步
[osmond@cnetos5 ~]$ rsync -avz –delete root@192.168.0.55:/var/www /var/www

使用基于 ssh 的 rsync 同步数据可以使用 -essh 参数,当前的 CentOS 默认指定使用 ssh 作为远程 Shell。若您在其他系统上执行 rsync 命令,为确保使用 ssh 作为远程 Shell,请添加 -essh 参数。
通常 rsync 命令在后台以 cron 任务形式执行,为了避免从终端上输入口令需要设置 ssh。ssh 的设置方法请参考安全登录守护进程。

使用–exclude/–include选项

可以使用–exclude 选项排除源目录中要传输的文件;同样地,也可以使用–include 选项指定要传输的文件。

例如:下面的 rsync 命令将 192.168.0.101 主机上的 /www 目录(不包含 /www/logs 和 /www/conf 子目录)复制到本地的/back up/www/ 。

# rsync -vzrtopg –delete –exclude “logs/” –exclude “conf/” –progress \
backup@192.168.0.101:/www/ /backup/www/

又如:下面的rsync命令仅复制目录结构而忽略掉目录中的文件。

# rsync -av –include ‘*/’ –exclude ‘*’ \
backup@192.168.0.101:/www/ /backup/www-tree/

选项–include 和–exclude 都不能使用间隔符。例如:

–exclude “logs/” –exclude “conf/”

不能写成

–exclude “logs/ conf/”

Rsync

rsync客户端配置

[root@server05 ~]# crontab -l
*/10 * * * * /home/system/rsync/rsync.sh
[root@server05 ~]# more  /home/system/rsync/rsync.sh 
#!/bin/bash
if test -e /tmp/rsync.pid
then
exit
else
touch /tmp/rsync.pid
/home/system/rsync/bin/rsync -uvrtopg --delete --password-file=/home/system/rsync/rsync.passwd root@192.168.1.2::down /home/down
rm -rf /tmp/rsync.pid
fi
[root@server05 ~]# more  /home/system/rsync/rsync.passwd 
d9jYijw
[root@server05 ~]# cd 
rsync.passwd 600

rsync服务器端设置

[root@server02 ~]# cd /etc/
[root@server02 etc]# more rsyncd.conf 
uid = root
gid = root
use chroot = no
max connections = 10 
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[down]
path = /home/down/
auth users = root
uid = root
gid = root
read only = true
secrets file = /etc/rsyncd.secrets
[root@server02 etc]# 
[root@server02 etc]# more rsyncd.secrets 
root:d9jYijw-&tTem60
[root@server02 etc]# 
[root@server02 etc]# ll rsyncd.conf 
-rw-r--r--  1 root root 271 2008-06-01  rsyncd.conf
[root@server02 etc]# ll rsyncd.secrets 
-rw-------  1 root root 21 2007-10-31  rsyncd.secrets
[root@server02 etc]#rsync --daemon --config=/etc/rsyncd.conf