Local backups with rsync (forget tar -cvf - . | (cd /dest; tar -xpf -)
By blogfinger on Feb 09, 2009
Recently, I encountered the rsync man page by accident (maybe as one of the lines in the top 10 Google search results?) and was quite surprised to find rsync examples where there was no remote host in any of its arguments. Doesn't rsync stand for something like "remote synchronization"?
So here's how it works:
If you want to copy all files in a directory and all directories and files below to another directory (for example on another file system on a different disk), use the following command:
$ rsync -avz /source_dir/ /dest_dir
Note the added slash after /source_dir. This command will recursively copy all files and directories in directory /source_dir to directory /dest_dir (will create it if it doesn't exist). If you omit the trailing slash, it will create a new directory /dest_dir/source_dir. The rsync command will copy links as links, not as the original files they point to (similar to the default behavior of Solaris or GNU tar). If the rsync command was run before at least once, it will copy only the changed or newly added files. It will not remove destination files if files have been removed in the source directory.
Example: Copy all directories and files in directory /tmp/1 to empty directory /tmp/2:
- Using the cp command (option P will copy links as links):
$ cp -Ppr /tmp/1 /tmp/2
- Using the tar command (Solaris or GNU. Solaris tar will report that a link has been created while GNU tar will only mention the file name of the link):
$ mkdir /tmp/2 $ cd /tmp/1 $ tar -cvf - . | ( cd /tmp/2; tar -xpf -)
- Using the rsync command:
$ rsync -avz /tmp/1/ /tmp/2