当前位置: 操作系统/服务器>linux
备份shell脚本实例代码
来源: 互联网 发布时间:2014-10-13
本文导语: 1、backup_run.sh 代码如下: #!/bin/sh # backup_run # script to run the backups # loads in a setting file for the user to change SOURCE=/home/bob/backup.defaults check_source() { # check_source # can we load the file ...
1、backup_run.sh
#!/bin/sh
# backup_run
# script to run the backups
# loads in a setting file for the user to change
SOURCE=/home/bob/backup.defaults
check_source()
{
# check_source
# can we load the file
# backup.defaults is the source file containing config/functions
# make sure your path includes this directory you are runing from
if [ -r $SOURCE ];then
. $SOURCE # load $source
else
echo "`basename $0`: cannot locate default file"
exit 1
fi
}
header()
{
# header
USER=`whoami`
MYDATE=`date +%A" "%e" of "%B-%Y`
clear
cat &1
if [ $? -ne 0 ];then
return 1
else
return 0
fi
}
#====================== main =======================
# can we source the file
check_source
header
# display the loaded in variables
show_settings
# ask user if he/she wants to change any settings
if continue_prompt "Do you wish To Change Some of The System Defaults" "Y";
then
echo $?
# yes then enter code name
if get_code;then
# change some settings
change_settings
fi
fi
#------------------ got settings... now do backup
if check_drive;then
echo "tape OK..."
else
echo "Cannot rewind the tape..Is it in the tape drive???"
echo "check it out"
exit 1
fi
# file system paths to backup
case $_TYPE in
Full|full)
BACKUP_PATH="sybase syb/suppor etc var bin apps usr/local"
Normal|normal)
BACKUP_PATH="etc var bin apps usr/local"
Sybase|sybase)
BACKUP_PATH="sybase syb/suppor"
esac
# now for backup
cd /
echo "Now starting backup......"
find $BACKUP_PATH -print | cpio -ovB -O /dev/$_DEVICE >> $_LOGFILE 2>&1
# if the above cpio does not work on your system try cpio below, instead
# find $BACKUP_PATH -print | cpio -ovB > /dev/$_DEVICE >> $_LOGFILE 2>&1
# to get more information on the tape change -ovB to -ovcC66536
if [ "$_INFORM" = "yes" ];then
echo "Backup finished check the log file" | mail admin
fi
2、backup.defaults
#!/bin/sh
# backup.defaults
# configuration default file for network backups
# edit this file at your own
# name backup.defaults
# --------------------------------------------
# not necessary for the environments to be in quotes.. but
_CODE="comet"
_LOGFILE="/appdva/backup/log.`date +%y%m%d`"
_DEVICE="rmt0"
_INFORM="yes"
_TYPE="Full"
#---------------------------------------------
continue_prompt()
{
# continue_prompt
# to call: continue_prompt "string to display" default_answer
_STR=$1
_DEFAULT=$2
# check we have the right params
if [ $# -lt 1 ];then
echo "continue_prompt: I need a string to display"
return 1
fi
while :
do
echo -n "$_STR [Y..N] [$_DEFAULT]:"
read _ANS
if [ "$_ANS" = "" ];then
: ${_ANS:=$_DEFAULT}
case $_ANS in
Y) return 0
N) return 1
esac
fi
# user has selected something
case $_ANS in
y|Y|Yes|YES)
return 0
n|N|No|NO)
return 1
*) echo "Answer either Y or N, default is $_DEFAULT"
esac
echo $_ANS
done
}
3、运行:
$./backup_run.sh backup.defaults
代码如下:
#!/bin/sh
# backup_run
# script to run the backups
# loads in a setting file for the user to change
SOURCE=/home/bob/backup.defaults
check_source()
{
# check_source
# can we load the file
# backup.defaults is the source file containing config/functions
# make sure your path includes this directory you are runing from
if [ -r $SOURCE ];then
. $SOURCE # load $source
else
echo "`basename $0`: cannot locate default file"
exit 1
fi
}
header()
{
# header
USER=`whoami`
MYDATE=`date +%A" "%e" of "%B-%Y`
clear
cat &1
if [ $? -ne 0 ];then
return 1
else
return 0
fi
}
#====================== main =======================
# can we source the file
check_source
header
# display the loaded in variables
show_settings
# ask user if he/she wants to change any settings
if continue_prompt "Do you wish To Change Some of The System Defaults" "Y";
then
echo $?
# yes then enter code name
if get_code;then
# change some settings
change_settings
fi
fi
#------------------ got settings... now do backup
if check_drive;then
echo "tape OK..."
else
echo "Cannot rewind the tape..Is it in the tape drive???"
echo "check it out"
exit 1
fi
# file system paths to backup
case $_TYPE in
Full|full)
BACKUP_PATH="sybase syb/suppor etc var bin apps usr/local"
Normal|normal)
BACKUP_PATH="etc var bin apps usr/local"
Sybase|sybase)
BACKUP_PATH="sybase syb/suppor"
esac
# now for backup
cd /
echo "Now starting backup......"
find $BACKUP_PATH -print | cpio -ovB -O /dev/$_DEVICE >> $_LOGFILE 2>&1
# if the above cpio does not work on your system try cpio below, instead
# find $BACKUP_PATH -print | cpio -ovB > /dev/$_DEVICE >> $_LOGFILE 2>&1
# to get more information on the tape change -ovB to -ovcC66536
if [ "$_INFORM" = "yes" ];then
echo "Backup finished check the log file" | mail admin
fi
2、backup.defaults
代码如下:
#!/bin/sh
# backup.defaults
# configuration default file for network backups
# edit this file at your own
# name backup.defaults
# --------------------------------------------
# not necessary for the environments to be in quotes.. but
_CODE="comet"
_LOGFILE="/appdva/backup/log.`date +%y%m%d`"
_DEVICE="rmt0"
_INFORM="yes"
_TYPE="Full"
#---------------------------------------------
continue_prompt()
{
# continue_prompt
# to call: continue_prompt "string to display" default_answer
_STR=$1
_DEFAULT=$2
# check we have the right params
if [ $# -lt 1 ];then
echo "continue_prompt: I need a string to display"
return 1
fi
while :
do
echo -n "$_STR [Y..N] [$_DEFAULT]:"
read _ANS
if [ "$_ANS" = "" ];then
: ${_ANS:=$_DEFAULT}
case $_ANS in
Y) return 0
N) return 1
esac
fi
# user has selected something
case $_ANS in
y|Y|Yes|YES)
return 0
n|N|No|NO)
return 1
*) echo "Answer either Y or N, default is $_DEFAULT"
esac
echo $_ANS
done
}
3、运行:
代码如下:
$./backup_run.sh backup.defaults