一个以远程备份选项进行循环式备份的样例脚本。
# 以远程备份进行循环式备份 # 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