Rsync for Local to Local Backups

Eduonix Learning Solutions
A free video tutorial from Eduonix Learning Solutions
1+ Million Students Worldwide | 200+ Courses
3.9 instructor rating • 228 courses • 1,235,061 students

Learn more from the full course

Projects in Linux - Learn Linux Doing Real World Projects

An unique course which will teach you all aspects of Linux Ecosystem by working on Projects

09:04:29 of on-demand video • Updated December 2018

  • Learn to install differt Linux flavours and tinker with it
  • Learn to use Linux as a backup or streaming server
  • Learn to create Linux clusters for real world applications
  • Learn to use Linux as a web and Proxy server
  • Learn to automate your tasks using Bash script
English [Auto] All right. Welcome back. In this lesson we are going to start talking about arsing are seeing as we mentioned in the previous lesson is in native Linux tool that is used for various file related operations. It basically transfers files and directories from one place to another whether that place was on your local file system. So it's got to move it locally from one place to another on your same machine. Or it can move your files and directories or technically speaking copies them from your local machine to a remote one or from a remote one to your local machine. Let's have a look. So for the purposes of this demonstration I am going to download a web application from this good hub repository. I found it online got a download it ok get Islam install lets install it quickly. Of course if you don't know about get it is a very very famous version control system. I'm going to download it here in order to be able to use it and I'm going to clone using get. I'm going to clone this whole application. The reason I'm going to do this is that it will provide some files for us to work with using our sync. This is just to demonstrate that for example may be working with a web application maybe you are a developer and you want to back up your files on a periodic basis. And this is one of the most common uses of arsing and backup systems in general OK let's clear the screen. And again that's how to load or clone the application. OK here we go. It's called simple dash BHB dash website. Get inside get a look quickly the files that we have. OK. We have a number of files. Suppose that this is your web application and you want to have a backup of this directory containing or web application for us. So the first thing we need to in your work to confirm is that we do have the R sync it's self-install or sync is spelled like this r s y and C. So I'm going to use our music so I'm going to use and get install to install it. If you are Alessandro's system or a Redhat based system this is of course yaun install arsing. OK now I have our sync installed on my system. Now let's say that I need to transfer the files inside this directory. Simple BHB website. Let's say that was create a new directory on my system and let's call it backup. Let's put it on my desktop for example. OK. This is the backup directory. OK. And I need to transfer the files using arsing. So I'm going to type parsing just like that and then I'm going to type the source and the rest in each to the source is simple BHB website. The destination is desktop backup. OK. If I use our sync like this and not a C or a very very important usage of our sync does slash the slash at the end of the source or the destination defines a lot of the behavior that are seeing is going to fall. Let's see how if I added a slash at the end of the source this means that our sync is going to copy the files inside the directory. So it's not it's not going to create a directory that is called simple BHB dash type site rather it is going to copy just the files. Or inside this directory. OK. So let's see what happens if I used arsing just like that and I could also add there should be more verbose. There are like just appended to the options the as we have just mentioned is for verbs which will give you an output detailing what's currently being copied and are is for the course of that is to take all the directories and files that happen to be under a simple but dash BHB dash website. OK let's also add is Z if you added the Z or sent here if you add a z this means that our sync is going to compress the files while they are being copied. This may not be of a big difference if you are copying files locally on your file system as we are doing now but they might be of great advantage if you are copying those files over the network. Especially if you are applauding them to a remote web site. For example on some web server that is located even in a even in a different region than the one that you are currently in it will prove to be very efficient. Because it's kind of compressed the files before transferring them and on the destination those files are going to be decompressed. So this will significantly decrease the amount of files to be transferred. In other words it is going to decrease the bandwidth. So as a rule of thumb whenever you are sync make sure you use the Z. This will become a second nature to you and optionally used a V because V is gonna give you a detailed output. A more detailed output of what is being copied and Desch are we are going to replace it later with another option that is more comprehensive. But let's just stick with Desch are for the moment going to click enter and as you can see in less than a second. All the files were transferred they were transferred in this directory that we have just created on our desktop which is backup. If I double click on backup I'm ready to see that I have here all the directories that happen to be inside. Simple aside if I go to school to home and get a seat I have simpled Ashbridge breeder website. These are the very same files that way are there now. Let's say that I'm going to leave all those files from the backup directory on my desktop. Go again to back up. I'm going to select all and I'm going to delete those files OK. Gonna get back to my command line and this time I'm going to remove the trailing slash from the source. So I'm going to copy or sink simpled Ashby site and removed the slash at the end. If I click enter now not as what happens. Our sync created a directory for me under backup and it created this directory simpled Ashby HPF website the same name of my source. If I don't click I'm going to see that I have my files here so this is a very very important thing to watch out for. I've seen many people forget or just ignore adding a slash at the end of the source and to their surprise if they are for example sinking local Web sites with a remote one let's say for example that this is your web server and imagine what will happen if you find yourself creating a trial directory inside your main directory containing a copy of all the files that are in the main directory. Of course this is just a big waste of time and effort. You will have to delete Dyster a tree and start all over again. So again be aware of this trailing stash at the end. The most common use case of arsing is to synchronize files between source and destination directories just the files that happened to be under the directory that you are seeking. So make sure that you always use a trail slash at the source and destination. So that is the first use of arsing. However if we have a look here to go to desktop and just go to closed doors directories and going to open a new terminal window just like that. When I open both of them because I want to choose something. OK now if I go to BHB simple website or simple BHB or site Garavel as well as you probably know LS Dasch L is going to give me the long listing of the finals and directories. Those files have this time stamp 16.00 09. There have been they have been cloned today at 16 by just for PM Let's look at the backup directory at our desktop. On the other hand see the timestep you get to see that the time stamp is different although it's the same date it's December 22. However look at the time stamp the day the hour and the minute of the file you get to see that it is changed. Here is 4 0 9 p.m. And here is 4:15. This reflects the time where those files are when those files were added rather than the date of the original file. Sometimes this is the intended behavior sometimes you may want to reflect that those files are new so they need to have this new time stamp that reflects the date of their creation or the date of their copy. But most of the times we want to preserve everything that is related to the file even its time stamp. So we want to preserve the ownership. You want to preserve the file permissions you want to preserve also the time stamp if you want to do that you will have to use a very common arsing option whatsoever. Look again at the arsing command and now we are going to use the option. So instead of our are going to use the A to Z the A or as it's more commonly referred to a v the lightest. A stands for archive mode. The archive mode will combine several options of our sync together. It is going to combine the recursive mode. It is going to preserve any symbolic things that happen to be inside this directory. So for example if I have here a symbolic link that is pointing to another directory on another path inside this directory this is going to be preserved it is going to preserve ownership and permissions and it's also going to preserve the time stamp. So let's see what happens if I go two steps back from here. Or one step and I'm going to remove simple BHB of sight and I'm going to use this r sync with our new option which is Dash 8. I'm not going to forget to add the creating stash as we just mentioned a few moments ago and I'm going to click enter now. OK. Now if I get inside simple BHB courses allow inside back up at the redirect we have a look here at the timestamp again let several look at simple pre-treat. Now what does the time stamp here. Is the same 16 0 9:16. It's the very same. So let's clear the screen or let's say that I want to change the ownership of one of the files let's say functions BHB to be owned by a route. For example let's say let's make this a directory or file owned by a user it is called Jondo Dedo for the user Dedo for the group functions. APHC. Now if I run our sync command again last year the difference I will need to run it using the root user because in order to change the ownership and permissions of a directory or a file you need to be root. So I need to use the arsing command here as root in order to change the ownership of the file on the destination directory. So what's first and last functions is owned by a woman and woman. This is the destination I'm going to run arsing again and RCO what happened functions now is owned by Jadot for the user and Dedo for the group. And if you changed the permissions in here and run our sync again you'll notice that they are treated here. One last thing before I close this lesson is notice that when I ran our sync again a very interesting thing happened. No files were transferred no files were transferred again or sync intelligently determined that the files that it is about to transfer to the destination directory already exist. It does have a county that HP does have a content directory it has or it does have a function index or we add a template directory on the destination with the same files that are here with the same files at the same time stamp or the same order and so on. Perhaps only the functions file has had a different owner and group. So it just just changed the ownership of this file on the destination. But no files were transferred. This is very very efficient because if you are working on this one application and you happen to change only two or three or four or five or four files you need and transfer the hundreds or maybe thousands of files that you have on your local directory to the destination directory where those files are already exist. There you will be wasting precious bandwidth. And of course precious time and effort. So as you can see here are seeing is a very powerful tool when it comes to backing up files and directories on your local system and having lessons we are going to see more and more examples of arsing. But when you get on a remote system. So until next lesson take care.