Rsync (synchronize) is a remote data synchronization tool that synchronizes files between local and remote hosts using the rsync algorithm.
The benefit of rsync is to sync only two different parts of the file, and the same part is not delivered. Similar to incremental backups,
This makes it much more time-consuming to deliver backup files or sync files to the server than to SCP tools.
Os:ubuntu Server 10.04
server:192.168.64.128
client:192.168.64.145
Server
1.ubuntu Server 10.04 default installed Rsync,rsync service is not started by default, we want to modify the following file.
$sudo Vi/etc/default/rsync
Rsync_enable=true #false改true
2. Modify the configuration file
$sudo cp/usr/share/doc/rsync/examples/rsyncd.conf/etc #已默认安装的软件, it seems that the default does not start.
Let's take a look at this file first.
$sudo cat/etc/rsyncd.conf
# Sample RSYNCD.CONF configuration file
# GLOBAL OPTIONS
#motd FILE=/ETC/MOTD #登录欢迎信息
#log FILE=/VAR/LOG/RSYNCD #日志文件
# for PID file, does not use/var/run/rsync.pid if
# You are going to run rsync out of the init.d script.
PID File=/var/run/rsyncd.pid
#指定rsync发送日志消息给syslog时的消息级别, common message levels are: Uth, Authpriv, cron, Daemon, FTP, Kern, LPR, mail, news, security, Sys-log, user, Uuc P, Local0, Local1, Local2, Local3,local4, LOCAL5, Local6 and LOCAL7. The default value is daemon.
#syslog Facility=daemon
#自定义tcp选项, the default is off
#socket options=
#以下是模块信息, we can create multiple modules
# MODULE OPTIONS
[FTP]
Comment = Public archive #模块描述
Path =/var/www/pub #需要同步的路径
Use chroot = yes #默认是yes |true, if true, before rsync chroot to the directory specified by the path parameter before transferring files. The reason for this is to implement additional security protection, but the disadvantage is that root permissions are required and you cannot back up the directory files that point to external symbolic connections.
# max Connections=10 #最大连接数
Lock file =/VAR/LOCK/RSYNCD #指定支持max connections parameter.
# The default for read-only is yes ...
Read Only = yes #只读选项
List = yes #客户请求时可用模块时是否列出该模块
UID = nobody #设定该模块传输文件时守护进程应该具有的uid
GID = Nogroup #设定该模块传输文件时守护进程应具有的gid, which is matched with UID to determine access rights for files
# exclude = #用来指定多个由空格隔开的多个模式列表, and add it to the exclude list. This is equivalent to using--exclude in the client command to specify the pattern, although the Exlude mode specified in the configuration file is not passed to the client and is applied only to the server. A module can specify only one exlude option, but it is possible to use "-" and "+" in front of the pattern to specify whether it is exclude or include #这个我的理解是排除目录中不需同步的文件
# Exclude From = #可以指定一个包含exclude模式定义的文件名
# include = #与exclude相似
# include from = #可以指定一个包含include模式定义的文件名
# auth users = #该选项指定由空格或逗号分隔的用户名列表, only those users are allowed to connect to the module. There is no relationship between the user and the system user here. If "Auth users" is set up, then the client sends a connection request to the module and the challenged is requested by rsync to authenticate the Challenge/response authentication protocol used here. The user's name and password are stored in plaintext in the file specified by the Secrets file option. Modules can be connected by default without a password (that is, anonymous)
# Secrets File =/etc/rsyncd.secrets #该文件每行包含一个username:p assword pairs, stored in plaintext, which takes effect only if Auth users is defined. At the same time we need to set this file permission to 0600
Strict modes = yes #该选项指定是否监测密码文件的权限, if the option value is true, the password file can only be accessed by a user who is running the identity of the rsync server, and no other user can access the file. The default value is True
# hosts allow = #允许的主机
# hosts Deny = #拒绝访问的主机
Ignore errors = no #设定rsync服务器在运行delete操作时是否忽略I/O error
Ignore nonreadable = yes #设定rysnc服务器忽略那些没有访问文件权限的用户
Transfer logging = no #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中
# log format =%t:host%h (%a)%o%f (%l bytes). Total%b bytes. #设定日志格式
Timeout = #超时设置 (sec)
Refuse options = checksum Dry-run #定义一些不允许客户对该模块使用的命令选项列表
Dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告诉rysnc那些文件在传输前不用压缩, default set compression package no longer compress
List of log format options:
%H: Remote Host name
%a: Remote IP Address
%l: Number of characters in file length
%p: Process ID for this rsync session
%o: Type of operation: "Send" or "recv", "Del."
%f: filename
%P: module Path
%m: module Name
%t: Current Time
%u: Authenticated username (null when anonymous)
%b: The actual number of bytes transferred
%c: When the file is sent, the field records the checksum code for the file
Now let's define our own conf file.
# Sample RSYNCD.CONF configuration file
# GLOBAL OPTIONS
#motd FILE=/ETC/MOTD
Log FILE=/VAR/LOG/RSYNCD
# for PID file, does not use/var/run/rsync.pid if
# You are going to run rsync out of the init.d script.
PID File=/var/run/rsyncd.pid
Syslog Facility=daemon
#socket options=
# MODULE OPTIONS
[MY_RSYNC_BK] #名字可以任意命名, as long as the client's rsync command is consistent
Comment = Public archive
Path =/HOME/RSYNC_BK #指定路径 If this directory is not built on its own.
Use chroot = no
# Max Connections=10
Lock file =/var/lock/rsyncd
# The default for read-only is yes ...
Read Only = yes
List = yes
UID = nobody
GID = Nogroup
# exclude =
# Exclude From =
# include =
# include from =
Auth users = Liu_rsync #rsync连接时的用户名, to be consistent with the client rsync command
Secrets file =/etc/rsyncd.secrets #这里是保存密码的地方, you don't need the password if you block it out
Strict modes = yes
Hosts allow = 192.168.64.145 #运行的客户端ip
# hosts Deny =
Ignore errors = yes
Ignore nonreadable = yes
Transfer logging = yes
Log format =%t:host%h (%a)%o%f (%l bytes). Total%b bytes.
Timeout = 600
Refuse options = Checksum Dry-run
Dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2
Create a password file
$sudo vi/etc/rsyncd.secrets
Liu_rsync:123
$sudo chmod 0600/etc/rsyncd.secrets
Start rsync
Sudo/etc/init.d/rsync start
Client
We will take a look at the operation of the client, the general client does not require special configuration, direct synchronization can
RSYNC-VZRTOPG--progress LIU_RSYNC@192.168.64.128::MY_RSYNC_BK. # for the current directory, touch a file at the service end, synchronization will appear, of course, you can also specify a location such as/DATABK, you need to pay attention to the established permissions.
We put this synchronization work to Crontab to execute. First we're going to create a password file
$sudo Vi/etc/rsync.pwd Enter 123, save #密码要一致
! Note: The following two steps are required, without sufficient permission synchronization will not succeed, ls-l the directory you are pointing to
sudo chmod 0600/etc/rsync.pwd
sudo chown common User: Common user group/etc/rsync.pwd
Then we open the crontab and make it sync automatically.
$crontab-E
* * * * * * * rsync-a--password-file=/etc/rsync.pwd LIU_RSYNC@192.168.64.128::MY_RSYNC_BK/DATABK
Crontab randomly start sudo vi/etc/rc.local
Parameter description:
-v verbose, verbose mode
-Z Compression
-R Recursive recursion
-TOPG maintain the original properties of the file, generally do not add
--progress to show detailed progress
--delete indicates that if the server deletes a file, the client should also correspond to the deletion
--exclude= "*.sh" indicates that some files are not included
--PASSWORD-FILE=/ETC/RSYNC.PWD Specifies the password file to use
The last item is a directory that needs to be synchronized
Note The password file you specify to use requires only a password and no user name.
------------------------------------------------------------------------
1. Synchronize two directories on the local machine $ RSYNC-ZVR filename1 filename2
The above code synchronizes the files in filename1 with the files in filename2, and if the files in Filename2 are synchronized to filename1, modify the code to: $ RSYNC-ZVR filename2 filename1
2. Use Rsync–a to synchronize retention times by Mark $ rsync-azv filename1 filename2
Using the above command, the new synchronized file in Filename2 is the same as the time it was created in filename1.
It retains the same symbolic link, permission, time stamp, user name, and group name.
3. Synchronize files from local to remote server $rsync-avz filename1 ubuntu@192.168.0.1:/home/ubuntu/filename2
The above command synchronizes the local filename1 to the remote 192.168.0.1 host.
Note: If the remote host port is not the default 22 port, and if it is a 3000 port, the above command is modified to $ Rsync-avz '-e ssh-p 4000 ' filename1 ubuntu@192.168.0.1:/home/ubuntu/ Filename2
4. Synchronize files from remote server to local
Similar to step 3, just swap the filename1 with the location of the remote server, $rsync-avz ubuntu@192.168.0.1:/home/ubuntu/filename2 filename1
Similarly, if the port is not 22, use the following command $ Rsync-avz '-e ssh-p 4000 ' ubuntu@192.168.0.1:/home/ubuntu/filename2 filename1
Reference: http://www.eduicc.com/read-24.html
http://jordy.easymorse.com/?p=273
Servers often maintain consistency between files or directories, such as large software downloads, which typically use multiple servers to provide download services. When the files on one server are updated, other servers need to be updated, and the updates should be updated only for new or modified files, which can cause network bandwidth and waste of time. Rsync is an excellent software that can effectively maintain the consistency of files and directories.
Rsync,remote Synchronize
As implies to know it is a remote synchronization function of the software, while synchronizing the files, it can maintain the original file's permissions, time, soft and hard links, and other additional information, and can be transmitted via SSH file, so that its confidentiality is also very good, in addition it is free software. RYSNC's official website: http://rsync.samba.org/, you can get the latest version from above. Of course, because rsync is such a useful software, so many Linux distributions are included in it. You do not have Rsync installed in your Linux, you can install it yourself by following the safety method:
First, the installation process
1. Download rsync
Currently (September 2003) The latest version of Rsync is 2.5.6, from RYSNC's official website to download one back:
# wget http://ftp.samba.org/ftp/rsync/rsync-2.5.6.tar.gz
2. Decompression
# TAR-XZPVF Rsync-2.5.6.tar.gz
3. Compile and install
# CD RSYNC-2.5.6/
#./configure--prefix=/usr/local/rsync
# make
# make Install
The above process does not appear to be installed, now there is the rsync command can be used, the rsync command on
/usr/local/rsync/bin. Use the rsync command to crawl data on servers running Rsync services.
If you want to turn the current machine into an rsync server, you need to continue with some configuration.
Second, the configuration of rsync services
Configuring a simple Rsync service is not complicated and you need to modify or set up some configuration files.
1.rsyncd.conf
# VI/ETC/RSYNCD.MOTD
Rsyncd.con is the main configuration file for the Rsync service, which controls the various properties of the Rsync service, given a
Examples of rsyncd.conf files:
#先定义整体变量
Secrets file =/etc/rsyncd.secrets
MOTD file =/ETC/RSYNCD.MOTD
Read Only = yes
List = yes
UID = nobody
GID = Nobody
Hosts allow = 192.168.100.90 #哪些电脑可以访问rsync服务
Hosts deny = 192.168.100.0/24 #哪些电脑不可以访问rsync服务
Max connections = 2
Log file =/var/log/rsyncd.log
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
#再定义要rsync目录
[Terry]
Comment = Terry ' s directory from 192.168.100.21
Path =/home/terry
Auth users = Terry,rsync
[Test]
Comment = Test rsync
Path =/home/test
In the above configuration file, the 192.168.100.0/24 is limited to 192.168.100.90 of the machines that can access this rsync server's rsync service. The following section of the configuration file defines two rsync directories, and the Terry directory is available only to people who know about the two accounts of Terry and Rsync, and the text directory is accessible without an account. Rsync also provides a number of other options for defining the directory, which can be more tightly controlled.
2.rsyncd.secrets
# vi/etc/rsyncd.secrets
Rsyncd.secrets is the user name and password that stores the Rsync service, which is a plaintext text file, and here's an example of a rsyncd.secrets file:
terry:12345
Rsync:abcde
Because Rsyncd.secrets stores the user name and password for the rsync service, it is important that the properties of the file must be
Set to 600, only the owner can read and write:
# chmod 600/etc/rsyncd.secrets
3.rsyncd.motd
# VI/ETC/RSYNCD.MOTD
RSYNCD.MOTD records the welcome information for the Rsync service, where you can enter any textual information, such as: