bash rsync - ghdrako/doc_snipets GitHub Wiki
Syntax
The rsync utility expressions take the following form:
Local to Local: rsync [OPTION]... [SRC]... DEST
Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST
Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]
- OPTION - The rsync options .
- SRC - Source directory.
- DEST - Destination directory.
- USER - Remote username.
- HOST - Remote hostname or IP Address.
The most widely used options are:
-
-a, --archive
archive mode, which allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps.-a
option of rsync is equivalent to-rlptgoD
options.- The -r option is for recursively transferring directories.
- The -l option is for keeping symlinks.
- The -p and the -t options are for keeping file permissions and modification times of source files, respectively.
- The -g and the -o options are necessary for keeping the group and the owner of source files, respectively. The -o option needs root privileges.
- the -D option is for copying device files and special files such as FIFO files and named sockets. We must have root privileges for transferring device files.
-
z, --compress
. This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow. -
-P
, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections. -
--delete
. When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring. -
-q, --quiet
. Use this option if you want to suppress non-error messages. -
-e
. This option allows you to choose a different remote shell. By default, rsync is configured to use ssh. -
-v
option to see the verbose output of the command.
Copy/Sync Files
sync a single file on a local machine from one location to another location. Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.
rsync -zvh backup.tar.gz /tmp/backups/
rsync -a /opt/filename.zip /tmp/
rsync -a /opt/filename.zip /tmp/newfilename.zip
rsync -v tmp/ata*.txt /backup/
Copy/Sync a Directory on Local Computer
rsync -avzh /root/rpmpkgs /tmp/backups/
rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/ # if dest dir does not exist rsync will create it
It is worth mentioning that rsync gives different treatment to the source directories with a trailing slash (/). If the source directory has a trailing slash, the command will copy only the directory contents to the destination directory. When the trailing slash is omitted, rsync copies the source directory inside the destination directory.
Copy/Sync Files and Directory to or From a Server
rsync -avzh /root/rpmpkgs [email protected]:/root/
rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/
Copy/Sync a Remote Directory to a Local Machine
rsync -avzh [email protected]:/root/rpmpkgs /tmp/myrpms
Rsync Over SSH
Copy a File from a Remote Server to a Local Server with SSH
rsync -avzhe ssh [email protected]:/root/anaconda-ks.cfg /tmp
Copy a File from a Local Server to a Remote Server with SSH
rsync -avzhe ssh backup.tar.gz [email protected]:/backups/
If SSH on the remote host is listening on a port other than the default 22 , specify the port using the -e option:
rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/
When transferring large amounts of data it is recommended to run the rsync command inside a screen session or to use the -P option:
rsync -a -P remote_user@remote_host_or_ip:/opt/media/ /opt/media/
Show progress
rsync -avzhe ssh --progress /root/rpmpkgs [email protected]:/root/rpmpkgs
Use of –include and –exclude Options
rsync -avze ssh --include 'R*' --exclude '*' [email protected]:/var/lib/rpm/ /root/rpm
rsync -az --delete --exclude=.git --exclude=data source destination
rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory # exclude the node_modules and tmp directories
The second option is to use the --exclude-from option and specify the files and directories you want to exclude in a file.
rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/
Use of –delete Option
If a file or directory does not exist at the source, but already exists at the destination, you might want to delete that existing file/directory at the target while syncing.
rsync -avz --delete [email protected]:/var/lib/rpm/ /root/rpm/