Article Title: Synchronize UNIX files. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
There are many tools available for cross-UNIX®Directory file synchronization, but to effectively and safely complete this task, you need to do more work. This article describes how to securely Synchronize files across UNIX file systems and different computer systems, including how to implement encrypted versions of backup synchronization files.
File synchronization is the process of adding, modifying, or deleting a file in another location. This article discusses three utilities, cp, tar, and rsync, which both help to synchronize UNIX files. The synchronization functions of the cp and tar commands are limited, while rsync provides comprehensive options. However, they all have their own application scenarios.
Run the cp command to copy data directly.
Although the cp command is not a real synchronization command, it may be the easiest way to copy files between two locations. For single file replication, cp is obviously very efficient: $ cp source destination.
To copy the entire directory structure, you can use the-r option to recursively copy the entire directory structure from one location to another: $ cp-r source destination. This replication method only Recursively copies files and directories. The permissions, owner, and other metadata of the object are not copied to the target location. You can use the-p option to retain the owner, permission, and time of each copied file and directory: $ cp-pr source destination.
Using the cp command is the easiest and most common method to copy files, but the cp efficiency is very low. If you do not use a remote file system solution such as NFS, it is impossible to copy the directory to a remote system.
Use tar
The tar (short for tape archive) utility was originally used to efficiently convert the directory structure (including file and file metadata) to a binary stream, and then you can write the binary stream to the backup tape.
You can use tar to create a. tar file that contains the required Directory: $ tar cf mydir.tar./mydir. Option c asks tar to create a new archive file. option f uses the following parameters to specify the name of the archive file to be created (mydir.tar ). Other parameters specify the files or directories that should be included in the archive file. The tar command automatically recursively traverses the directory structure. If you specify to include a directory, tar will include this directory and all the files and directories in the archive file.
An important feature of tar is that the path name specified by the user is regarded as an absolute path. That is to say, if you specify the complete directory location for tar, for example, the/etc directory: $ tar cf etc.tar/etc. By default, tar extracts files to their absolute positions. For example, if you extract the archive file $ tar xf etc.tar, the file and directory structure will be re-created in the/etc directory. This may cause damage (it may overwrite the file to be retained in/etc ). There are two solutions to this problem. The first method is to use GNU tar, which supports deleting elements from the extracted path through the -- strip-path option.
Another simple solution is to enter the parent directory and use the relative path (see Listing 1 ).
Listing 1. Enter the parent directory and use the relative path
$ Cd/
$ Tar cf etc.tar./etc |
When extracting an archive file, the file is re-created in the relative position. You can use this method to synchronize directories. Because tar creates the byte stream of the directory structure, you can use tar and pipeline to copy the file from one location to another: $ tar cf -. /etc | (cd/backup; tar xf -). "-" Specifies that tar should use standard output (During writing) or standard input (during reading ). Parentheses allow the statement to be executed in a subshell. Let's take a look at the code before the pipeline operator, which creates the byte stream of the file and sends it to the standard output. After the pipeline operator, switch to another directory and extract the byte stream from the standard input.
To retain the owner and permissions of a file, you can use the p option to retain the metadata of each file and directory: $ tar cfp-./etc | (cd/backup; tar xfp -).
After mastering this basic structure, you can perform more complex operations. For example, you can copy only the modified files after a specified time: $ tar cf--- newer 20090101./etc | (cd/backup; tar xf -). This command creates a copy of the file modified after January 1, January 1, 2009.
By using rsh or ssh, you can also synchronize files to the remote host: $ tar cfp-./etc | (ssh user @ host -- tar xfp -). Using ssh and tar in this way is a good way to create local file backup on a remote host. However, there are more efficient information synchronization methods.
[1] [2] [3] [4] Next page