WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.590 --> 00:00:10.970
In this video, you will create a script that backs up the engineering directory.

1
00:00:12.430 --> 00:00:15.690
Our plan is to create a bash script that performs a backup of the engineering

2
00:00:15.690 --> 00:00:16.890
directory in all its files.

3
00:00:17.340 --> 00:00:20.770
We'll learn how to use shell commands like tar and how to pass arguments into

4
00:00:20.770 --> 00:00:22.440
our script. Finally,

5
00:00:22.620 --> 00:00:26.640
you'll test the script and add it to a crime job so it can run on a schedule.

6
00:00:27.210 --> 00:00:31.450
Let's get started. Okay, so here we are in the terminal,

7
00:00:31.750 --> 00:00:36.050
and for this video we're gonna create a backup script that will backup any

8
00:00:36.050 --> 00:00:36.710
directory,

9
00:00:36.710 --> 00:00:40.450
but we're gonna default it to use the engineering directory created earlier in

10
00:00:40.450 --> 00:00:42.530
the videos. There's a few files in there,

11
00:00:42.550 --> 00:00:45.130
so it'd be enough to go ahead and get it a good test.

12
00:00:46.680 --> 00:00:48.220
If you recall from earlier,

13
00:00:48.390 --> 00:00:53.020
we've cloned and we've pulled down all the files that are used for this video.

14
00:00:53.440 --> 00:00:56.700
Um, and so those are gonna be in the section five sub subfolder.

15
00:00:59.140 --> 00:00:59.490
Okay,

16
00:00:59.490 --> 00:01:04.270
so let's open the backup script and let's take a look and see how it works.

17
00:01:06.160 --> 00:01:07.700
So in my vim editor, again,

18
00:01:07.960 --> 00:01:12.660
I'm going to go into the hands-on Lennox for cloud DevOps, cloud engineers, uh,

19
00:01:12.660 --> 00:01:16.660
directory section five and

20
00:01:16.880 --> 00:01:20.240
backup.sh. Okay,

21
00:01:20.380 --> 00:01:23.480
so let's go to the top of this file here. We, as we always have,

22
00:01:23.500 --> 00:01:28.470
we have our shebang on line one and line four we set

23
00:01:28.510 --> 00:01:31.350
a variable for the backup directory, the directory we wanna do.

24
00:01:31.650 --> 00:01:34.910
And then here on line five, this, uh, just like we looked at earlier,

25
00:01:35.010 --> 00:01:38.950
the dash f to see if a file exists, a kind of a, a built-in dash test,

26
00:01:39.410 --> 00:01:43.670
you can do one here, which is the dash z. And it basically says,

27
00:01:43.850 --> 00:01:48.230
is this, is the argument being passed in empty? Is it being set?

28
00:01:49.140 --> 00:01:51.040
So basically if it isn't set,

29
00:01:51.040 --> 00:01:54.080
then we want to just go ahead and default to the user local engineering

30
00:01:54.080 --> 00:01:57.260
directory that we've already created. Otherwise,

31
00:01:57.960 --> 00:02:02.820
we want to go ahead and set the backup directory to whatever the is passed

32
00:02:03.060 --> 00:02:05.060
in as the first argument to the script.

33
00:02:05.120 --> 00:02:08.660
And that's where this dollar sign one comes in. This basically says,

34
00:02:08.660 --> 00:02:09.980
just like you saw on the functions,

35
00:02:10.480 --> 00:02:13.660
is the first parameter or argument being passed in.

36
00:02:14.120 --> 00:02:18.580
So we set that on line 10 here. Let's come down to line 14.

37
00:02:19.120 --> 00:02:21.820
So this is us doing, um, a sanity check.

38
00:02:22.200 --> 00:02:24.980
So regardless of whatever the backup directory is,

39
00:02:24.980 --> 00:02:28.140
it's the default for user local engineering or something we pass in.

40
00:02:28.760 --> 00:02:32.460
If that backup directory does not exist, we wanna stop,

41
00:02:32.560 --> 00:02:35.500
we want to exit right there because there's nothing to back up and we need to

42
00:02:35.500 --> 00:02:40.440
fail. And so here on line 17, we do an exit one. Okay,

43
00:02:40.440 --> 00:02:42.960
let's go down to line 21. Here.

44
00:02:43.020 --> 00:02:47.980
We create the name of our archive and so we want this to be the name

45
00:02:48.080 --> 00:02:50.940
of what we're backing up. And then also you can see here,

46
00:02:51.050 --> 00:02:55.880
over here where we do this command interpolation in here with the doll

47
00:02:55.880 --> 00:02:56.880
sign open peren,

48
00:02:57.300 --> 00:03:01.560
we want to run this date command and in bash and in a Lennox,

49
00:03:02.490 --> 00:03:06.510
the date command will print out the current date and then all these plus

50
00:03:06.620 --> 00:03:07.390
percent,

51
00:03:07.390 --> 00:03:12.220
Y minus percent m et cetera are just

52
00:03:12.220 --> 00:03:16.660
ways to print out a format. And in this case it is year, month,

53
00:03:17.530 --> 00:03:21.400
date with an underscore. And then hour,

54
00:03:21.400 --> 00:03:26.280
minute and second and then we end our file in the dot Tar GZ

55
00:03:26.790 --> 00:03:27.460
extension.

56
00:03:27.460 --> 00:03:32.440
And that is because we are going to create a tar file and it is going to be

57
00:03:32.440 --> 00:03:35.640
zipped up in case it obviously is large. We want it to be as small as possible.

58
00:03:36.920 --> 00:03:37.230
Okay?

59
00:03:37.230 --> 00:03:40.970
And so it's probably a good stopping point to talk about with the tar command.

60
00:03:41.390 --> 00:03:44.170
So we're gonna be using the tar command to do our backup.

61
00:03:44.230 --> 00:03:48.010
And tar command is an archive utility. And so if you're unfamiliar with it,

62
00:03:48.270 --> 00:03:52.330
it basically will take files and directories and create an archive,

63
00:03:52.330 --> 00:03:57.170
which is basically a single file with all of the directories or files inside of

64
00:03:57.170 --> 00:04:01.950
it. And so then you could pass around just the one file tar also can work with,

65
00:04:02.090 --> 00:04:06.380
uh, zip or in our case gun zipp, uh, or G zipp.

66
00:04:06.800 --> 00:04:09.700
And what this will do with this will compress the archive to make it smaller.

67
00:04:10.280 --> 00:04:12.180
And it's always a good practice to do that.

68
00:04:12.180 --> 00:04:15.340
It doesn't take up a lot of resources. So, uh, we'll do that here.

69
00:04:17.100 --> 00:04:21.910
Okay, line 24 here. We created a function. So as I said before, we're,

70
00:04:21.910 --> 00:04:25.990
we're trying to use all the paradigms we've learned up to this point and to make

71
00:04:25.990 --> 00:04:28.430
our lives a little bit easier and the code just a little bit more readable.

72
00:04:28.650 --> 00:04:32.430
And so here we're going to get a count of the files and the directory to backed

73
00:04:32.430 --> 00:04:36.820
up. And we name this function files to be backed up.

74
00:04:37.890 --> 00:04:42.590
And we use the built in find executable to search our

75
00:04:42.590 --> 00:04:46.510
backup directory rate here, looking for file types only.

76
00:04:46.510 --> 00:04:49.750
This type type set a file. And then we're using the shell pipe,

77
00:04:49.750 --> 00:04:54.070
meaning whatever this re result returns, whatever the fine command returns,

78
00:04:54.090 --> 00:04:58.310
as a result, we're gonna pass it to the WC command, which is word count.

79
00:04:58.810 --> 00:05:01.750
And we just do a list command with the dash out, which just means list.

80
00:05:02.130 --> 00:05:05.390
And so this will just give us a basic number of the files that we're gonna back

81
00:05:05.390 --> 00:05:06.870
up, 1, 2, 3, et cetera.

82
00:05:08.960 --> 00:05:10.700
So here we get down onto line 30,

83
00:05:11.160 --> 00:05:15.780
and this is where we print out how many files that we're gonna be backed up.

84
00:05:16.080 --> 00:05:19.860
And as you can see here inside this double quoted string for echo,

85
00:05:19.860 --> 00:05:22.340
we're using the dollar sign open parentheses,

86
00:05:22.490 --> 00:05:25.460
meaning that we want to execute this function,

87
00:05:25.690 --> 00:05:27.740
that this is like an execution of it.

88
00:05:27.880 --> 00:05:32.140
And whatever the results are of that will then be expanded inside this string.

89
00:05:32.480 --> 00:05:37.100
And you'll see that when we run it. Okay, well line 32 here.

90
00:05:37.210 --> 00:05:41.710
This is where all the magic happens. So we're using what,

91
00:05:41.930 --> 00:05:46.570
so the tar command and then we're using the C Z F uh, flags.

92
00:05:47.170 --> 00:05:51.970
C means to create Z means to zip it up. And f is just for the file archive

93
00:05:53.510 --> 00:05:58.360
here, we're taking the backup file name that we created on line 21.

94
00:05:58.740 --> 00:06:02.640
So it'll be, it's gonna be under the slash temp directory.

95
00:06:02.910 --> 00:06:05.800
It's gonna be called engineering underscore, uh,

96
00:06:05.800 --> 00:06:08.800
date timestamp dot tar dot gz.

97
00:06:09.700 --> 00:06:12.120
And the backup directory, which is whatever we set,

98
00:06:12.120 --> 00:06:16.200
which is whether the default that got past, uh, the default that we set,

99
00:06:16.200 --> 00:06:19.160
which is usual local engineering or whichever one we passed in.

100
00:06:19.160 --> 00:06:23.870
So we have some versatility there. And then this whole line right here is just,

101
00:06:23.970 --> 00:06:24.950
is a little bit funny looking,

102
00:06:25.410 --> 00:06:29.110
but what this basically means is we want to take any standard error,

103
00:06:29.630 --> 00:06:31.790
anything that, any error that arrives from this command,

104
00:06:32.050 --> 00:06:36.990
we want to pass it to a file in the slash temp directory

105
00:06:36.990 --> 00:06:37.910
called backup log.

106
00:06:38.180 --> 00:06:41.070
This is just so later if there's any errors or if it doesn't work,

107
00:06:41.250 --> 00:06:43.550
we can go back and see what errors, um,

108
00:06:43.550 --> 00:06:47.230
have occurred during the archive creation. Okay,

109
00:06:47.650 --> 00:06:51.590
so line 34 to 40, this is our another sanity check in.

110
00:06:51.610 --> 00:06:54.550
You've seen this dash F before in the last V uh,

111
00:06:54.550 --> 00:06:55.790
dash F before in the last video,

112
00:06:57.020 --> 00:07:01.070
this we're just checking was the backup actually created. If it was,

113
00:07:01.070 --> 00:07:04.830
then we're just gonna print out succeeded if otherwise printout failed.

114
00:07:05.250 --> 00:07:09.590
And so that's just more of a, a, a indication to us of the state of it. Okay,

115
00:07:10.010 --> 00:07:13.240
so I'm gonna go ahead and we're gonna close this shift,

116
00:07:13.440 --> 00:07:15.840
colon X again and Vim to, to write and quit.

117
00:07:17.910 --> 00:07:22.450
And what we're gonna do is this file is already

118
00:07:22.580 --> 00:07:23.413
executable,

119
00:07:23.430 --> 00:07:27.730
so I can just remove the vim from the last line and I should be able to just run

120
00:07:27.730 --> 00:07:31.890
it right here. So let's go ahead and give it a try in our terminal. Okay.

121
00:07:32.150 --> 00:07:34.210
So as you can see, it was really fast.

122
00:07:34.450 --> 00:07:36.970
There was only three files and you could see that we,

123
00:07:37.070 --> 00:07:39.890
it says backing up three files and the backup succeeded.

124
00:07:40.070 --> 00:07:41.530
We did not pass any arguments,

125
00:07:41.670 --> 00:07:44.010
so it went ahead and backed up the engineering directory.

126
00:07:45.350 --> 00:07:49.040
Let's go ahead and look at the temp directory now. Okay,

127
00:07:49.040 --> 00:07:52.920
so what we're gonna do is we're gonna look at the temp directory and you're

128
00:07:52.920 --> 00:07:57.800
gonna see that there is a archive already here and

129
00:07:57.980 --> 00:08:02.160
it is called temp engineering. And then the date,

130
00:08:02.650 --> 00:08:04.920
month, year, and then our timestamp.

131
00:08:06.180 --> 00:08:09.070
And so if we use the tar command again,

132
00:08:09.410 --> 00:08:14.040
we can use the dash tf and what that'll do is it'll show us the

133
00:08:14.160 --> 00:08:17.880
contents inside the tar file. So we can see that what we have there.

134
00:08:18.100 --> 00:08:22.220
And as you can see it copied user, local engineering,

135
00:08:22.440 --> 00:08:25.380
it copied a contributed file, the config file and the cure file.

136
00:08:25.680 --> 00:08:27.220
So that gives us our three files.

137
00:08:28.010 --> 00:08:32.710
There is also if we want to cat out in the slash step is that backup is the

138
00:08:32.910 --> 00:08:36.350
backup log file. And so you do see an error here,

139
00:08:36.370 --> 00:08:39.710
but this is actually not really an error. It is printed standard out.

140
00:08:39.970 --> 00:08:41.950
It just says Tara saying, Hey,

141
00:08:42.450 --> 00:08:47.310
I'm removing the leading slash from this directory because of relative paths.

142
00:08:47.610 --> 00:08:50.150
You can safely ignore this. This is okay, so don't be alarmed.

143
00:08:50.530 --> 00:08:54.680
So now we have a working backup file that writes the backups.

144
00:08:55.090 --> 00:08:57.120
Let's go ahead and now we wanna automate this.

145
00:08:57.120 --> 00:08:59.960
Obviously we don't wanna have to do this by hand every time that we want on a

146
00:08:59.960 --> 00:09:03.880
backup. We want to use the built-in, uh, scheduler called kron.

147
00:09:05.580 --> 00:09:10.360
So Kron runs on your Lennox host and it is a Damon that runs

148
00:09:10.380 --> 00:09:15.360
and basically looks for commands to run for you on a specific cadence or or

149
00:09:15.360 --> 00:09:18.280
time interval. And for our case, we're gonna want to run this,

150
00:09:18.290 --> 00:09:21.120
let's just say daily. Um, that is a safe, uh,

151
00:09:21.670 --> 00:09:23.320
frequency to run your backups.

152
00:09:24.020 --> 00:09:26.850
So to access Theron tab system,

153
00:09:27.110 --> 00:09:32.050
you're gonna use a command called kron tab and you can pass at the dash l

154
00:09:32.080 --> 00:09:32.770
flag, right,

155
00:09:32.770 --> 00:09:37.610
which will go ahead and give us a listing of all Theron tabs we have

156
00:09:37.630 --> 00:09:42.030
for our user in this KCC two. And you can see there isn't any.

157
00:09:42.970 --> 00:09:45.950
So if we want to add one, you can use the dash eag,

158
00:09:45.950 --> 00:09:49.190
which will go ahead and let us edit Aron tab. Now I'm gonna,

159
00:09:50.090 --> 00:09:54.810
I'm gonna go ahead and copy this long path to this file

160
00:09:56.030 --> 00:09:58.120
because we're gonna use it in our chron tab.

161
00:09:58.300 --> 00:10:00.480
So I'm gonna hit chron tab dash e enter,

162
00:10:00.940 --> 00:10:05.520
and you can see it drops us into a vem like editor. Um, actually it is vem, but,

163
00:10:05.660 --> 00:10:06.520
and it's gonna create us.

164
00:10:06.520 --> 00:10:09.320
So the first thing we're gonna do is we're gonna hit I to go in insert mode.

165
00:10:10.550 --> 00:10:14.130
You're gonna hit at the at symbol and we're gonna set daily,

166
00:10:14.220 --> 00:10:18.910
which this is basically a shortcut. There's daily,

167
00:10:19.290 --> 00:10:21.590
hourly, monthly, yearly, that sort of thing.

168
00:10:22.570 --> 00:10:27.190
You can do way more finer grain control for kron tabs on like run

169
00:10:27.730 --> 00:10:30.150
on this minute, only on the third month of the day.

170
00:10:30.170 --> 00:10:33.670
You can get as granular as possible. I put in the repo,

171
00:10:34.180 --> 00:10:37.790
there's Aron tab format file if you wanna see how to do it. More granularity.

172
00:10:37.850 --> 00:10:40.870
But for our purposes, this is perfectly fine using the shortcut.

173
00:10:41.450 --> 00:10:42.510
Now I'm gonna paste,

174
00:10:44.820 --> 00:10:46.740
I am gonna paste the file in.

175
00:10:46.760 --> 00:10:51.640
Now we know that this is actually in the home e C

176
00:10:51.660 --> 00:10:56.560
two user directory and that is the companion repository

177
00:10:57.070 --> 00:11:00.960
from the course. And so here we're saying every day,

178
00:11:01.240 --> 00:11:05.160
whenever that daily kron fires off, go ahead and run this command.

179
00:11:05.210 --> 00:11:10.040
We're not gonna pass any arguments to it. Um, but we will show you,

180
00:11:10.190 --> 00:11:13.920
I'll show you what happens when you do pass our arguments to it and uh,

181
00:11:13.920 --> 00:11:16.160
what failure looks like. But for right now,

182
00:11:16.160 --> 00:11:19.160
let's go ahead and we're gonna get outta here. We're gonna hit shift colon x,

183
00:11:20.460 --> 00:11:22.960
and as you can see it installed our new chron tab.

184
00:11:23.540 --> 00:11:25.640
And then now I'm gonna do a dash L again.

185
00:11:25.700 --> 00:11:29.280
So we can list it and you can see we have our kron tab. So

186
00:11:31.080 --> 00:11:33.260
to test our script out one more time,

187
00:11:33.860 --> 00:11:35.980
I wanna show you passing an argument to it.

188
00:11:36.080 --> 00:11:40.380
So if I say like fubar for the directory,

189
00:11:40.380 --> 00:11:41.820
that directory does not exist,

190
00:11:42.140 --> 00:11:45.700
I expect this script to fail and bomb out.

191
00:11:45.800 --> 00:11:47.620
So let's go ahead and hit enter.

192
00:11:48.410 --> 00:11:51.950
And you could see right now it says the fubar directory does not exist,

193
00:11:51.950 --> 00:11:52.870
backup halted.

194
00:11:53.640 --> 00:11:58.100
So that is the protection and sanity check that we want in case anything

195
00:11:58.100 --> 00:12:02.060
happens. And we can go ahead and print out the backup log at this going.

196
00:12:02.320 --> 00:12:05.540
It should not have changed because we don't write to the log this early on.

197
00:12:05.840 --> 00:12:08.940
But just to show you that there's nothing in the backup log for errors either,

198
00:12:09.600 --> 00:12:12.210
because if we look at the script here,

199
00:12:14.550 --> 00:12:17.980
let me go ahead argument, we look at the script,

200
00:12:18.400 --> 00:12:19.940
you remember on line 17,

201
00:12:20.430 --> 00:12:24.140
we're bailing out really early because there's no point in going through the

202
00:12:24.140 --> 00:12:28.940
rest of the problems or the rest of the solution here to back

203
00:12:28.940 --> 00:12:29.780
everything up if we,

204
00:12:29.780 --> 00:12:33.300
there's no directory or doesn't exist cuz tar will just fail at that point

205
00:12:33.360 --> 00:12:37.320
Anyway. Okay, let me go ahead and close this.

206
00:12:42.560 --> 00:12:45.770
Okay, so we've come to the end of our backup script.

207
00:12:45.870 --> 00:12:49.890
So you've created your first real script that actually has substance to it.

208
00:12:49.890 --> 00:12:51.610
So let's head back over and do a recap.

209
00:12:53.360 --> 00:12:54.750
Let's talk about what we did in this video.

210
00:12:55.010 --> 00:12:57.590
You learned about creating a bash script that can backup directories.

211
00:12:58.370 --> 00:13:01.950
You were introduced to the tar command and how to pass arguments to your bash

212
00:13:01.950 --> 00:13:06.240
script. Finally, you tested the backup script and you ran it from a crime job.

213
00:13:07.720 --> 00:13:08.553
In the next video,

214
00:13:08.800 --> 00:13:12.220
you will create a script that displays important information about your e c two

215
00:13:12.220 --> 00:13:13.500
host. See you soon.
