rsync tutorial

Rsync is a command-line utility commonly used to copy files and folders from one location to another while preserving file attributes (permissions, ownership). In other words, it is used to synchronize files and directories between two locations. Copying process can be between two local folders or between two servers

Beside the copy option, with rsync we can copy only the files that are missing or have been modified in the destination location. Also, we can delete the files on destination location which are in the meantime deleted on the source location, exclude some files and folders, sync only files which are larger or smaller than specified size, etc.
Rsync can be useful in many cases and especially when you want to keep a backup of your files

Command syntax

rsync options SOURCE DESTINATION

Simple sync between two dirs

rsync -avzP --delete /home/user/A /home/user/B

Details:

-a - sync preserving all filesystem attributes (permissions, ownership, timestamp)
-v - run verbosely
-z - compress the data during the sync (transport the data in compressed mode)
-u or --update - only updated files and skip if file is modified on destination location
-r or --recursive - transfer the directories recursively
-P - show progress
-e - instructs the rsync to use the SSH protocol for remote transfers
-i - check if there is a difference between the source and the destination
--delete - delete the files and folders in target folder that do not exist in the source, 
--remove-source-files - delete the source files after transfer
/home/user/A source folder, 
/home/user/B target folder

Sync between two servers where ssh on source server listens on port 2222

rsync -avzP -e "ssh -p 2222" root@somehost.tld:/source/ /destination

Sync missing files between two dirs

rsync --ignore-existing -av /home/user/A /home/user/B

Sync new and updated files between two dirs

rsync -aurv /home/user/A /home/user/B

Other usable options

--max-age=X - copy only files that have been modified in the last X days
--size-range='SIZE' - copy the files that are larger or smaller than a certain size (+SIZE or -SIZE). For example +100M, -500M, etc
--dry-run - check what files would be copied without actually copying
--exclude - exclude certain files or directories
--max-size=X - maximum file size that rsync will transfer (for example 50M, 500k)
--min-size=X - minimum file size that rsync will transfer (for example 50M, 500k)
--bwlimit=X - set bandwith limit (for example --bwlimit=50 means rsync won't exceed 50kB/s)
-f"+ */" -f"- *" - means copy only folders and skip the files

When used with -i option, rsync will show the details and the differences between source and destination with a few letters:

Possible letters in the output:
f - stands for file (new file on destination)
d - shows the destination file will be overwritten
t - shows the timestamp has changed
s - shows the size has changed

Leave a Reply

Your email address will not be published. Required fields are marked *