Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Scripts for how to bulk back up server configuration files and directories with linux

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to back up the scripts of server configuration files and directories in batches with linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Features:

1. Execute only on the backup machine, and the remote server only needs to allow the backup machine to log in through the key using root

two。 Configure which servers need to be backed up on the backup machine, and add or subtract from the NEED_BACKUP_SERVERS array

3. Configure on the backup machine which files on the server need to be backed up. Add or subtract the files in the NEED_BACKUP_DETAIL array. If you need to back up a file defined in the NEED_BACKUP_DETAIL array on a certain server, it will back up, and the files that do not exist will be skipped automatically. For example, there is no my.cnf on the Web server, and there will be no effect.

4. If the new backup on the server is the same as the previous backup, only the older backup is retained. Because the configuration file of the server is not often changed.

5. By modifying the definition of KEEP_BACKUP_NUM as a variable, you can determine how many recent backups are kept on the remote server, which I define here as 10. Keep backups of all remote servers on the backup machine. Because the configuration files are relatively small, you don't have to worry about disk space consumption.

The following techniques have been used:

1. Get the remote server IP. If a server does not have a public network IP, use its hostname _ IP. Backup year, month, day, backup time .tar.gz

two。 Through loop and judgment, the elements in the array are re-assigned.

Please pay attention to the version number and update log after download.

The code is as follows:

#! / 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 $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_ exits [\ $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] {6jue 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report