The Rsync Remote Data Synchronization tool for Linux

Source: Internet
Author: User
Tags bz2 syslog rsync

rsync (remote synchronize) is a data synchronization tool that synchronizes files between local and remote hosts using the rsync algorithm.

The advantage of rsync is that only two files are synchronized in different parts, the same parts are not delivered. Similar to incremental backups,
This allows the server to pass backup files or synchronize files, which saves a lot of time compared to SCP tools.
Os:ubuntu Server 10.04
server:192.168.64.128
client:192.168.64.145

Server

1.ubuntu Server 10.04 The default installed Rsync,rsync service is not started by default and 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 #已默认安装的软件, the default does not start it seems to do this
Let's 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 is going to run rsync out of the the Init.d script.
PID File=/var/run/rsyncd.pid
#指定rsync发送日志消息给syslog时的消息级别, the 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 the file is transferred. The reason for this is to implement additional security, but the disadvantage is that root permissions are required, and you cannot back up a directory file pointing to an external symbolic connection.
# max Connections=10 #最大连接数
Lock file =/VAR/LOCK/RSYNCD #指定支持max the connections parameter.
# The default for read is yes ...
Read Only = yes #只读选项
List = yes #客户请求时可用模块时是否列出该模块
UID = nobody #设定该模块传输文件时守护进程应该具有的uid
GID = Nogroup #设定该模块传输文件时守护进程应具有的gid, which matches the UID to determine access to the file
# exclude = #用来指定多个由空格隔开的多个模式列表, and add it to the exclude list. This is equivalent to using--exclude in the client command to specify the pattern, but the exlude pattern 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 you can use "-" and "+" in front of the pattern to specify whether it is a exclude or an include #这个我的理解是排除目录中不需同步的文件
# Exclude From = #可以指定一个包含exclude模式定义的文件名
# include = #与exclude相似
# include from = #可以指定一个包含include模式定义的文件名
# auth users = #该选项指定由空格或逗号分隔的用户名列表, only these users are allowed to connect to the module. There is no relationship between the user and the system user. If the "Auth users" is set, then the client sends a connection request to the module that will be authenticated by rsync request challenged 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. By default, no password is required to connect to the module (that is, anonymous mode)
# Secrets File =/etc/rsyncd.secrets #该文件每行包含一个username:p assword pair, stored in clear text, this option 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 then the password file can only be accessed by a user running 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 = #超时设置 (seconds)
Refuse options = checksum Dry-run #定义一些不允许客户对该模块使用的命令选项列表
Dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告诉rysnc那些文件在传输前不用压缩, default set compressed package no longer compressed
List of log format options:
%H: Remote Host name
%a: Remote IP Address
%l: Number of file length characters
%p: The process ID of the rsync session
%o: Operation type: "Send" or "recv", "Del."
%f: File name
%P: module Path
%m: module Name
%t: Current Time
%u: Authenticated user name (null when anonymous)
%b: Number of bytes actually transferred
%c: When a file is sent, the field records the checksum of the file
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 is going to run rsync out of the the Init.d script.
PID File=/var/run/rsyncd.pid
Syslog Facility=daemon
#socket options=

# MODULE OPTIONS

[RSYNC_BK]

Comment = Public archive
Path =/HOME/RSYNC_BK #指定路径 If this directory is not built by itself.
Use chroot = no
# Max Connections=10
Lock file =/var/lock/rsyncd
# The default for read is yes ...
Read Only = yes
List = yes
UID = nobody
GID = Nogroup
# exclude =
# Exclude From =
# include =
# include from =
Auth users = rsync #rsync连接时的用户名
Secrets file =/etc/rsyncd.secrets
Strict modes = yes
Hosts allow = 192.168.64.145
# 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 *.tbz

Create a password file
$sudo vi/etc/rsyncd.secrets
Rsync:123
$sudo chmod 0600/etc/rsyncd.secrets
Start rsync
Sudo/etc/init.d/rsync start
Client
We look at the operation of the client, the general client does not need special configuration, the direct synchronization can
RSYNC-VZRTOPG--progress [email PROTECTED]::RSYNC_BK. #. For the current directory, touch a file on the server, after synchronization will appear, of course, you can also specify a location such as/DATABK, you need to pay attention to the established permissions!

We give this synchronization to Crontab to execute. First we're going to create a password file
$sudo vi/etc/rsync.pwd input 123, save #密码要一致
Note: The following two steps are required, not enough permissions to synchronize will not succeed, ls-l the directory you point to
sudo chmod 0600/etc/rsync.pwd
sudo chown Normal User: General user group/etc/rsync.pwd
Then we open the crontab and make it sync automatically
$crontab-E
* * * * * * rsync-a--password-file=/etc/rsync.pwd [email PROTECTED]::RSYNC_BK/DATABK

crontab random start sudo vi/etc/rc.local

Parameter description:

-v verbose, which is verbose mode

-Z Compression

-R Recursive recursion

-TOPG maintain the original properties of the file, generally do not add

--progress used to show the details of progress

--delete indicates that if a server deletes a file, the client should also delete the

--exclude= "*.sh" indicates that some files are not included

--password-file=/etc/rsync.pwd Specify the password file to use

The last item is a directory that needs to be synchronized

Note You only need to have a password in the password file that you specify, and do not have a user name.

------------------------------------------------------------------------

1. Synchronize two directories on a local machine

$ RSYNC-ZVR filename1 filename2

The code above is to synchronize the files in the filename1 with the files in the 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 time by tag

$ rsync-azv filename1 filename2

With the above command, the time of the newly synced file in Filename2 is the same as the time created in Filename1,

It retains the same symbolic link, permission, time stamp, user name, and group name.

3. Synchronizing files from the local to the remote server

$rsync-avz filename1 [email protected]:/home/ubuntu/filename2

The above command synchronizes the local filename1 to the remote 192.168.0.1 host.

Note: If the port of the remote host is not the default port 22, if it is Port 3000, the above command is modified to,

$ Rsync-avz '-e ssh-p 4000 ' filename1 [email protected]:/home/ubuntu/filename2

4. Synchronize the remote server's files to a local

Similar to step 3, just swap the location of the filename1 with the remote server,

$rsync-avz [Email protected]:/home/ubuntu/filename2 filename1

Similarly, if the port is not 22, use the following command

$ Rsync-avz '-e ssh-p 4000 ' [Email protected]:/home/ubuntu/filename2 filename1


The Rsync Remote Data Synchronization tool for Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.