Hello. In this lesson we are going to look into rsync.
Yes, another utility to transfer files from one machine to
another or copy from one machine to another,
and the rsync stands for remote synchronization.
What exactly rsync is?
It's a utility for efficiently transferring and
synchronizing files within the same computer or
to a remote computer by comparing the
modification times and size of the files.
So basically, transferring files from one server A to server
B, and the way it works efficiently is because it
looks at the modification times and the sizes.
If the size is, let's suppose two gig,
it will transfer the first time two gig.
If the second time is four gig, it will only transfer
the change, which is two gig, not the entire four gig.
I'll tell you a little bit more about it. Now the rsync
is a lot faster than those tools like FTP or SCP.
This utility is mostly used for the backup of
files and directories from one server to another.
A lot of system administrators and engineers actually
have rsync command or utility running in
the crontab to actually copy files, everyday
files and modified files from one server to
another as a purpose to back them up.
The default rsync port is 22, which is
the same as SSH that SSH uses.
So there is no rsync server that you need to
install on the remote server to accept the incoming traffic.
It is going to use the SSH protocol.
Just like SCP uses the same protocol
SSH, rsync does the same thing.
So it piggybacks on port 22 SSH.
For this lecture again, we need two Linux
machine because we need to test from copying
rsynchronizing files from one server to another.
I have my first machine as MyFirstLinuxVM,
which is I referred to as Client because
I transfer files from that machine to another.
And what is another machine is my server, which I created
another CentOS VM and I named it LinuxCentOS7.
It has the same specification as
the first machine that I created.
Nothing changed.
The only thing changed is the hosting.
All right, let's look at more in a
graphical or diagram way, so I'll make you
understand how the rsync utility works.
So you have a server A or client that
needs to go to server B and it needs
to transfer those files over the utility rsync.
Now, rsync uses SSH or piggyback or SSH to
transfer the files and for that you need SSHD service
running on the destination server where the files are going
and the port by default for SSHD is 22.
Now, what it does, the rsync, it actually copies,
and copies to destination, keep the same copy, the
same size and the time from source to destination.
How it works? Let me show you right now if you have a two meg,
the size of the file is two meg of a file. It
transferred the very first time the exact same size, two meg.
Next time someone comes in and changed that file.
Now the file size becomes from two meg to eight meg.
So now you need to transfer this
file from server A to server B.
So now this is the beauty of rsync.
It is not going to take your entire
eight meg and copy it over again.
It will actually subtract those two meg that
was copied earlier and only copy the difference
in the file which is six meg.
And then ultimately the first half or the first
two meg plus the six meg is going to
combine them together to make it eight meg.
Similarly, if the file grows from eight meg to
20 meg, the rsync will subtract the difference and
only transfer the difference over to the destination server.
All right, let's look into the
technical side, how the command works.
So, basic syntax of rsync command
is rsync space the option,
if you want to specify any option, then the
source, what are you copying or transferring, destination
where are you copying, and which location.
Install rsync in your Linux machine.
So first thing we need to do is if
rsync is not already installed or the command is
not available we have to install that utility.
But first before you go ahead and
install check if it is already installed.
So if it's not do yum install rsync on CentOS
Redhead distribution. In Ubuntu/Debian, you do apt-get install
rsync. Then rsync a file on a local machine.
So you need to copy a file
from local machine. Within local machine,
you need to do, let's say if you wanted to tar.
So for that let's create a backup of the entire home directory
and we put in a tar and then we back it up.
We'll rsync a directory on a local machine,
we'll rsync a file to a remote machine, and
we'll rsync a file from the remote machine. All right.
So these are the steps we will take.
So I know it's a lot of information throwing at
you, but we'll try one by one and I'm sure
you'll understand by the time you're done with this lecture.
So I have my Linux machine open.
This is my Linux machine which is my
client and which is MyFirstLinuxVM. Oops.
Okay. Now what I need to do is, I need
to make sure I have the rsync package installed
before I actually go in and do yum install,
let's do rpm -qa, and grep for rsync.
Beautiful. It is there.
By the way, I am logged in as myself
and again my hostname is MyFirstLinuxVM.
And to check if the package is installed
or not, you don't need to be root,
you could just simply run rpm -qa.
But if you need to install a package then
yes, then you need to run yum install rsync with
the root credentials. All right, now we
have confirmed that we do have rsync package.
Now let's get into the fun part where we're going
to do the rsync of file on a local machine.
So I am in home directory and when I do ls -ltr,
you see all these directories and files.
What I wanted to do is, I wanted
to put all these directories and files into
a tar file into a container and tar it.
So for that, I'll do tar cvf backup,
this is a name I wanted to
give once it's archived and tarred,
[No audio]
and what I'm tarring, I'm tarring
everything within the same directory,
and this is the same directory which is pwd,
when I did it, it's /home/iafzal.
So that's why I'm using dot.
I could have used /home/iafzal as well.
But since I'm already in this directory,
no need to specify the absolute path.
Go ahead and hit enter.
All right, so it has tarred all
the files and directories into backup.tar.
You see, it is there. Perfect.
Now I'm going to use the rsync utility
to rsync this backup.tar into another directory,
and that another directory I'm picking is /tmp/backups.
So before I do that, I wanted to create that
backup directory, which I don't have it created yet.
So I'll do mkdir /tmp/backups.
Perfect. It is created. To test,
you could do cd /tmp/backups.
And there you go. It is.
And you are in /tmp/backups.
That's how you confirm it.
Anyway, you cd back out. Do pwd
just to make sure you are in your home directory. Yes.
Now let's run the command.
rsync -zvh.
If you wanted to know every little detail about
each of these options that I'm using, then you
should run the command man rsync.
And it will give you the information about
every option that is available to you.
Here are the different options. Anyway.
rsync -zvh backup, which is the file name that
we created and backing up where, it's going
to tell me /tmp/backups, /tmp/backups.
Go ahead and hit enter. There you go. It's sent.
It says 1.6 bytes received 35 byte size and
it took seven to 707.54 bytes per second.
It's a transfer speed.
So now go to cd /tmp/backups, and check, verify if
the backup.tar file came through or not.
Do ls -ltr, it is there. Nice.
Okay, now, next thing we wanted to do here, let
me move this up on the side, right here,
sync a directory on a local machine.
So before we synced a file, now
I want to sync a directory.
So let me go back into my home
directory by just typing Cd and hitting Enter.
Now do pwd, you're going to see /home/iafzal, do ls -ltr
to see the content inside of my directory.
Now, again, you probably have a lot of different
files in your home directory that you might not
see on my screen because I make a lot
of changes when I go through making videos.
So don't worry about it,
if my contents don't match with your contents. Okay.
So rsync a directory.
So to do that, I want to rsync everything that
I have in this directory, to this /tmp/backup directory.
For that I will use the command
rsync -azvh /home/iafzal.
Perfect. And let me move this up and I'll put this
window right here so you know exactly where I am. Okay.
And now where I am copying it to /tmp/backup.
Hit enter. Bingo. Beautiful. It works.
So let's go to /tmp/backup.
Verify everything that I have in
my home directory which is /home/iafzal.
You notice everything has been synchronized here.
Perfect. Now let's talk about the part right here,
let's move it down. It says rsync file to a remote machine.
Now for my remote machine, I
have this as my remote machine.
And I call it LinuxCentOS7.
And the IP address for this
remote machine is ifconfig enp0s3.
It is 192.168.1.58.
This is the IP address,
and this is where I want to transfer.
So let's minimize again.
So I'll go into my home directory, and I
want to copy or rsync the backup.tar file.
Now before I rsync this over to the
remote server, I want to create the destination
folder where this is going to go.
So I will go back into my
server, and I'll go into slash tmp,
and here I will create
a directory called backups. lsmtr,
and you're going to see right here
at the bottom, it has been created.
Let's clear the screen.
Now let's go back to our client machine.
This is our client machine.
Now we're going to use the command rsync -avz backup,
that's the file we wanted to transfer over.
Now you want it to transfer
over as what user, as myself.
Okay, which machine is it going to? It's going 192.168.158, which
is the IP address we just checked on our remote
server, and which directory it's going to, :/tmp/backups.
This is the directory it's going
to, go ahead and hit enter.
It's going to ask you for the password
for the iafzal on the remote machine.
Put in the password.
Beautiful. It is transferred. It says sent.
It tells you a lot of
information and speed and everything.
Now once it's transferred, it's your job
to verify it has been transferred.
Go to your cd /tmp/backups directory, so ls -ltr, and
you'll see right here,
this is the one that's been transferred. Perfect.
Let's move on.
Let me minimize this.
Now the last part is right here at the bottom.
It says rsync a file from a remote machine.
Now we want to go to the remote
machine and fetch a file from there.
So which file are we fetching?
So first let's make a test file, a dummy file.
So I'll go into my home directory because
on the server I'm logged in as myself.
Here I'm going to create a file.
I'm going to call it serverfile, do ls -ltr,
and you're going to see the serverfile is there.
Perfect. Now if I come to my client machine to pwd, and do
ls -ltr, you see I don't have that serverfile.
So I need to bring or pull that serverfile
from the server, and for that I will have to
do rsync -avzh iafzal, because this is the username I will
use to log into the remote host.
What is the IP of the remote host?
58, perfect.
Now where is that file that you are trying to bring?
The file is in /home/iafzal, and
the file name is serverfile. Perfect.
Now, when you bring it, where do you
want to save it or copy it to?
I want to copy it to my /tmp/backups file or directory.
Go to enter, it's going to ask you for the password.
Perfect. It says sent 43 bytes.
Now where did it go? Let's verify it.
I ask it to come to my /tmp/backups directory, do ls -ltr, and
you're going to see right here it says serverfile.
See, that's how you fetch a file from the server.
So anyway, there are different options that are available
on rsync that allows you to do like dry
runs, allows you to do rsync continuously, allows you
to do only delta's meaning only the changes.
You just need to pick the right option to
choose which option works for your own requirement.
Good luck.
[No audio]