Linux bulk Backup server configuration files and directories script _linux Shell

Source: Internet
Author: User
Tags fpm mkdir tmp file ssh

Function:
1. Only in the backup machine, the remote server only need to allow the backup machine with root through the key login;
2. Configure which servers to back up on the backup machine, add or subtract the array in need_backup_servers;
3. Configure what files on the server need to be backed up on the backup machine, add or subtract the array in Need_backup_detail, and if you need to back up a file defined in the Need_backup_detail array on a server, the nonexistent files are automatically skipped, For example, there is no my.cnf on the Web server and no impact;
4. If the new backup generated on the server is the same as the previous backup content, only the older backups are retained. Because the server's configuration file is not constantly changed;
5. By modifying the definition keep_backup_num as a variable, you can determine how many copies of the most recent backups are left on the remote server, and I define 10. Keep Backups of all remote servers on the backup machine. Because the configuration files are small, do not worry about disk space consumption.

The following techniques have been used:
1. Get remote server IP, if a server does not have public network IP, use the server's "hostname _ip." Backup Date _ backup time. tar.gz
2. Through circulation, judgment, the elements in the array of the re-assignment;

Please note the version number and update log after downloading.

Copy Code code as follows:

#!/bin/bash
##################################################################
# backup_config_files_of_remote_servers
# Env:centos 5.5 x86_64
# Created by Hernan on 2011-04-06
# Copyright __chengyongxu.com__. All rights reserved.
#
# version:1.4.6
# Revision History
##################################################################

# local TMP Dir in 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[*]}
Todo
# When you want to backup the file exists,
# Assign to an element of the new array
While [e \ $file]
Todo
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
    do
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[*]}
Todo
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[*]}
Todo
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.