Pages

Saturday, December 12, 2015

rsync command one liners


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

1.       It efficiently copies and sync files to or from a remote system.
2.       Supports copying links, devices, owners, groups and permissions.
3.       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.
4.       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

1.       Copy/Sync Files Locally

# rsync -zvh junk_file.tar /tmp/backup
Note: if the destination is not already exists rsync will create a directory automatically for destination.

2.       Copy/Sync a Directory on Local Computer

# rsync -avzh /root/admin /tmp/backup/
# rsync -avzh /root/admin/ /tmp/backup1/
Note: Observe difference in both the folder command above just to observe difference in use of / in source path

3.       Copy/Sync a Directory from Local Server to a Remote Server

# rsync -avz /root/admin/ root@192.168.0.101:/tmp/backup

4.       Copy/Sync a Remote Directory to a Local Machine

# rsync -avzh root@192.168.0.100:/root/admin /tmp/backup

5.       Rsync Over SSH

With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our data you can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet.

Copy a File from a Remote Server to a Local Server with SSH

# rsync -avzhe ssh root@192.168.0.100:/root/admin /tmp/backup
Note: To specify a protocol with rsync you need to give “-e” option with protocol name you want to use.

Copy a File from a Local Server to a Remote Server with SSH

# rsync -avzhe ssh /root/admin root@192.168.0.100:/tmp/backup

6.       Show Progress While Transferring Data with rsync

# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs
Note: --progress option displays the files and the time remaining to complete the transfer.

7.       Use of --include and --exclude Options

# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/root/admin /tmp/backup
Note: rsync command will include those files and directory only which starts with ‘R’ and exclude all other files and directory.

8.       Use of --delete Option

# rsync -avz --delete root@192.168.0.100:/var/lib/rpm/ .
Note: --delete option can be used to delete files in destination those are not there in source directory.


9.       Set the Max Size of Files to be Transferred

# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.100:/root/tmprpm

10.   Automatically Delete source Files after successful Transfer

# rsync --remove-source-files -zvh /root/admin /tmp/backup/

11.   Do a Dry Run with rsync

# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/
Note: Use of --dry-run option will not make any changes only do a dry run of the command and shows the output of the command

12.   Set Bandwidth Limit and Transfer File

# rsync --bwlimit=100 -avzhe ssh  /var/lib/rpm/  root@192.168.0.100:/root/tmprpm/

Note: --bwlimit option can be used to set the bandwidth limit while transferring data from one machine to another machine which helps us to limit I/O bandwidth.


13.   rsync command with an SSH key for identity

rsync -avz -e "ssh -i /ssh-private-key" /source/folder username@username.evbackup.com:destination-folder


Back To Top
Home

No comments:

Post a Comment