In this video, you will create a script that backs up the engineering directory.
Our plan is to create a bash script that performs a backup of the engineering
directory in all its files.
We'll learn how to use shell commands like tar and how to pass arguments into
our script. Finally,
you'll test the script and add it to a crime job so it can run on a schedule.
Let's get started. Okay, so here we are in the terminal,
and for this video we're gonna create a backup script that will backup any
directory,
but we're gonna default it to use the engineering directory created earlier in
the videos. There's a few files in there,
so it'd be enough to go ahead and get it a good test.
If you recall from earlier,
we've cloned and we've pulled down all the files that are used for this video.
Um, and so those are gonna be in the section five sub subfolder.
Okay,
so let's open the backup script and let's take a look and see how it works.
So in my vim editor, again,
I'm going to go into the hands-on Lennox for cloud DevOps, cloud engineers, uh,
directory section five and
backup.sh. Okay,
so let's go to the top of this file here. We, as we always have,
we have our shebang on line one and line four we set
a variable for the backup directory, the directory we wanna do.
And then here on line five, this, uh, just like we looked at earlier,
the dash f to see if a file exists, a kind of a, a built-in dash test,
you can do one here, which is the dash z. And it basically says,
is this, is the argument being passed in empty? Is it being set?
So basically if it isn't set,
then we want to just go ahead and default to the user local engineering
directory that we've already created. Otherwise,
we want to go ahead and set the backup directory to whatever the is passed
in as the first argument to the script.
And that's where this dollar sign one comes in. This basically says,
just like you saw on the functions,
is the first parameter or argument being passed in.
So we set that on line 10 here. Let's come down to line 14.
So this is us doing, um, a sanity check.
So regardless of whatever the backup directory is,
it's the default for user local engineering or something we pass in.
If that backup directory does not exist, we wanna stop,
we want to exit right there because there's nothing to back up and we need to
fail. And so here on line 17, we do an exit one. Okay,
let's go down to line 21. Here.
We create the name of our archive and so we want this to be the name
of what we're backing up. And then also you can see here,
over here where we do this command interpolation in here with the doll
sign open peren,
we want to run this date command and in bash and in a Lennox,
the date command will print out the current date and then all these plus
percent,
Y minus percent m et cetera are just
ways to print out a format. And in this case it is year, month,
date with an underscore. And then hour,
minute and second and then we end our file in the dot Tar GZ
extension.
And that is because we are going to create a tar file and it is going to be
zipped up in case it obviously is large. We want it to be as small as possible.
Okay?
And so it's probably a good stopping point to talk about with the tar command.
So we're gonna be using the tar command to do our backup.
And tar command is an archive utility. And so if you're unfamiliar with it,
it basically will take files and directories and create an archive,
which is basically a single file with all of the directories or files inside of
it. And so then you could pass around just the one file tar also can work with,
uh, zip or in our case gun zipp, uh, or G zipp.
And what this will do with this will compress the archive to make it smaller.
And it's always a good practice to do that.
It doesn't take up a lot of resources. So, uh, we'll do that here.
Okay, line 24 here. We created a function. So as I said before, we're,
we're trying to use all the paradigms we've learned up to this point and to make
our lives a little bit easier and the code just a little bit more readable.
And so here we're going to get a count of the files and the directory to backed
up. And we name this function files to be backed up.
And we use the built in find executable to search our
backup directory rate here, looking for file types only.
This type type set a file. And then we're using the shell pipe,
meaning whatever this re result returns, whatever the fine command returns,
as a result, we're gonna pass it to the WC command, which is word count.
And we just do a list command with the dash out, which just means list.
And so this will just give us a basic number of the files that we're gonna back
up, 1, 2, 3, et cetera.
So here we get down onto line 30,
and this is where we print out how many files that we're gonna be backed up.
And as you can see here inside this double quoted string for echo,
we're using the dollar sign open parentheses,
meaning that we want to execute this function,
that this is like an execution of it.
And whatever the results are of that will then be expanded inside this string.
And you'll see that when we run it. Okay, well line 32 here.
This is where all the magic happens. So we're using what,
so the tar command and then we're using the C Z F uh, flags.
C means to create Z means to zip it up. And f is just for the file archive
here, we're taking the backup file name that we created on line 21.
So it'll be, it's gonna be under the slash temp directory.
It's gonna be called engineering underscore, uh,
date timestamp dot tar dot gz.
And the backup directory, which is whatever we set,
which is whether the default that got past, uh, the default that we set,
which is usual local engineering or whichever one we passed in.
So we have some versatility there. And then this whole line right here is just,
is a little bit funny looking,
but what this basically means is we want to take any standard error,
anything that, any error that arrives from this command,
we want to pass it to a file in the slash temp directory
called backup log.
This is just so later if there's any errors or if it doesn't work,
we can go back and see what errors, um,
have occurred during the archive creation. Okay,
so line 34 to 40, this is our another sanity check in.
You've seen this dash F before in the last V uh,
dash F before in the last video,
this we're just checking was the backup actually created. If it was,
then we're just gonna print out succeeded if otherwise printout failed.
And so that's just more of a, a, a indication to us of the state of it. Okay,
so I'm gonna go ahead and we're gonna close this shift,
colon X again and Vim to, to write and quit.
And what we're gonna do is this file is already
executable,
so I can just remove the vim from the last line and I should be able to just run
it right here. So let's go ahead and give it a try in our terminal. Okay.
So as you can see, it was really fast.
There was only three files and you could see that we,
it says backing up three files and the backup succeeded.
We did not pass any arguments,
so it went ahead and backed up the engineering directory.
Let's go ahead and look at the temp directory now. Okay,
so what we're gonna do is we're gonna look at the temp directory and you're
gonna see that there is a archive already here and
it is called temp engineering. And then the date,
month, year, and then our timestamp.
And so if we use the tar command again,
we can use the dash tf and what that'll do is it'll show us the
contents inside the tar file. So we can see that what we have there.
And as you can see it copied user, local engineering,
it copied a contributed file, the config file and the cure file.
So that gives us our three files.
There is also if we want to cat out in the slash step is that backup is the
backup log file. And so you do see an error here,
but this is actually not really an error. It is printed standard out.
It just says Tara saying, Hey,
I'm removing the leading slash from this directory because of relative paths.
You can safely ignore this. This is okay, so don't be alarmed.
So now we have a working backup file that writes the backups.
Let's go ahead and now we wanna automate this.
Obviously we don't wanna have to do this by hand every time that we want on a
backup. We want to use the built-in, uh, scheduler called kron.
So Kron runs on your Lennox host and it is a Damon that runs
and basically looks for commands to run for you on a specific cadence or or
time interval. And for our case, we're gonna want to run this,
let's just say daily. Um, that is a safe, uh,
frequency to run your backups.
So to access Theron tab system,
you're gonna use a command called kron tab and you can pass at the dash l
flag, right,
which will go ahead and give us a listing of all Theron tabs we have
for our user in this KCC two. And you can see there isn't any.
So if we want to add one, you can use the dash eag,
which will go ahead and let us edit Aron tab. Now I'm gonna,
I'm gonna go ahead and copy this long path to this file
because we're gonna use it in our chron tab.
So I'm gonna hit chron tab dash e enter,
and you can see it drops us into a vem like editor. Um, actually it is vem, but,
and it's gonna create us.
So the first thing we're gonna do is we're gonna hit I to go in insert mode.
You're gonna hit at the at symbol and we're gonna set daily,
which this is basically a shortcut. There's daily,
hourly, monthly, yearly, that sort of thing.
You can do way more finer grain control for kron tabs on like run
on this minute, only on the third month of the day.
You can get as granular as possible. I put in the repo,
there's Aron tab format file if you wanna see how to do it. More granularity.
But for our purposes, this is perfectly fine using the shortcut.
Now I'm gonna paste,
I am gonna paste the file in.
Now we know that this is actually in the home e C
two user directory and that is the companion repository
from the course. And so here we're saying every day,
whenever that daily kron fires off, go ahead and run this command.
We're not gonna pass any arguments to it. Um, but we will show you,
I'll show you what happens when you do pass our arguments to it and uh,
what failure looks like. But for right now,
let's go ahead and we're gonna get outta here. We're gonna hit shift colon x,
and as you can see it installed our new chron tab.
And then now I'm gonna do a dash L again.
So we can list it and you can see we have our kron tab. So
to test our script out one more time,
I wanna show you passing an argument to it.
So if I say like fubar for the directory,
that directory does not exist,
I expect this script to fail and bomb out.
So let's go ahead and hit enter.
And you could see right now it says the fubar directory does not exist,
backup halted.
So that is the protection and sanity check that we want in case anything
happens. And we can go ahead and print out the backup log at this going.
It should not have changed because we don't write to the log this early on.
But just to show you that there's nothing in the backup log for errors either,
because if we look at the script here,
let me go ahead argument, we look at the script,
you remember on line 17,
we're bailing out really early because there's no point in going through the
rest of the problems or the rest of the solution here to back
everything up if we,
there's no directory or doesn't exist cuz tar will just fail at that point
Anyway. Okay, let me go ahead and close this.
Okay, so we've come to the end of our backup script.
So you've created your first real script that actually has substance to it.
So let's head back over and do a recap.
Let's talk about what we did in this video.
You learned about creating a bash script that can backup directories.
You were introduced to the tar command and how to pass arguments to your bash
script. Finally, you tested the backup script and you ran it from a crime job.
In the next video,
you will create a script that displays important information about your e c two
host. See you soon.