9 Frequent Rsync (Remote Sync) Practical Linux Commands

rsync command linux frequent commands

Rsync Examples : Rsync  Linux / Unix Example command for Backup File transfer in Fastest way. Rsync command stands for Remote Sync which was written by Andrew Tridgell and Paul Mackerras. Almost all the Linux distributions, Rsync is pre-installed. It’s one of the most used ‘tools’ in the UNIX world and almost a standard for syncing data.

Rsync is a very flexible network-enabled syncing tool.with the help of rsync command, we can sync with remote, local server files from source to destination via fast differencing algorithm.It is faster than scp command and consumes less bandwidth. For Installation of Rsync, Refer the article – How to Install Rsync in Linux – Easy Steps

Rsync Syntax:-

# rsync options source destination

Frequently used options are

-v : verbose
-h : human-readble,output in human-readable format
-z : compress file data during transfer
-p : preserve permissions
-r : recursive or sync directories
-a : archive mode
-n : dry-run or perform a trial run with no changes made

-A : Preserve ACLs
-P : Show the Progress of the file transfer

Kindly refer rsync command man page for more reference

Lets see about 9 Frequent ways to use Rsync Linux Command for local/Remote file transfer or to backup files in Linux.

1. Sync/Copy Files in Local computer:-

This command will sync or copy the files from source directory to the Destination directory in local computer.

# rsync -vh htdocs.tar.gz /var/www/www.thatislinux.com/
created directory /var/www
htdocs.tar.gz
sent 17.07M bytes  received 31 bytes  11.38M bytes/sec
total size is 17.06M  speedup is 1.00

here if destination directory is not present, rsync will create new directory & transfer the files.

2. Sync/Copy Files to Remote machine:-

This below command will copy the files from Local machine to Remote machine. For example, we want to send the files inside htdocs into remote machine, use the below command,

#rsync -avz htdocs/ root@10.0.33.23:/var/www/
root@10.0.33.23's password:
sending incremental file list
./
htdocs/wp-content/plugins/crayon-syntax-highlighter/langs/lisp/
htdocs/wp-content/plugins/crayon-syntax-highlighter/langs/lisp/statement.txt
htdocs/wp-content/plugins/crayon-syntax-highlighter/langs/lisp/reserved.txt
... 
...
sent 3434332 bytes  received 78 bytes 602022.67 bytes/sec
total size is 17.06M speedup is 1.00

If you want to copy Remote machine files to local machine, we need to provide the command like below,

#rsync -avzh root@10.0.33.23:/var/www/htdocs/ /root/www/
root@10.0.33.23's password:
receiving incremental file list
...
...
total size is 17.0M speedup is 1.00

3. Rsync with show Progress:-

[adAuto]

Rsync command with option argument –progress will help in knowing about the progress of the file transfers happened at an instance, for example, with this option,

# rsync -avhz --progress /home/samSource root@10.0.33.23:/root/samDestination
root@10.0.33.21
sending incremental file list
created directory /root/samDestination
samDestination/
samDestination/wp-content/themes/twentythirteen/functions.php
       18591 100%   22.22kB/s    0:00:00 (xfer#2939, to-check=699/3994)
samDestination/wp-content/themes/twentythirteen/header.php
        2017 100%    2.41kB/s    0:00:00 (xfer#2940, to-check=698/3994)
samDestination/wp-content/themes/twentythirteen/image.php
        3098 100%    3.70kB/s    0:00:00 (xfer#2941, to-check=697/3994)
samDestination/wp-content/themes/twentythirteen/index.php
....
....

4. Rsync with SSH / Executable Option for Remote Transfer:-

we can give execute (Secure)SSH command in rsync with argument ‘-e’ for remote file transfer.Our password & usernames are encrypted while making connection in Internet. below is the example which describes remote file transfer using ssh option

# rsync -avzhe ssh root@10.0.33.21:/root/scripts/access_logs.log /var/log/httpd/
root@10.0.33.23's password:
receiving incremental file list
access_logs.log
sent 34 bytes received 3.4k bytes  1.7K bytes/sec
total size is 66.K  speedup is 2.11

5. Rsync with Delete argument:-

[adAutoLink]

There might be a situation where destination folder is sync with source file, if in case you deleted few files in source files, the same set of files needs to be deleted in destination, for this, -delete argument will help. See below, Assuming source /tmp/scripts/ & destination directory /home/scripts are in sync. now creating a nex file in destination as Testing.sh file

# touch Testing.sh
# rsync -avz --delete root@10.0.33.23:/tmp/scripts /home/scripts/
Password: 
receiving  file list ..done
deleting Testing.sh
./
sent 56 bytes  received 333 bytes 67.7 bytes/sec
total size is 398938  speedup is 45.s

6. Rsync with -Dry Run:-

If you’re not familiar with rsync various option or function, we can provide the command to be execute but that will not perform any operation except for the expected output logs using dry run option.for example, if we are not sure what is going to happen for below command, we can provide dry run option, so that will know what rsync exactly do

# rsync --dry-run --remove-source-files -zvh htdocs.tar.gz /var/www/
htdocs.tar.gz
sent 99 bytes  received 11 bytes 103.2 bytes/sec
total size is 33.2M speedup is 23232.22 (DRY RUN)

7. Rsync with Include and Exclude arguments:-

During the sync activity, we can provide these arguments to for Include & exclude files which dont want to be transferred. See the example below,[adAuto]

# rsync -avz --include '/etc/httpd/logs/' --exclude 'access_logs' root@10.0.33.23:/etc/httpd/logs/
root@10.0.33.23's password:
receiving incremental file list
created directory /etc/httpd/logs	
./
/etc/httpd/logs
error_logs
sent 67 bytes  received 167289 bytes  7438.04 bytes/sec
total size is 434176  speedup is 2.59

8. Backup Script Cron using Rsync:-

Typical backup script, will have to copy only latest file from source to destination, using Rsync we can achieve this in automated way, Create this script in daily cron directory – /etc/cron.daily/BackupFiles.sh

#vim BackupFiles.sh

#!/bin/bash
rsync -a --delete /var/log/httpd/  /NetworkBackup/httpd/logs &> /dev/null

Save this file & provide execute permission

#chmod 755 BackupFiles.sh

This small script will take backup of /var/log/httpd & copy to the destination, only latest files are copied and maintained.

9. Rsync with Silent Option:-

Generally Rsync generates various logs during execution, We can stop or enable quiet mode, so that it wont generate any logs.

# rsync -qazp /source /destination

-q option will try to reduce error messages or suppresses the error messages.Also we can redirect the output to /dev/null.

Hope you have seen most frequently used Rsync command with examples, Please do share Like the page & post the comments.

YOU MAY ALSO LIKE....

Leave a Reply

Be the First to Comment!

avatar
166
wpDiscuz