What is rsync?
rsync stands for remote sync. rsync is a free software computer program for Unix and Linux systems which synchronize the files and directories from one location to another in an effective way. The rsync protocol can easily be used for backups. rsync can be used as an advanced alternative for the cp command, especially for copying larger files.
Important features of rsync
- First time, rsync copies the whole content of a file or a directory from source to destination. Next time, rsync copies only the changed blocks and bytes to the destination location, which makes the transfer really fast.
- rsync allows encryption of data using ssh protocol during transfer.
- rsync uses compression and decompression method while sending and receiving data both ends. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
How do I install rsync?
Use the following commands to install rsync :
apt-get install rsync
Basic syntax of rsync command
rsync options source destination
Common rsync command options
- -v : verbose
- -r : copies data recursively
- -a : archive mode
- -z : compress file data
- -h : human-readable, output numbers in a human-readable format
- -e “ssh options” : specify the ssh as remote shell
Copy/Sync Files and Directory Locally
Copy/Sync a File on a Local Computer
This following command will sync a single file on a local machine from one location to another location. Here is the example, a file name backup.zip needs to be copied to /home/lab-informatika/backup folder.
root@lab-informatika:~$ rsync -avzh /home/lab-informatika/Documents/backup.zip /home/lab-informatika/backup
Output
sending incremental file list
backup.zip
sent 255.73M bytes received 31 bytes 26.92M bytes/sec
total size is 255.65M speedup is 1.00
Copy/Sync a Directory on a Local Computer
root@lab-informatika:~$ rsync -avzh /home/lab-informatika/Pictures/career /home/lab-informatika/backup
Output
sending incremental file list
career/
career/1.jpg
career/bg.jpg
career/landing_2_banner_1.jpg
sent 332.27K bytes received 73 bytes 664.69K bytes/sec
total size is 331.91K speedup is 1.00
Copy/Sync Files and Directory to or From a Server
Copy from Local Server to a Remote Server
This command will sync a file from a local machine to a remote machine. For example: There is a backup.sql.gz file and you want that local file’s send to a remote server, you can use following command.
root@lab-informatika:~$ rsync -avzh /home/lab-informatika/backup/backup.sql.gz user@host.domain:~/path
Output
sending incremental file list
backup.sql.gz
sent 30 bytes received 6.87M bytes 85.31K bytes/sec
total size is 6.86M speedup is 1.00
Copy/Sync a Remote Server to a Local Machine
This command will help you sync a remote file to a local directory. for example, a directory /path/backup.sql.gz which is on a remote server is being copied in your local computer in /home/lab-informatika/backup
root@lab-informatika:~$ rsync -avzh user@host.domain:~/path/backup.sql.gz /home/lab-informatika/backup
Output
receiving incremental file list
backup.sql.gz
sent 30 bytes received 6.87M bytes 85.31K bytes/sec
total size is 6.86M speedup is 1.00
Rsync Using SSH
Copy a File from a Remote Server to a Local Server with SSH
This command will help you sync a remote directory to a local directory. for example, a directory /public_html/directory which is on a remote server is being copied in your local computer in /home/lab-informatika/backup
root@lab-informatika:~$ rsync -avzhe ssh user@host.domain:~/public_html/directory /home/lab-informatika/backup
Output :
user@host.domain's password:
receiving incremental file list
directory/
directory/file.php
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Copy a File from a Local Server to a Remote Server with SSH
root@lab-informatika:~$ rsync -avzhe ssh /home/lab-informatika/backup/directory/file.php user@host.domain:~/public_html/directory
Output :
user@host.domain's password:
sending incremental file list
file.php
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Display progress While Transferring Data with rsync
To display the progress while transferring the data, we can use –progress option for it. It displays the files and the time remaining to complete the transfer.
root@lab-informatika:~$ rsync -avzh --progress user@host.domain:~/path/backup.sql.gz /home/lab-informatika/backup
Output
receiving incremental file list
backup.sql.gz
6.86M 100% 91.31kB/s 0:01:13 (xfer#1, to-check=0/1)
sent 30 bytes received 6.87M bytes 88.61K bytes/sec
total size is 6.86M speedup is 1.00
Exclude some files or directory while transferring data with rsync
This options allows us to exclude files by specifying parameters with this option helps us to exclude files and directory you don’t want to be transferred. For example this command below will transfer all files and directory from current directory except framework directory
root@lab-informatika:~$ rsync -avzh ssh --exclude 'framework' user@host.domain:~/directory /home/lab-informatika/backup
No Comments
Leave a comment Cancel