Transcript for:
Efficient Data Backup with rsync

Hello and welcome back to LearnLinux TV. In this video we're going to check out rsync. And more specifically what we're going to do is use rsync to backup files on our Linux installation. Now in this video I'll be using Nextcloud as the example server that I'll be backing up.

But what you're backing up doesn't really matter because rsync is, well, universal. What rsync does for us is it lets us sync data from one location to another, and the other location can be another server, a backup drive, a network share. And rsync is a very, very, very powerful utility, so I highly recommend you use it.

It's going to be one of those that you just can't live without once you actually learn it, so you should learn it as soon as possible. So without any further hesitation let's go ahead and dive into rsync. So here I am on the Linode dashboard and as you can see I have a number of servers here.

One of which being the backup server that I created for this video. Now what I'm going to do is back up my next cloud instance. I don't really have all that much on there but it's going to serve as an example. What I want to do is back up everything on my next cloud server over here to backup server.

Now my Nextcloud server, if I click on that, I have a special volume right here for Nextcloud data. So that's what I want to grab and then where I want to put it is right here on the backup server obviously, but specifically on the backup server I have a backup data volume and this is a block storage volume. It's 200 gigabytes so in my case it's going to have more than enough space to hold all my backups.

I think this is going to be a good example. So when I go down here to a terminal I'm already connected to my Nextcloud instance via SSH and then in this tab I'm connected to my backup server. So again what I want to do is grab data from Nextcloud and copy it over here to the backup server. But how exactly should I do that? If I list the storage you can see that I have my backup data mounted right here.

It's mounted under slash mnt slash backup data. So what I want to do is actually go into that directory. So cd slash mnt slash backup data. Right now I have absolutely nothing.

We can ignore this lost and found directory right here but... What I want to do is create a new directory that's going to serve as a directory to hold backups for my next cloud server. And what I'll do is create a sub directory for that because I might have other servers that I want to back up to this one as well. So it might actually be more cost effective to use this as a backup server for more than one instance rather than having a backup server for each and every one of my servers independently. So what I'm going to do is just make a new directory.

So what I'll do is I'll name the directory after the domain name for my Nextcloud server. In my case that's actually nextcloud.learnlinux.cloud. And then what I want to do is make sure that my user owns that directory so that way I can go ahead and move data over to it. So what I'll do is just type sudo chown. I'll use my username and my group.

I'll make it recursive even though I don't have anything in that directory anyway. That's probably unnecessary but I want to make sure that when you create it that you're taking into account what you may or may not already have in there. So what I'll do is change the ownership so that my user in my group is the owner of that new directory that we've just created. And as you can see now it is.

So now what I'm going to do is go over here to my next cloud server and of course what I want to do is go ahead and back up everything. So what I'm going to do is switch to root. I want to make sure that I have access to everything.

And if I list the storage you can see that I have my Nextcloud data directory right here. And that's actually the directory that I want to grab and back up. So let's go ahead and do it.

So I'll type rsync and what I want to do is a dry run. And a dry run is very important to do first because it gives you a simulation of what's going to happen if you were to actually back it up. It's not going to transfer any actual files but it will give you basically an overview of what would happen.

So that way if there's any risk of overwriting something important, you'll know that before you actually run it for real. So it's just a good idea. I'm going to give it options A, B, and Z. And what I want to do is copy that Nextcloud directory which is located at slash MNT and then Nextcloud data.

And then what I want to do is back that up to my backup server. And in that case I actually have a username of j on that end of things. And I'll just grab the IP address.

I should be able to get it from here. I don't actually have a domain name on this instance so I'll just copy the IP address and that should work just fine. And then I'll paste it right here. And what I want to do is tell it where I want to store the backups or where I want to copy that data to. And obviously I need to type a path so I added a colon.

And then what I'm going to do is type slash mnt slash backup hyphen data because that's the directory on the target. And underneath there as you know I created a sub directory so I'll type that in. And if this works we should have a proper simulation of a backup. And let's see what happens. And even though this is a dry run, it's still going to try to make a connection.

So I'll say yes I do want it to connect. So I'll go ahead and put in my password and let's see what happens. And you know what? That went by very quickly.

Now down here it's actually telling us that it's a dry run. So of course it's going to happen a lot faster than a real backup would happen because it's not actually sending anything. But even if it wasn't a dry run...

This backup process would probably happen fairly quickly because, well, I don't have all that much on my Nextcloud server yet. But anyway, what I recommend you do is just look at the output and if everything looks okay here, which I wouldn't see any reason why it wouldn't, and we can back up for real. And to do that, we simply remove the dry run option. And this is going to actually copy files over.

So if I go here to the backup directory, we have our next cloud directory right there. And of course we have nothing inside that directory because it was just a simulation run. So let's go ahead and run it for real.

I did remove the try run option here so this should actually do a real backup. Let's see what happens. And as you can see it copied a bunch of files over to that other server.

Or did it? Let's see. As you can see right here it did indeed copy data over to that backup directory.

So I do have a backup of my next cloud data. So what I'm going to do actually is remove everything in this backup because I think there's a slightly better way to do it and you'll see what I mean in just a moment. But for right now I'm going to remove everything here. So I'll just do an rm-rf and then star. Now be very careful of course when you use the rm command.

This is just a sample server anyway so I'm not at risk of losing anything. But anyway I'll go ahead and press enter and now everything is empty yet again. Now back here what I want to do is change the command a little bit and this is the command right here that I used for this purpose.

And actually to make this better what I'm going to do is echo this entire string. into a script. That'll make it a lot easier to run in the future. And I'll go ahead and save that in my local directory as backup.sh. Just like that.

And there we have the command that we used. But we're going to add a little bit more to this though. So as always when it comes to a bash script we're going to type a hashbang here at the very beginning. Standard practice.

It just tells the interpreter what we want to use to actually run our script which is going to be bin bash in this case. And then what I'm going to do is create a new variable. Now the variable that I'm going to create is going to be called date.

And what I'm going to do is set that equal to something and specifically what I want to set it equal to is the current date. And to capture that I could type a dollar sign with two parentheses. And inside the parentheses I could type the date command that I want to use to capture the date in the format that I want it in.

And thankfully that's pretty easy to do. So what I'll do is type date in lowercase. So this is the name of the variable.

And it's just a coincidence that the command is also named date. But this is a real command. You could run date on the command line. It'll give you the current date.

And what I want to do is use single quotes right here. I'll type plus. percent capital F and that should be good enough. So what are we actually doing with the date?

Well this is really clever. So what I'm going to do is go to the very end of this line right here and here we have the backup destination and what I want to do is actually use the variable right here so that way I could back it up every day and get a different folder every day. Now of course this is going to use more data so you might want to look into deduplication or pruning your backups or something like that. That's beyond the scope of this particular video. This is just an example.

But anyway, what I'm going to do is save this file and I'll exit out. Let's mark it executable. And now it is.

Back here, just to double confirm, we have nothing in this directory. Let's go ahead and run the backup script. So I'll type in the password.

Well, it definitely did something. Let's go ahead and take a look at our backup server right here. And check this out.

We have a folder that's named after the date. That's really cool. Inside that directory.

we have all of our data. And this is a method that I like to use to backup servers because if you want to go to a particular point in time then you have folders that aren't named after the date. So it's really easy to find out which folder was for which backup and when it happened because it's right there in the name.

So if you wanted to restore a file from a particular day you just go into that particular date folder and there's your files. So there you go. Thank you guys so much for checking out this video on backing up with rsync.

I really appreciate it. I love creating content for you guys. It's so much fun. So let me know in the comments down below what you'd like to see next. If you found this video helpful, please consider clicking that like button because that lets YouTube know that other people might benefit from this knowledge in rsync as well and I'd really appreciate that.

As always, thank you so much for watching and I'll see you in the next video.