linux批量备份服务器配置文件和目录的脚本
这篇文章主要介绍了linux下批量备份服务器配置文件和目录的方法,需要的朋友可以参考下
功能:
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