您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何用linux批量備份服務(wù)器配置文件和目錄的腳本”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
功能:
1. 只在備份機(jī)執(zhí)行,遠(yuǎn)程的服務(wù)器只需要允許備份機(jī)用root通過(guò)密鑰登錄;
2. 在備份機(jī)上配置需要備份哪些服務(wù)器,在NEED_BACKUP_SERVERS這個(gè)數(shù)組增減;
3. 在備份機(jī)上配置需要備份服務(wù)器上哪些文件,在NEED_BACKUP_DETAIL這個(gè)數(shù)組增減,如果需要備份某臺(tái)服務(wù)器上NEED_BACKUP_DETAIL數(shù)組里定義的某個(gè)文件就備份,不存在的文件則自動(dòng)跳過(guò),例如Web服務(wù)器上沒(méi)my.cnf , 也沒(méi)有影響;
4. 如果服務(wù)器上產(chǎn)生的新備份和已經(jīng)存在已往備份內(nèi)容一樣則只保留時(shí)間較早的備份。因?yàn)榉?wù)器的配置文件不是經(jīng)常改動(dòng)的;
5. 通過(guò)修改定義KEEP_BACKUP_NUM為個(gè)變量,可以決定遠(yuǎn)程服務(wù)器上保留最近多少份備份,我這里定義的是10份。備份機(jī)上保留所有遠(yuǎn)程服務(wù)器的備份。因?yàn)榕渲梦募急容^小,不用擔(dān)心磁盤空間占用問(wèn)題。
用過(guò)下列技術(shù):
1. 獲取遠(yuǎn)程服務(wù)器IP,如果某臺(tái)服務(wù)器沒(méi)有公網(wǎng)IP則使用該服務(wù)器的“主機(jī)名_IP.備份年月日_備份時(shí)間.tar.gz
2. 通過(guò)循環(huán)、判斷,對(duì)數(shù)組中的元素重新賦值;
下載之后請(qǐng)留意版本號(hào)和更新日志。
代碼如下:
#!/bin/bash
##################################################################
# Backup_Config_Files_Of_Remote_Servers
# Env: Centos 5.5 x86_64
# Created by Hernan on 2011-04-06
# Copyright 2010 __Chengyongxu.com__. All rights reserved.
#
# Version: 1.4.6
# Revision History
##################################################################
# Local Tmp Dir Of This Script
THIS_SCRIPT_TMP=/tmp/Do_at_remote_server.sh
THIS_SCRIPT_TMP_NAME=`echo $THIS_SCRIPT_TMP|awk -F/ '{print $NF}'`
# Local Backup Dir
LOCAL_BACKUP_DIR=/tmp/IDC_config_backup
# Need Backup Servers And Files List
REMOTE_SERVER_TMP_DIR=/tmp/Backup_server_config_files_tmp
NEED_BACKUP_SERVERS=(\
10.0.0.52
server_a_ip \
server_b_ip \
server_c_ip \
server_d_ip \
chengyongxu.com \
)
NEED_BACKUP_DETAIL=(\
/etc/hosts \
/etc/httpd/conf/*.conf \
/etc/httpd/conf.d/*.conf \
/etc/keepalived/keepalived.conf
/etc/my.cnf \
/etc/postfix \
/root/bin/*sh \
/usr/local/nagios/etc/nagios.cfg \
/usr/local/nagios/etc/objects/*cfg \
/usr/local/nginx/conf \
/usr/local/php/etc/php.ini \
/usr/local/php/etc/php-fpm.conf \
/usr/local/php-fcgi/etc/php.ini \
/usr/local/php-fcgi/etc/php-fpm.conf \
/usr/local/sphinx/etc/*.conf \
/var/spool/cron \
)
KEEP_BACKUP_NUM=10
SSH_PORT=22
SSH_USER=root
##################################################################
#
# Global Functions
#
##################################################################
#========= !!!! Warning ! Don't edit next function !!!! =========#
# Create Script Tmp File
Create_Script (){
cat << EOF > $THIS_SCRIPT_TMP
#!/bin/bash
NEED_BACKUP_DETAIL=(${NEED_BACKUP_DETAIL[*]})
# How many nums the backup needed
BAKNUM=$KEEP_BACKUP_NUM
REMOTE_SERVER_TMP_DIR=$REMOTE_SERVER_TMP_DIR
SERVER_IP=\`/sbin/ifconfig|grep "inet addr"|cut -f 2 -d ":"|cut -f 1 -d " "|grep -vE "^10.|^127|^172.16|^192.168"\`
BACKUP_TIME=\`date +%Y%m%d_%H%M\`
##################################################################
#
# Compress NEED_BACKUP_DETAIL
#
##################################################################
# If IPv4 private address only, use hostname.ip
if [ -z \$SERVER_IP ]
then
SERVER_IP=\`hostname\`_\`/sbin/ifconfig|grep "inet addr"|cut -f 2 -d ":"|cut -f 1 -d " "|grep -vE "^127"|head -n 1\`
fi
# Create Tpm dir
if [ ! -d \$REMOTE_SERVER_TMP_DIR ]
then
mkdir -p \$REMOTE_SERVER_TMP_DIR
fi
# Compress
for file in \${NEED_BACKUP_DETAIL[*]}
do
# When you want to backup the file exists,
# assign to an element of the new array
while [ -e \$file ]
do
NEED_BACKUP_DETAIL_EXISTS[\$num]=\$file
let num++
break
done
done
tar zcfpP \$REMOTE_SERVER_TMP_DIR/\$SERVER_IP.\$BACKUP_TIME.tar.gz \${NEED_BACKUP_DETAIL_EXISTS[*]}
##################################################################
#
# If files are the same,keep one only
#
##################################################################
NEW_BACKUP_ARCHIVE_CONTENT=\`tar tvf \$REMOTE_SERVER_TMP_DIR/\$SERVER_IP.\$BACKUP_TIME.tar.gz\`
for backfile in \`ls \$REMOTE_SERVER_TMP_DIR | grep tar.gz |grep -v \$SERVER_IP.\$BACKUP_TIME\`
do
OLD_BACKUP_ARCHIVE_CONTENT=\`tar tvf \$REMOTE_SERVER_TMP_DIR/\$backfile\`
while [ "\$NEW_BACKUP_ARCHIVE_CONTENT" = "\$OLD_BACKUP_ARCHIVE_CONTENT" ]
do
rm -rvf \$REMOTE_SERVER_TMP_DIR/\$SERVER_IP.\$BACKUP_TIME.tar.gz 1 >> \$REMOTE_SERVER_TMP_DIR/delete_new.log
break
done
done
##################################################################
#
# Keep Some Backup Files Newest
#
##################################################################
count=0
for name in \`ls \$BAKPATH/ |grep -E '[0-9]{6,6}' | sort -r\`
do
count=\$((count+1))
if [ \$count -gt \$BAKNUM ] ; then
echo -n "Cleaning old backup: \$name... "
if [ "\$BAKPATH/\$name" = "/" ] ; then
echo "No rm -rf /!"
exit 1
fi
rm -rf \$BAKPATH/\$name
echo " Done."
else
echo "Keeping old backup: \$name"
fi
done
exit
EOF
}
Copy_Script_To_Remote (){
for server in ${NEED_BACKUP_SERVERS[*]}
do
ssh -p $SSH_PORT -tt $SSH_USER@$server << EOT
mkdir -p $REMOTE_SERVER_TMP_DIR
exit
EOT
scp -P $SSH_PORT $THIS_SCRIPT_TMP $SSH_USER@$server:$REMOTE_SERVER_TMP_DIR/
done
}
Backup_On_Remote_Then_Copy_Back (){
for server in ${NEED_BACKUP_SERVERS[*]}
do
ssh -p $SSH_PORT -tt $SSH_USER@$server << EOP
chmod o+x $REMOTE_SERVER_TMP_DIR/$THIS_SCRIPT_TMP_NAME
sh $REMOTE_SERVER_TMP_DIR/$THIS_SCRIPT_TMP_NAME
rm -f $REMOTE_SERVER_TMP_DIR/$THIS_SCRIPT_TMP_NAME
exit
EOP
rsync -e "ssh -p $SSH_PORT" $SSH_USER@$server:$REMOTE_SERVER_TMP_DIR/*tar.gz $LOCAL_BACKUP_DIR/
done
}
##################################################################
#
# Main
#
##################################################################
#
Create_Script
Copy_Script_To_Remote
Backup_On_Remote_Then_Copy_Back
rm -rf $THIS_SCRIPT_TMP
“如何用linux批量備份服務(wù)器配置文件和目錄的腳本”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。