- 论坛徽章:
- 0
|
根据公司生产环境的要求,写了一个Shell
Script作为备份策略;运行这个脚本,需要一个集中备份的服务器,其上要运行Rsync,并放开可写入权限;然后,就是在每台需要备份的服务器上定时
运行这个脚本,通过对脚本中的一些变量进行配置,达到自动备份的目的。
目前,这只是一个基本可运行的版本,希望可以听到网友的一些意见,然后再加以修改。
更新日志:
1.脚本新建。
1.增加备份邮件提醒功能,并可以通过附件形式发送备份文件。
2.增加备份文件校验功能,以验证要备份的文件是否存在。
3.考虑删除SCP备份方法,因为通过SCP备份需要输入系统用户口令,存在一定的安全性风险。
###########################################################################
#/bin/bash
#
# backup.sh -- Backup User Specified Files and Directories to
# Local Directory or/and Remote Hosts.
#
# Copyright (C) 2006 Johnny
#
# Usage:
# ./backup.sh
#
###########################################################################
#
# 1. Define Script Variables
#
alias cp='cp'
alias mv='mv -f'
#
# 1.1 Define Basic Variables
#
# script configuration file
# includes files and directories to backup
CONFIG_FILE="/usr/local/bin/backup.conf"
# log file
LOG_FILE="/var/log/backup.log"
# current date, format YYYYMMDD
CUR_DATE=`date +%Y%m%d`
# compress to xxx.tar.gz before backup
# [0], backup without compress
# [1], backup with compress
COMPRESS="1"
# exclusive hostname, null for `hostname`
HOSTNAME=""
# define email users to receive backup information
MAIL_TO="johnny@oop8.com"
# define if send email to $MAIL_TO users
# [0], do not send email
# [1], send email
MAIL_REMIND="0"
# tar command path
TAR="/bin/tar"
# gzip command path
GZIP="/bin/gzip"
#
# 1.2 Define Backup Destination Variables
#
# local destination directory
LOCAL_DEST_DIR="/data/backup/`hostname`"
# remote destination directory, null for `hostname`
REMOTE_DEST_DIR=""
# remote hostname or ip address
REMOTE_HOST="192.168.0.10"
# backup mode
# [local], backup to $LOCAL_DEST_DIR
# [remote], backup to $REMOTE_DEST_DIR
# [both], backup to $LOCAL_DEST_DIR and $REMOTE_DEST_DIR
BACKUP_MODE="both"
#
# 1.3 Define Rsync Options
#
# rsync command path
RSYNC="/usr/local/rsync/bin/rsync"
# rsync module name
RSYNC_MODULE="target"
# rsync command options
RSYNC_OPTIONS="-vzr --port 8585 --progress"
# rsync username
RSYNC_USERNAME="RsyncBackupUsername"
# rsync password
RSYNC_PASSWORD="RsyncBackupPassword"
###########################################################################
#
# 2. Verify System Variables
#
# check if $CONFIG_FILE exists
[ ! -f "$CONFIG_FILE" ] &&
{
echo "$CONFIG_FILE not exists.";
exit 1;
}
# check if $LOG_FILE exists
[ ! -f "$LOG_FILE" ] &&
{
echo "$LOG_FILE not exists, create it now.";
echo "" > $LOG_FILE;
}
# check if $LOCAL_DEST_DIR exists
[ "$BACKUP_MODE" == "local" -a ! -d "$LOCAL_DEST_DIR" ] &&
{
echo "$LOCAL_DEST_DIR not exists.";
exit 1;
}
# check if $MAIL_TO exists
[ "$MAIL_REMIND" == "1" -a -z "$MAIL_TO" ] &&
{
echo "$MAIL_TO cannot be null.";
exit 1;
}
# check if $COMPRESS exists
[ "$COMPRESS" == "" \
-o "$COMPRESS" != "0" \
-a "$COMPRESS" != "1" ] &&
{
echo "variable COMPRESS is invalid.";
exit 1;
}
# check if $BACKUP_MODE exists
[ "$BACKUP_MODE" == "" \
-o "$BACKUP_MODE" != "local" \
-a "$BACKUP_MODE" != "remote" \
-a "$BACKUP_MODE" != "both" ] &&
{
echo "variable BACKUP_MODE is invalid.";
exit 1;
}
# check if $RSYNC exists
[ ! -f "$RSYNC" -o ! -x "$RSYNC" ] &&
{
echo "$RSYNC not installed or cannot execute.";
exit 1;
}
# check if $TAR exists
[ ! -f "$TAR" -o ! -x "$TAR" ] &&
{
echo "$TAR not installed or cannot execute.";
exit 1;
}
# check if $GZIP exists
[ ! -f "$GZIP" -o ! -x "$GZIP" ] &&
{
echo "$GZIP not installed or cannot execute.";
exit 1;
}
# set HOSTNAME if null
[ -z "$HOSTNAME" ] &&
{
HOSTNAME=`hostname`;
}
###########################################################################
#
# 3. Function Lists
#
#
# 3.1 Create Target File
#
createTarget()
{
echo "Creating target file..." | tee -a "$LOG_FILE"
rm -fr /tmp/"$CUR_DATE"
mkdir -p /tmp/"$CUR_DATE"
# if $COMPRESS set to 1, create xxx.tar.gz
if [ "$1" == "1" ]; then
echo "Backup type is TAR." | tee -a "$LOG_FILE"
touch /tmp/"$CUR_DATE"/"$HOSTNAME"-"$CUR_DATE".tar
for i in `cat "$CONFIG_FILE"`
do
[ ! -e "$i" ] &&
{
echo -e "\tWarninig: $i cannot be found." | tee -a "$LOG_FILE";
continue;
}
"$TAR" rf /tmp/"$CUR_DATE"/"$HOSTNAME"-"$CUR_DATE".tar "$i" >
/dev/null 2>&1
done
"$GZIP" /tmp/"$CUR_DATE"/"$HOSTNAME"-"$CUR_DATE".tar
# if $COMPRESS set to 0, create target directory
elif [ "$1" == "0" ]; then
echo "Backup type is DIRECTORY." | tee -a "$LOG_FILE"
mkdir -p /tmp/"$CUR_DATE"/"$HOSTNAME"-"$CUR_DATE"
for i in `cat "$CONFIG_FILE"`
do
[ ! -e "$i" ] &&
{
echo -e "\tWarninig: $i cannot be found." | tee -a "$LOG_FILE";
continue;
}
cp
-aRf "$i" /tmp/"$CUR_DATE"/"$HOSTNAME"-"$CUR_DATE"/ > /dev/null
2>&1
done
fi
}
#
# 3.2 Create Rsync Password File
#
createRsyncPasswdFile()
{
echo "Creating rsync password file..." | tee -a "$LOG_FILE"
echo "" > /tmp/rsync.password
echo "$RSYNC_PASSWORD" >> /tmp/rsync.password
chmod 600 /tmp/rsync.password
}
#
# 3.3 Backup to Local
#
backupLocal()
{
echo "Backuping to local..." | tee -a "$LOG_FILE"
cp -aRf /tmp/"$CUR_DATE"/* "$LOCAL_DEST_DIR"/ > /dev/null 2>&1
}
#
# 3.4 Backup to Remote
#
backupRemote()
{
echo "Backuping to remote..." | tee -a "$LOG_FILE"
if [ -z "$REMOTE_DEST_DIR" ]; then
REMOTE_DEST_DIR="$HOSTNAME"
fi
createRsyncPasswdFile
"$RSYNC" $RSYNC_OPTIONS /tmp/"$CUR_DATE"/ \
"$RSYNC_USERNAME"@"$REMOTE_HOST"::"$RSYNC_MODULE"/"$REMOTE_DEST_DIR"/ \
--password-file=/tmp/rsync.password > /dev/null 2>&1
rm -f /tmp/rsync.password
}
###########################################################################
#
# 4. Main Script
#
CUR_TIME=`date`
echo
"
" | tee -a "$LOG_FILE"
echo "Begin to backup at $CUR_TIME." | tee -a "$LOG_FILE"
# create target file
createTarget "$COMPRESS"
# begin to backup reference to $BACKUP_MODE
if [ "$BACKUP_MODE" == "local" ]; then
backupLocal
elif [ "$BACKUP_MODE" == "remote" ]; then
backupRemote
elif [ "$BACKUP_MODE" == "both" ]; then
backupLocal
backupRemote
fi
echo "Backup succussfully at `date`." | tee -a "$LOG_FILE"
# send email to remind users
if [ "$MAIL_REMIND" == "1" ]; then
FROM_LINE=`cat -n "$LOG_FILE" | grep "$CUR_TIME" | grep "Begin" | awk '{print $1}'`
sed -n "$FROM_LINE,\$p" "$LOG_FILE" > /tmp/"$CUR_DATE"/mail.txt
mail -s "Backup Information on $HOSTNAME" "$MAIL_TO" < /tmp/"$CUR_DATE"/mail.txt
fi
# remove temporary directory
rm -fr /tmp/"$CUR_DATE"
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3959/showart_93392.html |
|