linux批量備份伺服器設定檔和目錄的指令碼_linux shell

來源:互聯網
上載者:User

功能:
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.