[FrontPage] [TitleIndex] [WordIndex

This is a read-only archived version of wiki.centos.org

一個以遠端備份選項進行循環式備份的範例腳本。

# 以遠端備份進行循環式備份
# Version 1.1
# Fire Eater <LinuxRockz@gmail.com>
# Feburary 4, 2006
# Released under the GPL License- http://www.fsf.org/licensing/licenses/gpl.txt
##############################################################################
#
SERVER_HOSTNAME="`hostname`"

BACKUP_UID="yourbackupuser"
BACKUP_GID="yourbackupusersgroup"
#
ROOT_BACKUP_DIR="/your/backup/directory"
CONF_DIR="$ROOT_BACKUP_DIR/etc"
#例如:HTTPD、BIND、MYSQL
BACKUP_TYPE="HTTPD"
#
##############
#  遠端備份  #
##############
#
## 利用 Rsync
# 你須要正式建立以金鑰憑證的 ssh 登入。
USE_RSYNC="n"
RSYNC_HOST="yourserver"
SSH_RSYNC_USER="yoursshuser"
SSH_RSYNC_DEST_DIR="/your/remote/folder/$SERVER_HOSTNAME"

## SCP 資訊及目的地
# 你須要正式建立以金鑰憑證的 ssh 登入。
USE_SCP="n"
SCP_HOST="yourserver"
SCP_USER="sshuser"
SCP_REMOTEDIR="/your/remote/folder/$SERVER_HOSTNAME"

## FTP
USE_FTP="n"
#  FTP_SERVER 的語法: ftp://user:pass@ftp.server.com/folder (省略尾隨的 /)
FTP_SERVER="ftp://"

## 電郵資訊及目的地
EMAIL_INFO="n"
BACKUP_EMAIL="youremailaddress"

########################
#  建立目錄/檢查檔案  #
########################

if [ ! -d $CONF_DIR ] ; then
        mkdir -p $CONF_DIR
fi

if [ ! -f $CONF_DIR/homedirs ] ; then
        echo "No Home Directories List Found"
        echo "Please create $CONF_DIR/homedirs"
        exit
fi

################
#  循環式備份  #
################
#
BACKUP_DIR=$ROOT_BACKUP_DIR/$BACKUP_TYPE
BACKUP_DEST="$BACKUP_DIR/backup.0"
BACKUP="$BACKUP_DIR/backup.0"
BACKUP1="$BACKUP_DIR/backup.1"
BACKUP2="$BACKUP_DIR/backup.2"
BACKUP3="$BACKUP_DIR/backup.3"
BACKUP4="$BACKUP_DIR/backup.4"
BACKUP5="$BACKUP_DIR/backup.5"
BACKUP6="$BACKUP_DIR/backup.6"

if [ ! -d $BACKUP_DEST ] ; then
        mkdir -p $BACKUP_DEST
fi

if [ ! -d $BACKUP ] ; then
        mkdir $BACKUP_DIR/backup.0
fi

if [ ! -d $BACKUP1 ] ; then
        mkdir $BACKUP_DIR/backup.1
fi
if [ ! -d $BACKUP2 ] ; then
        mkdir $BACKUP_DIR/backup.2
fi
if [ ! -d $BACKUP3 ] ; then
        mkdir $BACKUP_DIR/backup.3
fi
if [ ! -d $BACKUP4 ] ; then
        mkdir $BACKUP_DIR/backup.4
fi
if [ ! -d $BACKUP5 ] ; then
        mkdir $BACKUP_DIR/backup.5
fi
if [ ! -d $BACKUP6 ] ; then
        mkdir $BACKUP_DIR/backup.6
fi

/bin/rm -rf $BACKUP_DIR/backup.6
mv $BACKUP_DIR/backup.5 $BACKUP_DIR/backup.6
mv $BACKUP_DIR/backup.4 $BACKUP_DIR/backup.5
mv $BACKUP_DIR/backup.3 $BACKUP_DIR/backup.4
mv $BACKUP_DIR/backup.2 $BACKUP_DIR/backup.3
mv $BACKUP_DIR/backup.1 $BACKUP_DIR/backup.2
mv $BACKUP_DIR/backup.0 $BACKUP_DIR/backup.1

if [ ! -d $BACKUP ] ; then
        mkdir $BACKUP_DIR/backup.0
fi
#
#############################
#     開始備份              #
#############################
START_TIME=`date +%a-%D-@-%X-%Z`
##################################
#
HTTP_CONF_DIR="/etc/httpd"
USERS_HOME_DIRECTORY="/home"
USERS_LIST="$CONF_DIR/homedirs"
DATENAME=`date +%Y%m%d`
START_TIME=`date +%a-%D-@-%X-%Z`
#
echo "Backing up HTTPD Conf Directories"
tar cfj $BACKUP_DEST/httpd-$DATENAME.tar.bz2 $HTTP_CONF_DIR  >> /dev/null
#
echo "Backing up Home Directories"
#
for USER_DIR in `cat $USERS_LIST`;
do
tar cfj $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $USERS_HOME_DIRECTORY/$USER_DIR  >> /dev/null
done
#
#設定權限
chown $BACKUP_UID:$BACKUP_GID $BACKUP_DIR
#
##################################
END_TIME=`date +%a-%D-@-%X-%Z`
################################
#       結束備份               #
################################
#
##################
#  遠端備份選項  #
##################
clear
#
if [ $USE_RSYNC = y ] ; then
echo "Preforming Remote Backup via Rsync"
echo ""
su -l $BACKUP_UID -c "rsync -az --stats --partial -e ssh $BACKUP_DIR $SSH_RSYNC_USER@$RSYNC_HOST:$SSH_RSYNC_DEST_DIR"  2>&1
echo "$BACKUP_TYPE backup has been Rsynced to $RSYNC_HOST"
echo ""
else
echo "Rsync not turned on"
fi
#
if [ $USE_SCP = y ] ; then
echo "Preforming Remote Backup via SCP"
echo ""
scp $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $SCP_USER@$SCP_HOST:$SCP_REMOTEDIR
echo ""
echo "$BACKUP_TYPE Backup has been uploaded via scp to $SCP_HOST"
else
echo "SCP not turned on"
fi
#
if [ $USE_FTP = y ] ; then
echo "Preforming Remote Backup vis FTP"
echo ""
curl -T $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $FTP_SERVER/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2
echo "$BACKUP_TYPE  backup has been uploaded via FTP to $FTP_SERVER"
else
echo ""
echo "FTP not turned on"
echo ""
fi
#
################
#  備份的結果  #
################
#
echo "The current $BACKUP_TYPE backup for $SERVER_HOSTNAME has completed and is located in: $BACKUP_DEST"
echo "" 
echo "Total disk space used for the entire $BACKUP_TYPE backup."
echo "Size - Location"
echo "`du -hs "$BACKUP_DIR"`"
echo "" 
echo "Total disk space used for the current $BACKUP_TYPE backup."
echo "Size - Location"
echo "`du -hs "$BACKUP_DEST"`"
#
##############
#  郵寄結果  #
##############
#
if [ $EMAIL_INFO = y ] ; then
echo "$SERVER_HOSTNAME $BACKUP_TYPE Backup Summary" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Backup Start Time: $START_TIME" >> /tmp/emailmsg.txt
echo "Backup End Time: $END_TIME" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Rsync="$USE_RSYNC >> /tmp/emailmsg.txt
echo "SCP="$USE_SCP >> /tmp/emailmsg.txt
echo "FTP="$USE_FTP >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Total disk space used for the entire $BACKUP_TYPE backup." >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Size - Location" >> /tmp/emailmsg.txt
echo "`du -hs "$BACKUP_DIR"`" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Total disk space used for the current $BACKUP_TYPE backup." >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Size - Location" >> /tmp/emailmsg.txt
echo "`du -hs "$BACKUP_DEST"`" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
#
cat /tmp/emailmsg.txt | mail -s"$BACKUP_TYPE Backup Executed for $SERVER_HOSTNAME at $END_TIME" $BACKUP_EMAIL
rm -f /tmp/emailmsg.txt
else
echo "E-Mail not turned on"
fi
exit

Translation of revision 3


2023-09-11 07:23