On my system I have two hard drives in my server that will be known as drive1 and drive2. On my drive2 are two partitions: 1) /home 2) /ptmp. Drive1 contains all other partitions.
Notice: All of the following takes place on the server. I telnet into the server from my workstation for convenience. Before you telnet into your server you MUST recognize that telnet sends EVERYTHING in clear text, including passwords. You must make sure your network is secure before using telnet, or someone listening with relatively simple tools can get full access to your root account on the server. My suggestion is to use SSH instead of telnet or else physically walk to your server to accomplish the backups.
1) Access to the root account
2) The bash shell (type "echo $SHELL" at a command line. If it says "/bin/bash" you should
be OK)
The absolute minimum backing up of files that I want to accomplish is to backup everything in the /home directory, including all its sub-directories, and store them on drive1 in the /tmp directory. In addition, I must get a specific file called "WATCH.TXT" that the programmer demanded be put in the directory with the program. It has my billable clock hours in it. My /home directory contains a directory for each user and a couple of special directories for shared data on the network. The /tmp directory is just that; TEMPORARY. Each day all files in the /tmp directory that are older than x number of days are deleted. Be sure you copy your backed up data file to another media for removal from the site and as an additional safety precaution.
To accomplish my needs I stored the following four lines in a file called "backup" in /home/david/bat. Do not use a Windoze editor as some of the commands do strange things with the extra character Windoze sticks in at the end of each line.
mkdir /tmp/`date +"%b%d_%Y"`
cd /home
tar cvzf /tmp/`date +"%b%d_%Y"`/home.tgz .
find /ptmp/bus_tmp/stop -name "WATCH.TXT" | xargs tar -cvzf /tmp/`date +"%b%d_%Y"`/other.tgz
Open a command window and su to root. This script must be run as root to get the appropriate permissions to back up all sub-directories under /home. The script is run simply by switching to the bat directory and typing "sh backup" and pressing the <Enter> key.
The tar command looks very daunting until you realize that 99% of the functionality you need is in a few command switches. The ones listed above are the ones I use.
"sh backup" This tells the shell that the lines in the following file name are a set of commands to
be run sequentially until the end of the file in the command shell.
"cd /home" Change the active directory to /home
"tar" Start the tar program with the following special instructions. The order of the special instructions
is important.
"-c" Create a file
"v" Verbose output (Tells you everything it's doing)
"z" gzip the file (Do file compression so the resulting file takes less space)
"f" Filename to be created is supplied
"/tmp/`date +"%b%d_%Y"`/home.tgz" The named file will be placed in the /tmp directory in the
correct subdirectory by current system date. Now to determine how the file is named. The character sequence "`date
+"%b%d_%Y"`" contains two back quotes. The bash shell interprets this as "Whatever is between
the back quotes is executed as a command and the returned value is substituted for the back quoted text".
This is very powerful as we can now change the program name on-the-fly. In my case I wanted the current date as
part of the filename. Therefore date command is executed with some nice options(flags).
The "+" symbol says that options follow. The options are enclosed in quotes
The "%b" is the abbreviated month name.
The "%d" is the 2 digit date (01..31). I tried the %e option instead and the program failed on the 1st
through the 9th of the month. The %e is day of month, blank padded
The "_" is a separator I use between day and year and is part of the filename
The "%Y" is the 4 digit year
Then the string ".tgz is appended to the date for a file named something like this "/tmp/Oct02_2001/home.tgz"
The files to be "tar"ed are designated by either filename, without wildcards, or directory name. If a
directory name is specified all fills in all directories below the specified one are "tar"ed. In this
case the final "." is a place holder that says all files in this directory, the /home directory, and
all directories below it are to be backed up.
The final line "find /ptmp/bus_tmp/stop -name "WATCH.TXT" | xargs tar -cvzf /tmp/`date +"%b%d_%Y"`/other.tgz" is MUCH more complex than it needs to be just so it can serve as a reminder of the strength of the command line. "find" locates the file "WATCH.TXT" and pipes (the | symbol) it to tar via xargs. "find" can locate groups of files all over your computer and pass them to tar with their pathnames. Please see Find is your Friend (Rich Griswold) from a previous meeting for a more in-depth treatment of find and xargs. In tar if you gzip the file as I do you are not able to append additional files to it once it closes. That's why I have the script create an additional file.
If you run the "sh backup" command more than once in a calendar day, the old file(s) are deleted and replaced with the new file(s) without warning.