In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the rsync command of Linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the rsync command of Linux.
Linux common commands the rsync command is a remote data synchronization tool that allows you to quickly synchronize files between multiple hosts through LAN/WAN.
Rsync remote data synchronization tool
It is added that rsync uses the so-called "rsync algorithm" to synchronize files between local and remote hosts, which transfers only different parts of the two files instead of the whole file every time, so it is quite fast. Rsync is a very powerful tool, and its command also has many functional options. We will analyze and explain its options one by one below.
Syntax 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] corresponds to the above six command formats, and rsync has six different working modes:
Copy the local file. This mode of operation is started when neither SRC nor DES path information contains a single colon: delimiter. For example: rsync-a / data / backup
Use a remote shell program (such as rsh, ssh) to copy the contents of the local machine to the remote machine. Start this mode when the DST path address contains a single colon ":" separator. Such as: rsync-avz * .c foo:src
Use a remote shell program (such as rsh, ssh) to copy the contents of the remote machine to the local machine. Start this mode when the SRC address path contains a single colon ":" separator. For example: rsync-avz foo:src/bar / data
Copy files from a remote rsync server to the local machine. This mode is started when the SRC path information contains the "::" delimiter. For example: rsync-av root@192.168.78.192::www / databack
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 root@192.168.78.192::www
List the files for the remote machine. This is similar to rsync transport, except that the local machine information is omitted from the command. Such as: rsync-v rsync://192.168.78.192/www
Option-v,-- verbose verbose mode output. -Q,-- quiet compact output mode. -c,-- checksum turns on the check switch to force the file transfer to be checked. -a,-- archive archiving mode, which means that files are transferred recursively and all file attributes are maintained, which is equal to-rlptgoD. -r,-- recursive handles subdirectories in recursive mode. -R,-- relative uses relative path information. -b,-- backup creates a backup, that is, rename the old file to ~ filename when the same file name already exists for the destination. 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 backup file prefix. -u,-- update only updates, that is, skips all files that already exist in DST and the file time is later than the file to be backed up, and does not overwrite the updated file. -l,-- links retains soft links. -L,-- copy-links handles soft links like regular files. -- copy-unsafe-links only copies links that point outside the SRC path directory tree. -- safe-links ignores links that point outside the SRC path directory tree. -H,-- hard-links retains hard links. -p,-- perms maintains file permissions. -o,-- owner maintains file ownership information. -g,-- group maintains file group information. -D,-- devices maintains device file information. -t,-- times keeps file time information. -S,-- sparse performs special handling of sparse files to save DST space. -n,-- dry-run reality which files will be transferred. -w,-- whole-file copies files 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, which defaults to 700bytes. -e,-- rsh=command specifies that rsh and ssh are used for data synchronization. -- rsync-path=PATH specifies the path information where the rsync command is located on the remote server. -C,-- cvs-exclude automatically ignores files in the same way as CVS to exclude files that you don't want to transfer. -- existing updates only those files that already exist in DST, not those newly created files. -- delete deletes files in DST that are not available in SRC. -- delete-excluded also deletes files on the receiver that are specified and excluded by this option. -- delete the delete-after after the transfer is finished. -- delete ignore-errors even if there are IO errors. -- max-delete=NUM deletes a maximum of NUM files. -- partial retains files that were not fully transferred for some reason, thus speeding up subsequent retransfers. -- force forces directory deletion, even if it is not empty. -- numeric-ids does not match numeric user and group id to user and group names. -- timeout=time ip timeout in seconds. -I,-- ignore-times does not skip files of the same time and length. -- size-only when deciding whether to back up a file, it only looks at the file size without considering the file time. -- the timestamp window used by modify-window=NUM to determine whether the file is 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 a backup is needed. -P equals-- partial. -- progress shows the backup process. -z,-- compress compresses the backed-up files during transfer. -- exclude=PATTERN specifies to exclude file modes that do not need to be transferred. -- include=PATTERN specifies the file mode that needs to be transferred without exclusion. -- exclude-from=FILE excludes files of the specified pattern in FILE. -- include-from=FILE does not exclude files that FILE specifies pattern matching. -- version print version information. -- address binds to a specific address. -- config=FILE specifies another configuration file and does not use the default rsyncd.conf file. -- port=PORT specifies other rsync service ports. -- blocking-io uses blocking IO for remote shell. -stats gives the transfer status of some files. -- progress actually transmits when it transmits. -- log-format=formAT specifies the log file format. -- password-file=FILE gets the password from FILE. -- bwlimit=KBPS limits the bandwidth of KBytes per second. -h,-- help displays help information. Instance SSH mode
First start the ssh service on the server side:
Service sshd start starts sshd: [OK] use rsync for synchronization
Next, you can use the rsync command on the client to back up the data on the server, which is backed up by the system user in SSH mode, as follows:
Rsync-vzrtopg-- progress-e ssh-- delete work@172.16.78.192:/www/* / databack/experiment/rsync work@172.16.78.192's password: receiving file list. 5 files to consider test/ a 0 100% 0.00kB/s 527 of 35 of 41 (1,20.0% of 5) b 67 100% 65.43kB/s 0:00:00 (2,40.0% of 5) c 0 100% 0.00kB/s 527 rsync 35 of 41 (3) 60.0% of 5) dd 100663296 100% 42.22MB/s 0:00:02 (4,80.0% of 5) sent 96 bytes received 98190 bytes 11563.06 bytes/sec total size is 100663363 speedup is 1024.19 the information above describes the entire backup process And the total size of the backup data.
Backstage 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 isa 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} / etc/init.d/xinetd restart stop xinetd: [OK] start xinetd: [OK] create configuration file, after rsync program is installed by default The main configuration file for rsync is not automatically created, but needs to be created manually. Its main configuration file is "/ etc/rsyncd.conf". Create the file and insert the following:
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] comment= backup web path=/www read only = no exclude=test auth users=work creates a password file. In this way, you cannot use the system user to authenticate the client, so you need to create a password file with the format "username:password" The user name and password can be defined freely, it is best not to be consistent with the system account, and the permissions of the password file created should be set to 600, which is described in detail in the previous module parameters.
Echo "work:abc123" > / etc/rsyncd.passwd chmod 600 / etc/rsyncd.passwd backup, complete the above work, and now you can back up the data, as follows:
Rsync-avz-- progress-- delete work@172.16.78.192::www / databack/experiment/rsyncPassword:receiving file list... 6 files to consider./ files...a0 100% 0.00kB/s 528 of 20 of 41 (150.0% of 6) b67 100% 65.43kB/s 0:00:00 (2,66.7% of 6) c0 100% 0.00kB/s 528 of 6) dd100663296 100% 37.49MB/s 0:00:02 (4) 100.0% of 6) sent 172 bytes received 98276 bytes 17899.64 bytes/sectotal size is 150995011 speedup is 1533.75 recovery When there is a problem with the data of the server, you need to recover the server through the data of the client, but only if the server allows the client to have write permission, otherwise you cannot recover the server directly on the client. You can use rsync to recover the data as follows:
Rsync-avz-- progress / databack/experiment/rsync/ work@172.16.78.192::wwwPassword:building file list... 6 files to consider./ab67 100% 0.00kB/s 0:00:00 (2,66.7% of 6) csent 258 bytes received 76 bytes 95.43 bytes/sectotal size is 150995011 speedup is 452080.87 synchronize the source directory to the destination directory
In the above command of $rsync-r source destination,-r indicates recursion, that is, it contains subdirectories. Note that-r is required, otherwise the rsync run will not succeed. The source directory represents the source directory and destination represents the destination directory.
Synchronize multiple files or directories
In the above command of $rsync-r source1 source2 destination, both source1 and source2 are synchronized to the destination directory.
Synchronization meta-information
The-a parameter can replace-r to synchronize meta-information (such as modification time, permissions, etc.) in addition to recursive synchronization. Because rsync uses file size and modification time by default to determine whether a file needs to be updated,-an is more useful than-r. The following usage is a common way to write.
$rsync-a source destination destination directory destination if it does not exist, rsync will be created automatically. After executing the above command, the source directory source is completely copied to the destination directory destination, which forms the directory structure of destination/source.
If you only want to synchronize the contents of the source directory source to the destination directory destination, you need to add a slash after the source directory.
After the above command of $rsync-a source/ destination is executed, the contents of the source directory are copied to the destination directory, and a source subdirectory is not created under destination.
Results of simulation execution
If you are not sure what the result of rsync execution will be, you can first simulate the result of execution with the-n or-- dry-run parameter.
In the above command of $rsync-anv source/ destination, the-n argument simulates the result of the command execution, but does not actually execute the command. The-v parameter outputs the result to the terminal so that you can see what will be synchronized.
The destination directory becomes a mirrored copy of the source directory
By default, rsync only ensures that all contents of the source directory, except files that are explicitly excluded, are copied to the destination directory. It does not keep the two directories the same and does not delete files. If you want the destination directory to be a mirrored copy of the source directory, you must use the-- delete parameter, which deletes files that exist only in the destination directory but not in the source directory.
In the above command of $rsync-av-- delete source/ destination, the-- delete parameter makes destination a mirror of source.
Exclude Fil
Sometimes, when we want to exclude certain files or directories during synchronization, we can specify the exclusion mode with the-exclude parameter.
$rsync-av-- exclude='*.txt' source/ destination # or $rsync-av-- exclude'* .txt 'source/ destination excludes all TXT files.
Note that rsync synchronizes hidden files that start with "dot". If you want to exclude hidden files, you can write-- exclude= ". *".
If you want to exclude all files in a directory, but do not want to exclude the directory itself, you can write something like this.
$rsync-av-- exclude 'dir1/*' source/ destination multiple exclusion modes, you can use multiple-- exclude parameters.
Rsync-av-- exclude 'file1.txt'-- exclude' dir1/*' source/ destination multiple exclusion modes can also take advantage of the extension of Bash's large extension, with only one-- exclude parameter.
$rsync-av-- exclude= {'file1.txt','dir1/*'} source/ destination if you exclude many patterns, you can write them to a file, one line for each pattern, and then specify the file with the-- exclude-from parameter.
The-- include parameter is used to specify the file mode that must be synchronized, often in conjunction with-- exclude.
$rsync-av-- include= "* .txt"-- exclude='*' source/ destination the above command specifies that when synchronizing, all files are excluded, but TXT files are included.
Thank you for your reading, the above is the content of "how to use the rsync command of Linux". After the study of this article, I believe you have a deeper understanding of how to use the rsync command of Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.