In this article we will help you to sync data using rsync over ssh in Linux Operating System.
What is requirements ?
Ubuntu, Debian or CentOS should be installed on your computer or server.
SSH access if you don’t have ssh installed kindly install it.
sudo apt-get install openssh-server
sudo yum install -y openssh-server
Rsync is a great and simple tool that allows you to transfer and synchronize data between servers and clients. Using SSH you can perform this tool.
Install rsync using command
If this command is not available by default in the server we can easily add it using the default package manager from repository :
sudo apt-get install rsync
sudo yum install rsync
Copy a file from local server to remote server:
rsync -v -e ssh /home/ubuntu/textfile.txt email@example.com:/home/remoteuser/data
In the above example xxx.xxx.xxx.xxx means remote server IP, we will copy a file called textfile.txt from the current server to the remote server and will place it inside the folder /home/remoteuser/data
If the remote server is configured with not default SSH port (other than 22) we can specify that inside the -e option as below :
rsync -v -e "ssh -p2020" /home/ubuntu/textfile.txt firstname.lastname@example.org:/home/remoteuser/data
Again the textfile.txt will be copied inside the /home/remoteuser/data folder on the remote server.
We can copy a file from remote server into a local folder:
rsync -v -e ssh email@example.com:/home/remoteuser/data/textfile.txt /home/ubuntu/
In the above example we will copy a file called textfile.txt from the remote server to a local folder called /home/ubuntu//.
To synchronize local folder on remote server:
rsync -r -a -v -e ssh --delete /home/ubuntu/Lfolder firstname.lastname@example.org:/home/remoteuser/Rfolder
Synchronize folder from the remote server on the local server:
rsync -r -a -v -e ssh --delete email@example.com:/home/remoteuser/Rfolder /home/ubuntu/Lfolder
Why we should use “/” at the end of path:
If “/” is used at the end of the source folder, rsync will copy the content of the folder. but if you are not using “/” at the end of source folder, rsync will copy the folder itself and the content of the folder.
If “/” is used at the end of the destination folder, rsync will paste the data directly in the folder. But if you are not using “/” at the end of destination folder, rsync will create a folder with that name and paste the data inside that folder.
Here is a list of options available for common rsync:
- –delete – delete files that don’t exist on sender’s server
- -v – verbose (-vv – This will provide more detailed information while running command )
- -e “ssh options” – This option specify the ssh as remote shell
- -a – archive mode – it is preserving permissions
- -r – recursively into directory
- -z – compress data during transfer
- –exclude ‘foldername’ – excludes the mention folder from transfer
- -P – show progress during transferring data
This article is contributed by RootLinuxBlog.
If you like RootLinuxBlog and would like to contribute, you can submit an
article using contact us from. See your article appearing on the RootLinuxBlog
main page and help other Techies.
Please write comments if you found any error with the above article would really help us to serve you a better way. Thank you…