The rsync command is a remote data synchronization tool that can quickly synchronize files between multiple hosts over a LAN/WAN. Rsync synchronizes files between local and remote hosts using what’s called the “rsync algorithm,” which is fairly fast because it only sends different parts of two files, rather than the whole one at a time. Rsync is a very powerful tool, and its commands have a number of feature options, which are explained below.

grammar

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]host:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
Copy the code

Rsync has six different working modes corresponding to the above six command formats:

  1. Copy local files. This working mode is enabled when neither SRC nor DES path information contains a single colon “:” delimiter. For example, rsync -a /data /backup
  2. Use a remote shell program (such as RSH, SSH) to copy the contents of the local machine to the remote machine. This mode is enabled when the DST path address contains a single colon “:” delimiter. For example, rsync-avz *. C foo: SRC
  3. Use a remote shell program (such as RSH, SSH) to copy the contents of the remote machine to the local machine. This mode is enabled when the SRC address path contains a single colon “:” delimiter. For example, 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. For example: rsync-av [email protected]:: WWW /databack
  5. Copy files from the local machine to the remote Rsync server. This mode is started when the DST path information contains the “::” delimiter. For example: rsync-av /databack [email protected]:: WWW
  6. List of files for the remote machine. This is similar to rsync transport, except that local machine information is omitted from the command. Such as: rsync – v rsync: / / 192.168.78.192 / WWW

options

-v, --verbose Verbose mode output. -q, --quiet Specifies the thin output mode. -c, --checksum Turns on the checksum switch to forcibly verify file transfer. -a, --archive Indicates the archiving mode, which transfers files recursively and keeps all file attributes, equal to -rlptGod. -r, --recursive processes subdirectories in recursive mode. -r, --relative Uses relative path information. -b, --backup creates a backup, that is, if the same filename already exists for the destination, rename the old file to ~filename. You can use the --suffix option to specify different backup file prefixes. --backup-dir Stores backup files, such as ~filename, in a directory. -suffix= suffix Defines the prefix of backup files. -u, --update only updates, that is, skips all files that already exist in DST and are later than the files to be backed up, not overwriting the updated files. -l, --links Keep soft links. -l, --copy-links wants to treat soft links like regular files. --copy-unsafe-links copies only links that point outside the SRC path directory tree. --safe-links ignores links that point outside the SRC path directory tree. -h, --hard-links Retain hard links. -p, --perms preserves file permissions. -o, --owner Keeps the file owner information. -g, --group Keeps the file owner group information. -d, --devices Keeps device file information. -t, --times Keeps the file time. -s, -- SPARSE Special handling of sparse files to save space for DST. -n, --dry-run specifies which files will be transferred. -w, --whole-file Copies the file without incremental detection. -x, --one-file-system Do not cross file system boundaries. -b, --block-size= size Verifies the block size used by the algorithm. The default is 700 bytes. -e, -- RSH =command Specifies data synchronization in RSH and SSH mode. --rsync-path= path Specifies the path of the rsync command on the remote server. -c, --cvs-exclude Automatically ignores files using the same method as CVS. It excludes files that you do not want to transfer. Existing only updates files that already existin DST and does not back up newly created files. --delete deletes files that SRC does not have in DST. --delete-excluded also deletes files on the receiving end that are excluded by this option. --delete-after Deletes the data after the transfer ends. --ignore-errors Deletes I/O errors even when they occur. --max-delete=NUM Deletes a maximum of NUM files. Partial preserves files that have not been fully transferred for some reason to speed up subsequent re-transfers. --force Forcibly deletes a directory, even if it is not empty. -- Numeric user and group ids do not match numeric user and group ids to user and group names. --timeout=time INDICATES the IP timeout duration, 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, only view the file size regardless of the file time. --modify-window=NUM Specifies the timestamp window used to determine whether files are at the same time. The default is 0. -t --temp-dir= dir Creates temporary files in dir. --compare-dest=DIR Also compares files in DIR to determine whether backup is needed. -p is equal to --partial. --progress Displays the backup process. -z, --compress Compresses backup files during transmission. --exclude=PATTERN Specifies file patterns that do not need to be transferred. --include=PATTERN Specifies file patterns that need to be transferred without excluding them. --exclude-from=FILE Excludes files of the specified pattern in FILE. --include-from=FILE Does not exclude FILE files that specify pattern matching. --version Displays the version information. --address binds to a specific address. --config=FILE Specifies another configuration FILE, not the default rsyncd.conf FILE. --port= port Specifies another rsync service port. --blocking- IO uses blocking IO for remote shells. -stats Indicates the transfer status of certain files. --progress Indicates the actual transmission process during transmission. --log-format= format Specifies the log file format. --password-file= file Get the password from file. --bwlimit=KBPS Limits the I/O bandwidth, KBytes per second. -h, --help Displays help information.Copy the code

The instance

SSH way

Start the SSH service on the server:

Service SSHD start SSHD: [ok]Copy the code

Use rsync for synchronization

Then you can use the rsync command on the client to back up data on the server. SSH is used to back up data using the system user as follows:

Rsync vzrtopg - progress - e SSH -- delete [email protected]: / WWW / * / databack/experiment/rsync [email protected] 's password: receiving file list ... 5 files to consider test/ a 0 100% 0.00kB/s 516:35:41 (1, 20.0% of 5) b 67 100% 65.43kB/s 0.0% of 5) c 0 100% 0.00kB/s 516:35:41 (3, 60.0% of 5) DD 100663296 100% 0.00 MB/s 80.0% of 5) Sent 96 bytes Received 98190 bytes 11563.06 bytes/ SEC Total size is 100663363 speedup is 1024.19Copy the code

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

Background Service mode

Start the rsync service, change the disable=yes value in the /etc/xinetd.d/rsync file 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 }Copy the code
D /xinetd restart Stop xinetd: [ok] Start xinetd: [OK]Copy the code

Reference: launch rsync blog.51cto.com/oldma/20519… You can start rsync –daemon manually

After the rsync program is installed by default, the main rsync configuration file is not automatically created. You need to manually create the main rsync configuration file in /etc/rsyncd.conf. Create the file and insert the following contents:

uid = zhongchuan gid = zhongchuan use chroot = no max connections = 4 pid file = /var/run/rsyncd.pid lock file =/var/run/rsyncd.lock log file =/var/log/rsyncd.log timeout = 900 port = 873 [xxxxx] ignore errors read only = no list =  yes hosts allow = * auther users = xxxxx path = /home/xxxxx/entitiesCopy the code

Therefore, you need to create a password file in the format of “username:password”. The username and password can be defined arbitrarily. It is better not to be the same as the system account, and set the permission of the password file to 600. This is described in detail in the previous module parameters.

echo "work:abc123" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd
Copy the code

Now you can back up your data as follows:

Rsync - avz -- progress -- delete [email protected]: : WWW/databack/experiment/rsync Password: identifiers file list... 6 files to consider ./ files... A 0 100% 0.00kB/s 528:20:41 (1, 50.0% of 6) B 0 100% 0.00kB/s 528:28:41 (2, 66.7% of 6) C 0 100% 0.00kB/s 528:28:41 (3, 66.7% of 6) 83.3% of 6) DD 100663296 100% 37.49MB/s 0:00:02 (4, Of 6) Sent 172 bytes received 98276 bytes 17899.64 bytes/ SEC Total size is 150995011 speedup is 1533.75Copy the code

Restore: When the server data is faulty, the server needs to use the client data to restore the server, but the premise is that the server allows the client to have write permission, otherwise, the server cannot directly restore the server on the client. The method of using rsync to restore the data is as follows:

Rsync - avz - progress/databack/experiment/rsync/[email protected]: : WWW Password: building the file list... 6 files to consider./ a b 67 100% 0.00kB/s 66.7% of 6) C sent 258 bytes Received 76 bytes 95.43 bytes/ SEC Total size is 150995011 speedup is 452080.87Copy the code

rsync

The server didn’t start because I started it using xinet, using ipv4 port 873, using rsync –daemon –config only with ipv6. Rsync: safe_read failed to read 1 bytes [Receiver]: Connection reset The corresponding port service is not enabled on the server

  • Lsof -i:873 Check which process occupies 873
  • Then kill off
  • Then restart rsync

/usr/bin/rsync –daemon –config /etc/rsyncd.conf