Pages

Tuesday, November 2, 2010

Bash Script for Backing Up Log Files

This bash script is used in OpenSUSE 11.2 for backing up and compressing system log files to reduce storage requirements. The script is intended to be run manually.

Open your favorite text editor (gedit, vi, awk, etc), copy the script over (with cut and paste), and save it. I used logtar for the filename. Place the file in the directory: /usr/local/bin/ and changed the permissions to be executable. Placing the file in this directory should place it in the default system path and allow the file to be executable from any directory in the system, and for any user.

The comments in the script explain how it works and what to modify for your personal system. The first line of the script "#!/bin/bash" is special and tells the system where the bash compiler is located. If you are not using and OpenSUSE system, the bash compiler may be in a different location. You can discover the path to your bash compiler by entering the following at the command prompt (without the '$'):
$ which bash

To change the permissions of the new file, enter the following at the prompt, from within the same directory as the logtar file (or whatever you chose to call it):
$ chmod 755 logtar

No warranty or guarantee is implied with the use of this script. It works great on my machine and the servers I maintain, but you will still use this at your own risk. I would suggest creating a new directory and copy some .log files over to it (keep your originals where they are) and practice with the script until you understand how it works and figure out what needs to be changed (if anything) for your own system.

Enjoy!


BEGIN CUT AND PASTE BELOW THIS LINE. DO NOT LEAVE ANY BLANK LINES AT THE TOP OF THE SCRIPT:

#!/bin/bash
#
#####################################################################
# Written by: Andrew Stout, July 2010
# No warranty or guarantee is implied with the use of this script. Use
# at your own risk.
#
# The logtar file is used to backup and compress log files. It will .tar
# and compress the designated file, then empty the original log and move
# the new file to the designated backup directory.
#
# This script will automatically add a current date and time stamp prefix
# to the backup filename, with the .tar.gz suffix.
#
# Command-line syntax: ./logtar foo foo.log
#
# Replace foo with the new file name. Do not include the automatic prefix
# and suffix. Replace foo.log with the full, original filename, including
# the path, if the file is not in the current directory.
######################################################################


# This is where the .tar'd and compressed file will be moved to.
DESTINATION="/home/logs"

# Changing this will change your date/ time stamp format. The current format
# is (Y)ear, (m)onth, (d)ay, (H)our, (M)inute, (S)econd. If this variable were
# not used, the tar and mv commands would reference different file names and
# the .tar'd file would be placed in the local directory, instead of the
# $DESTINATION directory.
DATE=$(date +"%Y%m%d%H%M%S")

# This will make the $DESTINATION directory if it does not already exist.
mkdir $DESTINATION

# This command compresses your .log file. The && means that the first command
# on the line will be executed, and (&&) the second command will only be
# executed if the first command is successful. The > empties the original file.
# $1 refers to the new name, or the second command-line element. $2 refers to
# the original name and path, or the third command-line element. $0 is the
# first command-line element, the logtar file being executed.
tar -czvf $DATE.$1.tar.gz $2 && > $2

# This moves the new file to the $DESTINATION directory.
mv $DATE.$1.tar.gz $DESTINATION

#######################################################################
## END OF SCRIPT

No comments:

Post a Comment