Let’s continue the discussion about my favorite Linux command which is rsync. rsync can also be 1
used to synchronize local directories to a remote system if you have ssh access to that remote system. 2
This should be your first choice when you want to back up or mirror your server configuration over the 3
network. 4
All of the options we've seen in the previous lecture for locali sync are still available. 5
The basic syntax is very straightforward and is similar to a scp. 6
Let's get started! 7
I’ll run rsync on CentOS, which will be client and synchronize it to Ubuntu where the SSH daemon is 8
running 9
so ssh access from centos to ubuntu is necessary. 10
The first step is to check that ssh is working. 11
I want to keep any problem isolated. 12
And it's working. 13
Let's back up or synchronize the /etc directory where's the entire configuration of Centos. 14
The user of the thrust of the rsync command should have read access to each file on the source location and the 15
user that connects to the remote server should have write access on the destination. 16
So on Centos I'll run 17
I'll run the rsync command as root. In /etc there are files that can be read only by root. 18
So sudo rsync -av -e ssh 19
the source /etc/ and the destination. 20
I'll use the same format as in the case of scp 21
So the user @ the server IP address 22
: and the destination directory. 23
etc-centos. You notice that the syntax is similar to scp; the destination part is indicated as the username 24
that will connect to the remote server @ the IP address of the server, :, and the remote path 25
or where the files will be copied. 26
Note that no space is allowed between the username, the IP address and the remote path. 27
I'm hitting enter and it will start copying the files. 28
That was the sudo password and now the authentication to ssh. 29
It's done. 30
If I run it again, it won't copy anything because the source and the destination directories are already 31
synchronized. 32
Nothing was copied, but if I change anything in the sourse location, only the changes will be copied. 33
Let's add the new group, groupadd devops 34
Of course, as root. 35
And I'm also creating a new file. 36
I'm running rsync again, just to check that only the modified files will be copied to the destination, 37
overwriting the old ones. 38
Great, only those files were copied. 39
If you want to delete the files that were deleted in the source directory from the destination directory 40
add the --delete option; this is useful for mirroring when you want 2 directories to 41
always have the same contents. 42
Without this option, it will not remove such files. 43
I'm removing the file that I've just created, this one. 44
And I'm running rsync without the --delete option. 45
I'm running, in fact, the same command; it will not delete the file; you see, the file was not 46
deleted from the destination and I'm adding the --delete option. 47
Take care not to add the option between -e and ssh. 48
An the file was deleted from the destination. If you want to specify an ssh port other than the 49
default 22, then after the -e option put everything between quotes and use any option of the 50
ssh command like this. 51
This is how you specify another port. 52
Now, let's transfer data from the remote to the local machine. All I have to do is switch the source 53
and the destination. 54
So rsync - av-e 55
ssh the source 56
and this will be the remote server 57
for example, I'm copying this directory, my project. 58
So student@ the IP address : ~ and the name of the directory, my_project. 59
And I'm copying it in the current directory. 60
And the directory was copied. 61
Note that all other options that we've discussed in the previous lecture, like excluding files, are still 62
available. 63
OK, That’s all. Making a good backup and testing it is probably the first thing a good sysadmin should do. 64
In case of a disaster 65
you'll be fully covered.