Now I’ll present to you probably my favorite Linux command: rsync. Rsync is a fast and versatile 1
utility that synchronizes files and directories between two locations that can be local or remote. 2
Think of rsync as of a very complex command, with tons of options, that is widely used for backups 3
and mirroring. It’s like an improved copy command. 4
I, myself, use rsync for personal and professional backups frequently. When you have rsync available, 5
you don't need a complex commercial application for most types of backups. 6
I think is preinstalled on all major Linux distributions and on Mac OS. 7
Let's get started with a basic example. 8
Suppose you want a backup of the/etc directory where the entire configuration of the system resides. 9
Instead of copying the directory using thescp command you can use rsync. The basic syntax is rsync 10
some options 11
one or more source files and then the destination 12
directory. The user that runs the rsync command must have read permissions on the source location and 13
write permissions on the destination location 14
and that’s why I’m running the 15
command as root. 16
sudo rsync -av the sourse /etc/ and the destination, for example ~ in the user's home directly 17
/etc-backup/ 18
The -a option is telling rsync to run in archive mode and it means it will copy directories 19
recursively, preserve symlinks, the owner, the group and the permissions. It will copy the files as 20
they are. 21
The -v option indicates to run in verbose mode. rsync will print out everything it does. 22
As the destination directory does not exist, rsync will create it. 23
I'm hitting enter and after entering the password, I'll see how it's copying the files and directories. 24
When it's done, it's displaying a short summary. 25
Though it looks like it does the same as cp, in fact, it doesn't; if I run the command again, it won't 26
copy the same files in directories. 27
In fact, it won't copy a single file. 28
It will do nothing. 29
Rsync finds files that need to be transferred using a quick check algorithm that looks for files that 30
have changed in size or in last modified time. Since no file was exchanged, rsync does nothing. 31
This is more efficient compared to the cp command meant we would have copied again each file. 32
By the way, if you don't want to display output messages 33
use the -q option, which stands for quiet instead of v. 34
It won't print a thing. 35
Now, let's suppose something has changed in the sourse location, like, for example, the user has 36
changed its password and that means that the shadow file was changed. 37
Let's change the user's password. 38
OK, the password was changed. 39
And I'm running the same rsying command again. 40
I'll use V instead of Q. 41
I want to be verbose. 42
And I'm hitting enter and we notice how it found the file that was changed and copied only that file. 43
The shadow file! This is a big improvement. 44
Let's see what will happen if new files are created in the sourse location. As route I'm creating a 45
new file in /etc. 46
And I'm the running rsync command again. 47
And as we've expected, just that file was copied, now I'm removing the file from the sourse location. 48
I'm running the rsync command again, expecting to remove the file from the destination location as 49
well. 50
But the file was not removed from the destination. It's in the destination directory. 51
To make rsync remove such files add the ---delete option. This is useful for mirroring, when 52
you want two directories to always have the same contents. 53
I'm adding --delete. 54
And the file was removed. Until this point in the course, we gave no importance to a trailing slash 55
in a directory name. 56
So cd /etc/ is the same as cd /etc without a trailing slash or ls /etc is the 57
same as ls /etc/ 58
So a trailing slash doesn't matter. 59
However, rysinc gives different treatment to the source directories with a trialing slash. Let's see the rsync command 60
again. If you add a trailing slash to the source directory name, it will copy only the contents of 61
the directory to the destination directory. 62
In my previous example, there was a trailing slash and that meant that it copied the contents of /etc 63
inside the destination directory, inside the etc backup. 64
If the trailing slash is omitted, rysinc will copy the source directory itself inside the destination 65
directory. 66
So this command will copy the directories /etc in the destination directory. 67
I'm hitting enter. 68
And in the destination directory, which is etc-backup in user's home directory, this one, a new 69
directory called etc appeared, this one; this is, in fact, the source directory that was created 70
by rsync. 71
Sometimes you want to exclude some specific file types from synchronization. When I back up my personal 72
files 73
I want to exclude the files that are extremely large, like movies or files that are not important to 74
me. 75
Let's create a new directory tree! 76
Let's consider this directory tree! 77
To exclude files and directories use the --exclude-files option. 78
In the directory tree, there are two other directories and a few regular files inside; and I'm creating 79
a new one, movie1.mkv 80
OK, I want rsync exclude from synchronization movie1.mkv, dir1 and all files of type pmg. 81
To do that, I'm creating a new file and on each line I'll write a file or directory name to be excluded. 82
So vim exclude files.txt All the paths are relative to the source location: movie1.mkv dir 1 83
and the trailing slash ( to exclude the directory a trailing slash after its name) 84
And I can also exclude based on a pattern that matches the file or the directory name; for example, 85
to exclude all pmg files, I'll add such a line. 86
*.png I'm saving and exiting the file. 87
Perfect! And I'm running the rysing command, using the --exclude- from option 88
--exclude-from= 89
and between quotes, the path to the final, and the current directory 90
exclude_files.txt the source, my project, 91
and the destination; it will create a new directory called Backup in the current worketing directory 92
and I'm hitting enter. 93
And we see rsync has excluded the files and directories specified in the excluded file. 94
It's important to use a correct path to the file, and I recommend you to use an absolute one that's 95
always valid. 96
So it would be better to use an absolute path here, like home student and the name of the file. 97
This command will be valid no matter the directory you are in when you run it. 98
It's much better. 99
If you want to quickly exclude a file or a directory there’s also the --exclude option which 100
takes a file or a directory 101
as argument in the command line like this: rsync -av --exclude= and between 102
quotes, a file or directory name to exclude. 103
You can also use patterns like *.png 104
The *.png files were excluded. And to exclude multiple files or directories simply specify multiple exclude 105
options. 106
I'm removing the backup directory. 107
And I'm running the rsyinc command again. I'm adding a new exclude option like this -- exclude = 108
let's say a.pdf; the file will be excluded as well. 109
And you see, it was excluded. 110
That’s all about rsync for the moment. Remember that rsync is a very complex tool with tons of options and 111
you can see them all in its man page. In this lecture I’ve just shown you the most useful ones. 112
In this lecture, I've shown you the most useful ones, the ones that I'm using often. We will take a short 113
break, and in the next video, I'll show you how to synchronize files and directories over the network 114
using rsync and ssh.