功能:
1. 只在備份機執行,遠程的服務器只需要允許備份機用root通過密鑰登錄;
2. 在備份機上配置需要備份哪些服務器,在NEED_BACKUP_SERVERS這個數組增減;
3. 在備份機上配置需要備份服務器上哪些文件,在NEED_BACKUP_DETAIL這個數組增減,如果需要備份某台服務器上 NEED_BACKUP_DETAIL數組裡定義的某個文件就備份,不存在的文件則自動跳過,例如Web服務器上沒my.cnf , 也沒有影響;
4. 如果服務器上產生的新備份和已經存在已往備份內容一樣則只保留時間較早的備份。因為服務器的配置文件不是經常改動的;
5. 通過修改定義KEEP_BACKUP_NUM為個變量,可以決定遠程服務器上保留最近多少份備份,我這裡定義的是10份。備份機上保留所有遠程服務器的備份。因為配置文件都比較小,不用擔心磁盤空間占用問題。
用過下列技術:
1. 獲取遠程服務器IP,如果某台服務器沒有公網IP則使用該服務器的“主機名_IP.備份年月日_備份時間.tar.gz
2. 通過循環、判斷,對數組中的元素重新賦值;
下載之後請留意版本號和更新日志。
復制代碼 代碼如下:
#!/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