rsync command (Synchronize/back up data)

Source: Internet
Author: User
Tags rsync

rsync command
The rsync command is a remote data synchronization tool that enables fast synchronization of files between multiple hosts via Lam/wan.
Rsync uses the so-called "rsync algorithm" to synchronize files between local and remote two hosts,
This algorithm only passes the different parts of the two files, not the whole transfer every time, so the speed is quite fast.
Grammar
rsync [OPTION]. SRC dest
rsync [OPTION]. src [[Email protected]]host:dest
rsync [OPTION]. [[Email Protected]]host:src dest
rsync [OPTION]. [Email protected]]host:: src dest
rsync [OPTION]. src [[email protected]]host::d EST
rsync [OPTION]. Rsync://[[email protected]]host[:p ort]/src [dest]
There are 6 different modes of operation for Rsync, which corresponds to the above six command formats.
1. Copy Local files
This mode of operation is initiated when both the SRC and dest path information do not contain a single colon ":" delimiter
As:rsync-a/data/backup

2. Copy Local machine contents to remote machine
The mode is started when the Dest path address contains a single colon ":" delimiter
As:rsync-avz a.txt FOO:SRC

3. Copy the contents of the remote machine to the local machine
This mode is started when the SRC address path contains a single colon ":" delimiter. such as: Rsync-avz Foo:src/bar/data

4. Copy files from the remote rsync server to the local machine
This mode is started when the SRC path information contains the "::" delimiter. such as: rsync-av [email Protected]::www/databack

5. Local machine copy files to the remote rsync server
This mode is started when the DST path information contains the "::" delimiter. such as: rsync-av/databack [email protected]::www
(The WWW name here is in the configuration file, as mentioned later)

6. List the files of the remote machine.
This is similar to the rsync transfer, but only if the local machine information is omitted from the command. such as: Rsync-v rsync://192.168.78.192/www

Parameter option description
-V,--verbose verbose mode output.
-Q,--quiet thin output mode.
-C,--checksum turns on the check switch to force a checksum of the file transfer.
-A,--archive archive mode, which means that files are transferred recursively, and all file attributes are maintained, equal to-rlptgod.
-R,--recursive subdirectories are processed in recursive mode.
-R,--relative uses relative path information.
-B,--backup creates a backup, that is, the old file is renamed to ~filename when the same file name exists for the purpose. You can use the--suffix option to specify a different backup file prefix.
--backup-dir store backup files (such as ~filename) in the directory.
-suffix=suffix defines the backup file prefix.
-U,--update only updates, that is, skips all existing DST, and the file time is later than the file to be backed up, not overwriting the updated file.
-L,--links retains the soft-link knot.
-L,--copy-links to handle soft links like regular files.
--copy-unsafe-links only copies links to the SRC Path directory tree.
--safe-links ignores links to links other than the SRC path directory tree.
-H,--hard-links preserves hard links.
-P,--perms maintain file permissions.
-O,--owner keep the file owner information.
-G,--group keep the file group information.
-D,--devices maintains device file information.
-T,--times keeps file time information.
-S,--sparse special processing of sparse files to save DST space.
-N,--dry-run reality which files will be transmitted.
-W,--whole-file copy files without incremental detection.
-X,--one-file-system do not cross file system boundaries.
-B, the block size used by the--block-size=size test algorithm is 700 bytes by default.
-E,--rsh=command specifies that data is synchronized using RSH and SSH.
--RSYNC-PATH=PATH Specifies the path information for the rsync command located on the remote server.
-C,--cvs-exclude uses the same method as CVs to automatically ignore files, which are used to exclude files that you do not want to transfer.
--existing only updates those files that already exist in DST, without backing up those newly created files.
--delete Delete those files that are not in the DST Src.
--delete-excluded also deletes those files that are excluded by the option specified by the Receive side.
After the--delete-after transmission is finished, delete again.
--ignore-errors Timely IO errors are also deleted.
--max-delete=num delete up to NUM files.
--partial retains files that are not fully transmitted for any reason, in order to expedite subsequent transmissions.
--force forcibly deletes the directory, even if it is not empty.
--numeric-ids does not match the user and group ID of the number to the user name and group name.
--timeout=time IP time-out, in seconds.
-I,--ignore-times does not skip files that have the same time and length.
--size-only when deciding whether to back up a file, just look at the file size regardless of the file time.
--modify-window=num determines whether the file is time-stamped with the time Stamp window, which defaults to 0.
-T--temp-dir=dir creates a temporary file in Dir.
--compare-dest=dir also compares the files in DIR to determine if a backup is required.
-P is equivalent to--partial.
--progress shows the backup process.
-Z,--compress compresses the files that are backed up when they are transferred.
--EXCLUDE=PATTERN Specifies to exclude file patterns that do not need to be transferred.
--INCLUDE=PATTERN Specifies the file mode that is not excluded and that needs to be transferred.
--exclude-from=file excludes files in the specified schema in file.
--include-from=file does not exclude files that specify pattern matching.
--version print version information.
--address is bound to a specific address.
--CONFIG=FILE specifies a different configuration file and does not use the default rsyncd.conf file.
--port=port specifies a different rsync service port.
The--blocking-io uses blocking IO on the remote shell.
-stats gives the transfer status of some files.
--progress the actual transmission process during transmission.
--LOG-FORMAT=FORMAT Specifies the log file format.
--password-file=file gets the password from file.
--bwlimit=kbps limits I/O bandwidth, Kbytes per second.
-H,--help displays help information.

Instance:
SSH mode
Start the SSH service on the server first:
Service sshd Start

Synchronizing with Rsync
Next, you can use the rsync command on the client side to back up the data on the server, and SSH is backed up by the system user, as follows:

RSYNC-VZRTOPG--progress-e SSH--delete username @ip address:/www/*/databack/experiment/rsync

The information above describes the overall backup process and the size of the total backup data.

Background service mode

Start the Rsync service, edit the/etc/xinetd.d/rsync file, change the Disable=yes to Disable=no, and restart the XINETD service as follows:
Vi/etc/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
Socket_type = Stream
wait = no
user = root
Server =/usr/bin/rsync
Server_args =--daemon
Log_on_failure + = USERID
}


Restart Service
/etc/init.d/xinetd restart

Creating a configuration file, the Rsync program is installed by default and does not automatically create a master profile for rsync.
It needs to be created manually, its master profile is "/etc/rsyncd.conf", the file is created, and the following content is inserted:
Vi/etc/rsyncd.conf

Uid=root
Gid=root
Max Connections=4
Log File=/var/log/rsyncd.log
PID File=/var/run/rsyncd.pid
Lock File=/var/run/rsyncd.lock
Secrets file=/etc/rsyncd.passwd
Hosts DENY=172.16.78.0/22 #这个一般不用

[www] # www is said above, directly with this www, you can indicate the path is/var/www/html, the above mode V
comment= Backup Web
Path=/var/www/html
Read Only = no
Exclude=test
Auth Users=work #定义的用户名

Create a password file, in this way can not use the system user authentication to the client,
So you need to create a password file in the format "Username:password",
Username can and password can be arbitrarily defined, it is best not to be consistent with the system account,
At the same time, to set the password file permissions created to 600,
This is described in detail in the previous module parameters

echo "User name: Password" >/etc/rsyncd.passwd #即上面的 work username
chmod 600/etc/rsyncd.passwd

Backup, complete the above work, you can now back up the data, as follows:
Rsync-avz--progress--delete User name @ip::www/databack/back

Recovery, when there is a problem with the server's data, then we need to restore the service side through the client's data.
But only if the server allows the client to have write access,
Otherwise, it is not possible to restore the server directly on the client side.
Here's how to recover data using rsync:
Rsync-avz--progress/databack/back User name @ip:: Name in configuration file

rsync command (Synchronize/back up data)

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.