|
Let's do something useful : Create an automated daily backup procedure for
your files
Let's assume that your /root directory is full of those very-important-and-regularly-changing
files.
Naturally you are worried that you may lose these files one day, so you
want to back them up every day...
I assume that you know nothing about backing up files in GNU-Linux so I
will show you how to do it step-by-step.
Please note that I am a newbie as well; these pages are part of my learning
process, and mistakes may exist...
STEP
1: ARCHIVING FILES
Here
is the first command that you have to learn: "tar"
. It simply takes multiple files and sticks them together in a single file.
So the following example would take all my files from my /root directory
and archive them in my /mnt/win_d/backup/ directory under the name new.tar:
[root@localhost root]# cd /mnt/win_d/backup ( - go
to the backup directory)
[root@localhost backup]# tar cvf new.tar /root ( - create
the archive)
Why did I use the cvf bit?
Well "c" creates an archive, "v" lists all the files that are going in
the archive (it keeps you occupied while the archive is created) and "f"
tells tar to use the following name as the the filename for the newly created
file, in our case "new.tar". Then we have our source directory "/root".
For those who are completely new to linux the c,v,f are called switches
of the tar command.
What about compressing the file as well? When I run the above command in
my machine, it creates a 19MB file.
Compression is easy - just add the z (z for zipping) switch in the line
above:
[root@localhost backup]# tar cvzf new.tar /root
( - create the archive and zip it as well!)
Having just run the command, my new.tar file is now 8.8MB, which shows
two things:
1.
The previous file was overwritten
2. Compression works pretty well (it always depends though on the types
of files you try to compress)
So we have
mastered the art of archiving and compressing files.
STEP 2: WHAT'S THE DATE TODAY?
Every day we want of cource to create a new file, and the easiest way to
name the file is the date. If you type the command "date
" you should get something like this:
[root@localhost backup]# date
Web May 1 07:07:27
BST 2002 (it's 7o'clock already?)
but if you use the switch "-I" you get just the date, in a numerical format
[root@localhost backup]# date -I
2002-05-01
Before we go ahead, you have to get familiar with the concept
of using the output of one command as part of a second command.The following
command:
[root@localhost backup]# tar cvzf 2002-05-01.tar /root
will create the file 2002-05-01.tar that I want. BUT if I use the `date
-I` instead of entering the date manually, ie
[root@localhost backup]# tar cvzf `date -I`.tar /root
I get the file created with the a (date).tar as a filename.
So if I execute the same command every day, my previous backups will not
get overwritten as every my new backup will have a different name.
NOTE: the ( ` ) character that I used is located over the tab button in
the UK and US keyboard and it is different from the ( ' ) and the (
" ), ok? If you try the above command and it doesn't give you the expected
results you are not using the right character, so be aware!
STEP 3: CREATING THE SCRIPT
That's the easiest part. You can use the editor of your choice, and simply
type the commands
cd /mnt/win_d/backup
tar czf `date -I`.tar /root
I removed the v switch, so that you don't get any output when you run the
script.
Then save it as mybackup in the /mnt/win_d/backup directory.
So far you have created a file. To make it executable you have to type
the following command
[root@localhost backup]# chmod 755 mybackup
To run your newly created command simply type
[root@localhost backup]# ./mybackup
(that's "dot slash mybackup" and works only if the command is in your current
directory, got it?)
or
[root@localhost
wherever_u_are]# /mnt/win_d/backup/mybackup (give the full path to be
safe!)
So now everytime that you run the mybackup command, a backup is done.
(That was easy, wasn't it?)
STEP 4: AUTOMATING THE BACKUPS
To automate the process, we have to use the crontab command - this
command enables users to run a command periodically.
The default editor for crontab is vi, which is a powerful but not user-friendly
editor. Another powerful abd friendlier editor is emacs. Assuming that you
are new to linux, you really want to use emacs instead of vi (trust me on
this!).
by simply running the command:
[root@localhost backup]# export EDITOR=emacs (you
tell linux to use emacs as your default editor.)
Now the crontab part.
I will assume that your PC is not on 24 hours a day, but when it's on it
will stay on for at least an hour. In this case I can safely say that if I
run the "mybackup" script every hour I will be safe from a disaster. What
will happen is that if I use my computer for more than an hour, the .tar file
will be overwritten every hour with a new one.
When the date changes, the latest backup of the previous day will remain
on my hard drive.
Scheduling a job is very easy. Run this command first (make sure you are
logged in as root):
[root@localhost backup]# crontab -e (use the -e switch
to edit the existing file, if there is one)
You should now have your emacs editor open, and probably some lines are
already filed. Just start a new line at the bottom of the list and type the
following line:
15 * * * * /mnt/win_d/backup/mybackup
Then save and close.
This line tells Linux to run the "mybackup" script at 15 minutes past the
hour, every hour, every day and every month.
You have probably noticed that there are five fields. These fields, from
left to right are:
- MINUTE:
range 0-59
- HOUR:
range 0-23
- DAY of
MONTH: range 1-31
- MONTH:
range 1-12
- DAY of
WEEK: range 0-7 (where both 0 and 7 represent Sunday - don't ask me why)
And... that's
about it really.
So I hope that from this mini tutorial you have learned how to
- compress
and zip files (tar command)
- use the
date command
- combine
the date and tar commands
- create
scripts and make them executable
- schedule
jobs using crontab
You can obviously
modify this job to back up multiple directories (ie the /home as well or anything
you consider important).
You can also add a line in the script to write a log file, so that you
know when the backup was performed.
You can possibly write a second script that runs once a week and creates
an extra backup in a different directory and deletes the previous daily
backups - in order to save space.
You can even upload the file to an FTP site for extra security (make sure
your file is also encrupted). This is not recommended if it is done on an
hourly basis and it's definitely a NO-NO for modem users.
Written By D Mitsinikos - Date: 1 May 2002 - Version:
0 .01
|