From Gruff Goat Wiki
#!/usr/local/bin/bash
# NAME
# ggis_dump.sh part of Gruff Goat's Script Collection
#
# OVERVIEW
# Include this script in your daily periodic to insure nightly backups.
# The script allows the user to set many options including: filesystems
# to dump, local and remote dumps, various paths, various dump file names.
# Dumped filename will be of the format 'prefacefilesystemyymmdd_level.gz'.
#
# REQUIREMENTS
# Bash
# Requires bash shell. This is standard in most linuxes. For FreeBSD install
# bash from the ports.
#
# SSH key
# In order to perform remote dumps or copies, this script uses SSH and SCP.
# Since this is done by script wihtout user interaction and it is not
# safe to include passwords in a script, you must use public key encryption.
# Instructions for this are beyond the scope of this document but you may look
# at http://www.gruffgoat.com/wiki/index.php/Goat_Security
#
# LICENSE INFORMATION
# Copyright (C) 2006 Gruff Goat Information Services (Gary Dalton).
# You may contact the author of this software at ggis@gruffgoat.com.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You may view the full license terms at
# http://www.gruffgoat.com/wiki/index.php/License_-_Gruff_Goats_Script_Collection
# Alternatively, you may receive a copy of the GNU General Public License
# by writing to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
##########
# USER SETTABLE VARIABLE
#
# What to dump
aFS="/ /var /home /usr" # Space seperated list of filesystems to dump
# It is not recommended to dump a filesystem into itself.
# Doing so will cause warnings.
# Files and locations
pathDUMP="/sbin/dump" # Path to dump
pathSCP="/usr/bin/scp" # Path to scp
pathSSH="/usr/bin/ssh" # Path to ssh
pathKEY="/root/keys/tiamat_id_rsa" # Path to SSH key, public key should be known by remote
pathTEMP="/tmp" # Temp files path
# Flags
flagDUMP="-uLan" # Dump flags
flagGZIP="-2" # Gzip flags
flagSSH="-c blowfish" # SSH flags
flagSCP="-c blowfish" # SCP flags
# Dump decisions
bDUMPLOCAL=1 # Dump files locally first (0|1)
# Cannot dump the filesystem where pathDUMPLOCALDIR resides
pathDUMPLOCAL="/home/back" # Local storage path, required if bDUMPLOCAL=1
bDUMPREMOTE=1 # Dump or copy files to remote (0|1)
stREMOTESERVER="ggis.homeunix.com" # Remote server name
stREMOTEUSER="tiamat" # User name on remote system
pathDUMPREMOTE="/home/dumps/tiamat" # Remote storage path, required if bDUMPREMOTE=1
bSAVELOCAL=0 # Keep dumps locally (0|1)
# Output file
stPREFACE="" # What would you like for a preface
stYEARLENGTH=2 # Year format 2 or 4 digit (2|4)
# User selectable dump order. Level 0 dump will be performed
# instead of the first level 1 dump in each month.
aDUMPLEVEL[7]=1 # Sunday
aDUMPLEVEL[1]=3 # Monday
aDUMPLEVEL[2]=2 # Tuesday
aDUMPLEVEL[3]=5 # Wednesday
aDUMPLEVEL[4]=4 # Thursday
aDUMPLEVEL[5]=7 # Friday
aDUMPLEVEL[6]=6 # Saturday
#
##########
##########
# USER BEWARE of editing below this point
#
# FUNCTIONS
function fsname()
{
local fs="${1:1}"
fs="${fs%%/*}"
fs="/$fs"
echo "$fs"
}
#
# VARIABLES
if [ "$stYEARLENGTH" -eq "2" ]
then
stYEAR=$(date +%y)
else
stYEAR=$(date +%Y)
fi
stMONTH=$(date +%m)
stDAYOFWEEK=$(date +%u)
stDAYOFMONTH=$(date +%d)
#
# Set stDumpLevel depending on day of week.
# If it is the first level 1 dump of the month,
# Change dump level to 0.
#
stDUMPLEVEL="${aDUMPLEVEL[$stDAYOFWEEK]}"
if [ "${aDUMPLEVEL[$stDAYOFWEEK]}" -eq "1" ]
then
if [ "$stDAYOFMONTH" -le "$stDAYOFWEEK" ]
then
stDUMPLEVEL=0
fi
fi
##########
# Prerequisite tests
#
if [ "$bDUMPLOCAL" -eq "1" ] # Dump locally
then
if [ -d $pathDUMPLOCAL ] # Does local dir exist
then
cd $pathDUMPLOCAL
else
echo "No such directory: $pathDUMPLOCAL"
exit 1
fi
fi
#
#####
if [ -d $pathTEMP ] # Does temp dir exist
then
cd $pathTEMP
else
echo "No such directory: $pathTEMP"
exit 1
fi
#
#####
if [ ! -e $pathDUMP ]
then
echo "Path to dump is incorrect: $pathDUMP"
exit 1
fi
#
#####
if [ "$bDUMPREMOTE" -eq "1" ]
then
if [ ! -e $pathSCP ]
then
echo "Path to scp is incorrect: $pathSCP"
exit 1
fi
if [ ! -e $pathSSH ]
then
echo "Path to ssh is incorrect: $pathSSH"
exit 1
fi
if [ ! -e $pathKey ]
then
echo "Path to your ssh key is incorrect: $pathKEY"
exit 1
fi
fi
#
# Loop through list of filesystems
# This is where the action is
#
for fs in $aFS
do
#echo $fs
#echo `fsname "$pathDUMPLOCAL"`
if [ -z "$(df |grep $fs)" ] # Check for existance of filesystem
then
echo "No such filesystem: $fs"
exit 1
else
if [ "$fs" = "/" ]
then
vOUTFILENAME="$stPREFACE""root""$stYEAR$stMONTH$stDAYOFMONTH""_$stDUMPLEVEL.gz"
else
vOUTFILENAME="$stPREFACE${fs:1}$stYEAR$stMONTH$stDAYOFMONTH""_$stDUMPLEVEL.gz"
fi
#
# Create the command strings
#
comDUMP=""
comCOPYREMOTE=""
comRMLOCAL=""
if [ "$bDUMPLOCAL" -eq "1" ] && [ "$fs" != `fsname "$pathDUMPLOCAL"` ]
#
# Dump local
#
then
comDUMP="eval $pathDUMP $flagDUMP ""-$stDUMPLEVEL"" -f - $fs | gzip $flagGZIP > $pathDUMPLOCAL""/$vOUTFILENAME"
if [ "$bDUMPREMOTE" -eq "1" ]
#
# Copy remote
#
then
comCOPYREMOTE="eval $pathSCP $flagSCP -i $pathKEY $pathDUMPLOCAL""/$vOUTFILENAME $stREMOTEUSER@$stREMOTESERVER:$pathDUMPREMOTE""/$vOUTFILENAME"
if [ "$bSAVELOCAL" -eq "0" ]
#
# Remove local
#
then
comRMLOCAL="eval rm $pathDUMPLOCAL""/$vOUTFILENAME"
fi
fi
#
# Dump remote
#
else
if [ "$fs" = `fsname "$pathDUMPLOCAL"` ]
then
if [ "$bSAVELOCAL" -eq "1" ]
then
echo "WARNING"
echo "Will not dump filesystem into which you are saving other filesystem dumps"
echo "Strongly advise that you change your dump process."
echo "WARNING"
exit 1
else
echo "CAUTION"
echo "No local dump of $fs can be made as the backup path lies within it."
echo "Consider moving your backups from $pathDUMPLOCAL."
echo "CAUTION"
if [ ! "$bDUMPREMOTE" -eq "1" ]
then
echo "WARNING"
echo "No remote dump of $fs was made either as this option is not selected."
echo "WARNING"
fi
fi
fi
if [ "$bDUMPREMOTE" -eq "1" ]
then
comDUMP="eval $pathDUMP $flagDUMP ""-$stDUMPLEVEL"" -f - $fs | gzip $flagGZIP | $pathSSH $flagSSH $stREMOTEUSER@$stREMOTESERVER -i $pathKEY dd of=$pathDUMPREMOTE""/$vOUTFILENAME"
fi
fi
# echo "comDUMP is: $comDUMP"
# echo "comCOPYREMOTE is: $comCOPYREMOTE"
# echo "comRMLOCAL is: $comRMLOCAL"
fi
#
# RUN IT
#
if [ -n "$comDUMP" ]
then
echo "Dumping ... "
$comDUMP
fi
if [ -n "$comCOPYREMOTE" ]
then
echo "Copying to remote ... "
$comCOPYREMOTE
fi
if [ -n "$comRMLOCAL" ]
then
echo "Deleting local ... "
$comRMLOCAL
fi
done
echo "Dump process completed successfully."
exit 0