rsync: A Backup Strategy for Modern Times - Page 2
rsync CommandsThe following command makes copies on the same machine. The file or directory being copied is named first and the destination directory second:
$ rsync -a sourcedir destinationdir
To copy a directory from the local machine to a remote machine (note that rsync must be present on both machines):
$ rsync -a sourcedir remotehost:destinationdir
To copy a directory from the remote machine to a local machine:
$ rsync -a remotehost:/sourcedir destinationdir
It is best to be explicit and use full filepaths, even when copying from the current directory.
-a, or --archive, is shorthand for -rlptgoD:
- r = Copy directories recursively. Without this switch, directories will not get copied at all.
- l = Recreate symlinks.
- p = Preserve permissions.
- t = Transfer modification times and update them on the remote system. Must have this for accurate synchronization.
- g = Set the group of the destination file to be the same as the source file.
- o = Set the owner of the destination file to be the same as the source file.
- D = Re-create character and block devices on the remote system (only root can do this).
To transfer over a ssh tunnel, use this command:
$ rsync -a -e ssh sourcedir email@example.com:/destinationdir/
-e ssh means "replace the native rsh protocol with ssh." If there is some other secure tunnel you want to use, this is where to name it.
Also, mind your trailing slashes, as they make a difference to rsync in the source arguments. A trailing "/" on a source argument means "copy the contents of this directory." Leaving off a trailing slash means "copy the directory and its contents."