Can someone explain this rsync command for me?

Can anybody tell me what this does?

rsync -uva --delete /srv/bkp01 /srv/offbk2/ > ~/offbkp01.log

Does it delete bkp01 and offbk2 and sends a log to offbkp01.log?


To break this up into parts, there are two major sections, followed by a summary and notes:

  • The rsync command:
    rsync -uva --delete /srv/bkp01 /srv/offbk2/
  • the output redirection done by the shell running the rsync command:
    rsync-command > ~/offbkp01.log

rsync command:

Base command:

  • rsync - I assume this needs no explanation (but will provide if requested).

Command options:

  • -uva - A set of single character options, being (short then long form shown):

    • -u --update - Tells rsync that all modifications should only apply to files on the destination which are the same age (and different sizes) or older than the ones on the source. That is, rsync should only pass on the changes from the source that would update the destination, but it should not make any changes that would make a destination file become an older version.

      There are some exceptions. Differences in type (e.g., one is a file, the other is a directory) will cause an update regardless of dates. Symlinks and other special files ignore this option. And, importantly, this does not affect which files get deleted.

    • -v --verbose - Increases the amount of information rsync prints out while executing. Without this, rsync will print (almost?) nothing. With this option as-is, rsync will tell you which files are being transferred, and put a summary at the end. More vs increase the information printed, although the man page says more than two vs should only be used when debugging.
    • -a --archive - This is equivalent to several other options (-rlptgD) packaged together. As the long name suggests, it's a quick way to set many common options for archiving files and directories. It will:
      • -r --recursive - Tells rsync to copy directories recursively. Without this, or the more particular -d --dirs option, rsync would simply skip any directories it encountered (and tell you about it in its output).
      • -l --links - When the source is a symlink, create a symlink on the destination. Depending on other options, symlinks may otherwise be skipped or made into a full separate copy (i.e., a true directory with contents (or another copy of the file) instead of the symlink).
      • -p --perms - Sets the permissions of the destination files to the same as the source files.
      • -t --times - Sets the modification times of the destination files to the same as the source files. Since keeping these times in sync makes the rsync algorithm more efficient, you generally always want this to be used, explicitly or implicitly.
      • -g --group - Sets the group attribute of the destination to the same as the source. Associates by name by default, but will fall back to group ID number in some circumstances. If the receiving rsync is not run as super-user (or faking it with --fake-super), only groups that the user who invoked rsync on the receiving side is a member of can be set. Otherwise, the group will just be set to the default group of the same user mentioned before.
      • -D - Equivalent to the options --devices and --specials together. They mean:
        • --devices - Character and block device files are transferred. Has no effect if the receiving rsync is not run as super-user (or faking it with --fake-super).
        • --specials - Special files (like sockets) are transferred (they otherwise would not be).
  • --delete - Tells rsync to delete files that are on the destination, but not on the source. This is NOT affected by the update option set above (indeed, how could it be since most file-systems will not retain the needed data to see which action was newer?). This option acts on the sources after wildcard/glob expansion, so dir/* is not the same as dir/. Some important notes:
    • This is, obviously, dangerous. You should probably try it with the --dry-run option first, which will do a pretend transfer that allows you to make sure no unintended changes are made.
    • If there are any I/O (input/output) errors - that is, if anything goes wrong transferring, reading, or writing changes - delete will be ignored from that point on. This can be overridden with the --ignore-errors option, though you probably don't/won't want to do that.
    • There are several variations on the delete algorithm, about which you can read more in the man page. When the receiving rsync is version 3.0.0 or newer (as of the time of this writing) the --delete-during algorithm is used. If the receiver is older, then --delete-before is used. If it's important that the files maintain a certain sort of consistency should an error occur (such as not deleting anything until all other changes are made), you should use a more specific option than the default one.

Command locations:

  • /srv/bkp01 - The source file or directory. There are some detailed rules on what it means if there is/isn't a slash on the end of the directory, and if the other directory exists. You can see the USAGE section of the man page for the details, but here the lack of a trailing slash means this directory will be copied into the destination, rather than this directory's contents being copied into the destination. Same if this is a file rather than a directory.
  • /srv/offbk2/ - The destination directory. In general, this will always be the last item listed in the rsync command section unless an option is used to specify it elsewhere in the command.

Shell's output redirection:

Shell commands:

  • > - This redirects stdout (the standard output) from whatever is on the left to the file on the right, becoming its contents, and creating the file (if possible) if it does not already exist. Some notes:
    • You can also redirect stderr (the standard error output) to the same file by adding 2>&1 to the very end of the command above. Order is important, so be careful generalizing where to put that. The magic number 1 refers to stdout, 2 refers to stderr, & is (essentially) "the address of", and the full command snippet means "redirect the output of stderr into the stdout stream".
    • If you want to append to the log file instead of replacing it, you can use >> instead. The method to redirect stderr to the same place remains the same.
    • You can send stdout and stderr to different places by using 1> stdout_file 2> stderr_file. You can also use >> instead of > in that command to do so by appending instead of replacing.

Shell variables / special symbols:

  • ~ - This is shorthand for the current user's home directory, and forms part of the next bit.

Shell-level locations:

  • ~/offbkp01.log - the file offbkp01.log inside the current user's home directory. This is where the output of the rsync command will be put because of the redirection with >.

Summary:

In summary, this command will:

  • Copy all regular files, directories, symlinks, special files, and device files
  • as well as most, if not all, important file attributes,
  • from /srv/bkp01,
  • to /srv/offbk2/bkp01,
  • and it will delete anything in /srv/offbk2/ that is not in /srv/bkp01, with the following conditions:

    • If a regular file, directory, or symlink(?) has a modification time on /srv/offbk2/ that is newer than its modification time on /srv/bkp01, it will not be updated, if both items are still the same type (regular file, directory, or symlink(?)).
  • Additionally, rsync will print out some information about what it is doing (specifically, which files are being transferred, and a final summary), which the shell will write into the file ~/offbkp01.log. That log file will be created anew if it does not already exist, and will be overwritten if it does already exist.


NB: --fake-super is an option used to cause rsync to simulate super-user activities. It does this by using the files' extended attributes to store the attributes it could not set without having greater privileges. The real version of each attribute is set to whatever makes sense given the options and the privileges available. While this makes it convenient to make some backups even if you don't have super-user privileges, one should also note that it may pose a security risk greater than the use of rsync in general, as the real security attributes may not be set properly, even though the information about them was backed-up.


Source: lots of time working with rsync and reading its man-page.


From the manual:

--delete

This tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized. You must haveasked rsync to send the whole directory (e.g. "dir" or "dir/") without using a wildcard for the directory’s contents (e.g. "dir/*") since the wildcard is expanded by the shell and rsync thus gets a request to transfer individual files, not the files’ parent directory. Files that are excluded from the transfer are also excluded from being deleted unless you use the --delete-excluded option or mark the rules as only matching on the sending side (see the include/exclude modifiers in the FILTER RULES section).

So it will delete files from the destination that are not part of the original directory if you did not use wildcards.

Source directory:

1.txt
2.txt
3.txt

Destination directory:

4.txt

4.txt will be gone after the rsync and 1.txt, 2.txt and 3.txt will be on the destination.


Regarding the other things:

uva
  • update files
  • verbose: echo results to terminal (ie. redirect it to your log file so yes to the last question)
  • archive: archive mode; equals -rlptgoD (no -H,-A,-X)

No, it only delete extraneous files from bkp01 directory.

Also:

  • -u, --update - skip files that are newer on the receiver
  • -v, --verbose - increase verbosity
  • -a, --archive - archive mode; equals -rlptgoD (no -H,-A,-X)

The output of rsync -uva --delete /srv/bkp01 /srv/offbk2/ command is send to ~/offbkp01.log file using redirection operator (>) instead to be printed in your terminal. If the file doesn't exists it will be created. If the file exists, the old content it will be erased.

See man rsync for more info.


rsync 

Remote file copy - Synchronize file trees across local disks, directories or across a network.

-u update 

-v verbose

-a archive


--delete --> delete

Here it will delete files inside offbk2 .

Then the output of the result ( success or not ) will be redirected to that log file .