Grundlagen - rsync
Rsync (Remote Sync)
is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.
Some advantages and features of Rsync command:
- It efficiently copies and sync files to or from a remote system.
- Supports copying links, devices, owners, groups and permissions.
- It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
- Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
Basic syntax of rsync command:
# rsync options source destination
Some common options used with rsync commands:
-v : verbose
-r : copies data recursively (but don’t preserve timestamps and permission while transferring data
-a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
-z : compress file data
-h : human-readable, output numbers in a human-readable format
Rsync Examples
When specifying the Rsync data parameters, the following always applies: “SOURCE” “DESTINATION
Basic Copy/Sync Files and Directory
Attention: the examples still have a ”–dry-run
“ in them. If you want to copy data effectively and not only test the command, you have to remove this parameter
Copy/Sync Files and Directory locally
$ rsync -av /mnt/sourcefiles/webproject1/* /var/www/html/webproject1/ --delete --dry-run
Copy/Sync Files and Directory from a Server to local
$ rsync -av remote.serverdomain.com:/volume1/stuff/folder/ /mnt/localstorage/folder/other-folder/ --delete --dry-run
Copy/Sync Files and Directory from local to a Server
$ rsync -av remote.serverdomain.com:/volume1/stuff/folder/ /mnt/localstorage/folder/other-folder/ --delete --dry-run
Using custom port for rsync over ssh
To use another port with rsync, you can use the -e
option to create an ssh tunnel and then using normal ssh parameter:
$ rsync -a -e "ssh -p 14022" /local/dir user@host:/backup/dir
For example, if you want to force using IPv6 addresses, just add standard -6
ssh parameter to above command:
$ rsync -a -e "ssh -p 14022 -6" /local/dir user@host:/backup/dir
You can also combine any other ssh parameters in this way.