#!/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
请发表评论