Kids now and just listen we are going to write a script that will back up our file system.
Very important script.
If you do not have a backup product or backup application or backup solution in your environment.
Most of the time smaller companies do not have the back up third party application that does the backup
for them.
So what do they have to do is they have to rely on this operating system tools that are available to
do the backup and run those backup manually by creating a little script.
If you own a company you have a backup.
Perfect.
You probably don't have to worry about it but I would still say that sometimes you do have to tar a
file to do the backup if you are making certain changes for certain times anyway.
Let's go into the lesson and this lesson we are going to back up our slash at sea and waar directories
using the tar command.
Ok why am I picking slash at sea and why directories.
Two reasons one.
These are the most important directories in a Linux system environment which is less ETSI that has all
the configuration files and that has all your apache and logging information because you need to go
back and troubleshoot.
Same thing if you need to go back if something has changed in your directory configuration has Jane
and your application has stopped working.
You could go back and restore it from your backup at c directory.
That's one reason.
Second reason is I'm just picking these up.
You could actually pick up any directory that you want for your test purposes.
So I am picking these up as just for my lesson to go away to make you understand.
So we'll create the backup of these two directories.
What is using the command tar.
I'm sure you familiar with that.
Then what are you going to do is we're going to compress the backup file which is the Ta-Ta tar file
now using the Jesus command.
So compress is actually going to reduce the size of the tar file.
Then what are you going to do of course then you're going to write a script to automate the entire process.
And we're going to put some logic some intelligence inside of this group that will look for the backup
file first before is actually moved somewhere send it to someone or send an e-mail that the backup is
complete or anything.
So we put the logic in there.
So let's go into a Linux machine.
I have logged in again as myself and which directory and I'm in my home directory now since we are going
to backup slash at c and our directory these are the directories that are owned by root.
So me being myself I don't have permissions to do the backups or touch even touch those records would
have to do is I have to become root
perfect.
Who am I to confirm that I am root.
Now I'm very powerful I could do anything I want.
So the command first of all be always before you write a script.
Always run your command to make sure that command works before you actually put that command inside
of your script.
So I'm going to do the same thing I'm going to do TAR CVF.
It's just the option for the tar to actually put everything off at sea or tar and then you have to specify
the name of the destination file which has backup backup.
Tar and what are we backing up.
Backing up at sea and slash war.
So that's simple command to tar both of these directories and put them into backup dock tar file which
will go into slashed disctrict that's run it as you can see it's coming up with all those files and
it looks perfect at actually taking every file and at sea and every file or directory in war and tarring
it together.
Tar ball and Ill put in to the file that we have specified which is back up to tar.
OK so it should be done pretty soon my system is small it's not huge.
But if your system that you are working on is pretty big and has gigs of gigs of sig file system partition
at C and R then it's probably going to take a little longer.
So anyway we'll give it a few seconds.
Oh there you go see it's done.
As a matter of conversation that I was having with you and it's time now let's verify it if it actually
has created the file in which we defined the location
temp.
Right.
And the file was back up to.
I hit tab and it automatically completed because it knows there's a file there.
Hit enter.
And now you'll see the date the time the file was created and it is there perfect.
It has everything.
Now if you want to view what's inside the file you can view that by untiring it or you could use Option
tar TV which will not on TAR.
Ill just show you what's inside of it.
Anyway our focus is not the tar or compress command.
Our focus is to write a script that will run those commands right.
So now now we know that it works.
Next thing we have to do is we have to compress it.
So Jean Zipp
back up you'll see the size of my slash at CS and slash war.
Is this much.
And when I do the zipper it will reduce the size.
Most likely it should bring it down to about half of its size and once it's completed it will actually
change the extension from backup tar to backup.
Target G zip.
And of course it will have a lot smaller size.
All right hopefully should be done soon.
If you are doing with me while I am teaching you though be great exercise to actually tar your balls
and to compress your files by the way if it is taking a really long time you could actually open up
another terminal.
I'll show you.
This is a good exercise as well.
Hello again decem log in as root because you're running as root
can now go to a temp directory and do ls minus 0 TR on back up Esterbrook and you will see both files
one is backup tar and the other one is backup target GZ.
Now since Ve command is still running it means this size is going to change.
If I run that command again you see it's keep reducing keep shrinking the file OK.
Anyway if you look at the back window you'll see the command is finished successfully and of the Balta
far Kamaz that you want to include in our script ran successfully and even if the script will run it
we know it's not going to error out or is now going to complain.
Now the time to actually create
our backup scripts so we'll call it back up
file system you can pick any name you want of course to enter.
Now you have to specify the shell bin bash again.
Who is the author on the other.
In your case you are the author.
OK.
Date 0 8 0 8 to 20 Why am I picking 0 8 0 8 all the time.
Is this something someone spoke today or is it something important.
No it says something I'm picking is stuck in my mind so I keep on picking on them.
Anyway description is backup slash at c and slash waar file system.
That description is important modified.
It's the first time so will the same date.
Right now we have two put the same command that we create.
Iran which is tar CBF temp backup tar slash at c slash war.
Then what are you going to do.
We are going to compress it.
Zip back up tar.
Right now you're probably thinking Hey Imraan.
Maybe when it runs the script brontë's all the time Iran's it does the back of tar file and the file
already exists is going to overwrite the all the file right.
Yes you're right it will overwrite if you want to have a different name every time it created you could
do that by specifying dot the date variable.
Again I'm not going to go into it it's going to get very longer but you you could definitely do that.
Now get into our backups.
Now the first thing I have to do is find back up.
TAR GC before it it's actually going to run the script.
I have to actually sorry when it runs this group created.
I have to do something that I have to notify.
This is demonstrator or I have to actually copy that backup and put it somewhere.
So in case if this system were disturbed I'm working on if it actually crashes then I could actually
recover from another server.
It's always a good idea to crash for them.
So this is the time I'm actually doing that third step.
So before I do the third step I have to verify it.
If that X Files that I'm going to trash for exist so and that would be saying that fine back up towards
the end time and if it exists minus one meaning if it if it was created today not yesterday I don't
want to send the yesterdays back up Darkfall
and then am per cent
Dev No.
Basically I'm saying if it does exist.
Meantime this which is a type is the file simple file then print.
I don't have to put that print.
It's going to put it on the screen.
But you know it's just a comment that I have I'm going to leave it and I'm saying ampersand grid assign
meaning whatever the output comes up from here don't throw it on the screen just put it on Dev-Null.
No it's just a way to flush out the output but it doesn't mean that we're flushing out the actual output.
You're not going to use it.
We actually will use it but it's not going to throw that on the screen.
So now we're going to do is if
dollar question mark is equal to zero.
Now what is dollar question mark.
It's existe data.
So if the command that ran earlier than that because this.
So what we're saying is if we find a backup to a GC which was created today and that's we would define
and their access statuses which is there.
Ben put it right here then.
What do you do.
Echo back up was created perfect write code just put one more line in Echo another and now say archiving
backup because archiving meaning you are sending it to another location and the another location would
be you have to run the command like FCP or SEP or whatever the command that you're wanting to run Sep
sashed temp back up tar which is the file to where to route anywhere your server that you want to send
it to 1 to 168 out 1 to 1 or 2 or X whatever.
And then the Piatt path where it's going to go to and then do else if it can not find it now else's
If equal if the output is not equal to 1 then what do you do else at go back up failed.
Simple as that.
And then just two feet.
So this is the script to actually back up the file system and actually transferring def file the black
about 5 to another location.
Now since we are testing it and this is like a training lab I just want to exclude this or put hash
here because what hash is going to do is going to comment out this entire line.
So now when I mean I'm going to run this script what it's going to do is if this file backup file exists
then it's going to say backup was created and archiving the backup.
And if it does not exist then it's going to say Ecko backup failed.
Simple is that I'm going to modify permission executable to back a fault system.
Perfect.
Now let's run it.
All right.
Moment of Truth.
You know what it's doing.
It is backing up your false system.
Them you see just what happened here.
It came back and says back up dot tar found no such file or direct.
Why is that.
Because it's See what our script is doing.
Our script is for storing and putting it away here and then.
See right here.
I did not specify the slash back up so I'll do more.
Back up fs I had to let it know where a file is located and it follows temp.
So that's where you have to do it.
Here you have to specify the absolute path again because if you're running from somewhere else it will
complain.
Now it's chevron.
But now I don't want to run it.
I want it to calm that out because we already have the backup.
Cheesy that's.
Temp backup.
That's easy.
Yes we have it.
And now let's run our script and it says backup was created are coming back.
Perfect.
Now what if I delete that
yes.
Now I run the script back failed.
They go bingo anyway.
So that's how you need to create a backup if you are in a new environment a corporate environment.
You can back up the entire file system or Pacific file system that you want to do and whatever that
you want to do with it you want to send e-mails or indication or you want to send it to a different
host.
You could do that.