首页 > 其他分享 >定时自动备份【脚本】

定时自动备份【脚本】

时间:2022-11-18 14:58:58浏览次数:84  
标签:脚本 devicename Network Backup 备份 echo home 定时 Config

建立执行脚本

vi /home/Network_Config_Backup/start.sh

#!/bin/bash
#使用该脚本前需要安装tftp,xinetd,expect
#取时间戳
BACKUP_DATA=`date +%Y%m%d`
#创建时间戳的备份目录
mkdir -p /home/Network_Config_Backup/$BACKUP_DATA

#TFTP服务根目录授权
chmod 777 /home/Network_Config_Backup/$BACKUP_DATA

log=/data/log/start_${BACKUP_DATA}.log
#修改TFTP根目录
sed -i "s/Network_Config_Backup\/......../Network_Config_Backup\/$BACKUP_DATA/" /etc/xinetd.d/tftp
echo
echo
echo
echo ------------------------------------------------------------
echo 
echo          该脚本适用于Cisco、Huawei、H3C 网络设备使用 
echo          Cisco使用tftp方式将配置文件上传到服务器 
echo          Huawei、H3C使用窗口记录方式获取配置信息 
echo 
echo ------------------------------------------------------------
echo
echo 备份文件存放位置: /home/Network_Config_Backup/$BACKUP_DATA
echo
echo
echo
systemctl restart xinetd
sleep 4

#账号密码及IP地址存放在user.passwd中,通过awk获取所有IP并存放在数组IPADDR(大写)
DEVICENAME=(`cat /home/Network_Config_Backup/user.passwd | awk '{print $1}'`)

#for循环,取出所有设备名称devicename(小写)
for devicename in ${DEVICENAME[@]} 
do
#输出备份开始时间戳
echo "`date +%H:%M:%S` 开始备份$devicename"

#根据设备名称devicename取出相应交换机的IP地址、用户、密码
IPADDR=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $3}'`)
echo ${IPADDR}
USERNAME=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $4}'`)
echo ${USERNAME}
PASSWORD=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $5}'`)
echo ${PASSWORD}
TABLE=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $2}'`)
echo ${TABLE}
devicename=${devicename}_${BACKUP_DATA}
if [ $TABLE = Cisco ]
then
/usr/bin/expect >> /home/Network_Config_Backup/$BACKUP_DATA/log  << EOF
    set timeout 10
    spawn telnet $IPADDR
    expect "*sername:"
    send "$USERNAME\r"
    expect "*assword:"
    send "$PASSWORD\r"
    expect {
    "*>" {
        send "enable\r"
        expect "*assword:"
        send "$PASSWORD\r"
        expect "*#"
        send "copy running-config tftp:\r"
        expect "*remote host*"
        send "tftp服务器IP\r"
        expect "*filename*"
        send "$devicename\n"
        expect "*!*"
        send "\n"
    }
    "*#" {
        send "copy running-config tftp:\r"
        expect "*remote host*"
        send "tftp服务器IP\r"
expect "*filename*" send "$devicename\n" expect "*!*" send "\n" } } EOF elif [ $TABLE = Huawei -o H3C ] then /usr/bin/expect >> /dev/null << EOF set timeout 100 log_file /data2/SW/$devicename spawn ssh ${USERNAME}@$IPADDR expect "*assword:" send "$PASSWORD\r" expect "*>" send "display current-configuration\r" while (1) { expect { "*--- More ----" { send " " } "return" { break } } } EOF fi sleep 3 #判断备份目录中是否存在$devicename,如果存在则检查文件内是否存在字符串end或者return。如果文件不存在视为备份失败,如果不存在end或者return视为备份失败 if [ -f "/data2/SW/$devicename" ];then grep -E "^end$|^return$" /data2/SW/$devicename if [ $? -eq 0 ];then echo "$devicename $IPADDR 备份成功" echo "$devicename $IPADDR 备份成功" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" else echo "$devicename $IPADDR 备份失败" echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" fi else echo "$devicename $IPADDR 备份失败" echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" fi done

 

chmod 777 /home/Network_Config_Backup/start.sh
新建user.passwd存放设备信息

vi /home/Network_Config_Backup/user.passwd
*设备名 品牌 IP地址 用户名 密码*

user.passwd()格式

Huawei-TEST-SW01 Huawei 192.168.1.55 admin admin12345

Cisco-TEST-SW01 Cisco 192.168.1.66 admin admin12345

 

 

 

cd /home/Network_Config_Backup

sh start.sh //执行脚本即可自动备份

修改tftp文件储存位置

vi /etc/xinetd.d/tftp 
service tftp
{     socket_type            = dgram
      protocol               = udp
      wait                   = yes
      user                   = root
      server                 = /usr/sbin/in.tftpd
      server_args            = -s /data2/SW -c
      disable                = no
      per_source             = 11
      cps                    = 100 2
      flags                  = IPv4
}

以上出自Cisco、华为、H3C交换机配置文件自动备份_fsk2684的博客-CSDN博客

 

由于环境不同可更改部分内容例如存储位置

 

远程连接方式【telnet,ssh】

 

 

 

 

 

 

 

 

 

 

 

#!/bin/bash#使用该脚本前需要安装tftp,xinetd,expect#取时间戳BACKUP_DATA=`date +%Y%m%d`#创建时间戳的备份目录mkdir -p /home/Network_Config_Backup/$BACKUP_DATA
#TFTP服务根目录授权chmod 777 /home/Network_Config_Backup/$BACKUP_DATA
log=/data/log/start_${BACKUP_DATA}.log#修改TFTP根目录sed -i "s/Network_Config_Backup\/......../Network_Config_Backup\/$BACKUP_DATA/" /etc/xinetd.d/tftpechoechoechoecho ------------------------------------------------------------echo echo          该脚本适用于Cisco、Huawei、H3C 网络设备使用 echo          Cisco使用tftp方式将配置文件上传到服务器 echo          Huawei、H3C使用窗口记录方式获取配置信息 echo echo ------------------------------------------------------------echoecho 备份文件存放位置: /home/Network_Config_Backup/$BACKUP_DATAechoechoechosystemctl restart xinetdsleep 4
#账号密码及IP地址存放在user.passwd中,通过awk获取所有IP并存放在数组IPADDR(大写)DEVICENAME=(`cat /home/Network_Config_Backup/user.passwd | awk '{print $1}'`)
#for循环,取出所有设备名称devicename(小写)for devicename in ${DEVICENAME[@]} do#输出备份开始时间戳echo "`date +%H:%M:%S` 开始备份$devicename"
#根据设备名称devicename取出相应交换机的IP地址、用户、密码IPADDR=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $3}'`)echo ${IPADDR}USERNAME=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $4}'`)echo ${USERNAME}PASSWORD=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $5}'`)echo ${PASSWORD}TABLE=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $2}'`)echo ${TABLE}devicename=${devicename}_${BACKUP_DATA}if [ $TABLE = Cisco ]then/usr/bin/expect >> /home/Network_Config_Backup/$BACKUP_DATA/log  << EOFset timeout 10spawn telnet $IPADDRexpect "*sername:"send "$USERNAME\r"expect "*assword:"send "$PASSWORD\r"expect {"*>" {send "enable\r"expect "*assword:"send "$PASSWORD\r"expect "*#"send "copy running-config tftp:\r"expect "*remote host*"send "10.20.5.5\r"expect "*filename*"send "$devicename\n"expect "*!*"send "\n"}"*#" {send "copy running-config tftp:\r"expect "*remote host*"send "10.20.5.5\r"expect "*filename*"send "$devicename\n"expect "*!*"send "\n"}}EOFelif [ $TABLE = Huawei -o H3C ]then/usr/bin/expect >> /dev/null << EOFset timeout 100log_file /data2/SW/$devicenamespawn ssh ${USERNAME}@$IPADDRexpect "*assword:"send "$PASSWORD\r"expect "*>"send "display current-configuration\r"while (1) {expect {"*--- More ----" { send " " }"return" { break }}}EOFfisleep 3
#判断备份目录中是否存在$devicename,如果存在则检查文件内是否存在字符串end或者return。如果文件不存在视为备份失败,如果不存在end或者return视为备份失败if [ -f "/data2/SW/$devicename" ];thengrep -E "^end$|^return$" /data2/SW/$devicenameif [ $? -eq 0 ];thenecho "$devicename $IPADDR 备份成功"echo "$devicename $IPADDR 备份成功" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"elseecho "$devicename $IPADDR 备份失败"echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"fielseecho "$devicename $IPADDR 备份失败"echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"fi
done

标签:脚本,devicename,Network,Backup,备份,echo,home,定时,Config
From: https://www.cnblogs.com/ctdd/p/16903181.html

相关文章

  • 定时自动备份【TFTP服务器搭建】
    一、准备在centos7上安装部署TFTP服务器。(我使用版本:CentOSLinuxrelease7.9.2009(Core))二、安装过程1,安装TFTP服务器yuminstallxinetdtftptftp-server2、......
  • bash脚本批量启动停止jar包
    #!/bin/sh#端口号PORTS=(100868380858088890080878089)#系统模块MODULES=(registrygatewayuserwelcowisdomauthresources)#系统模块名称MODULE_NAMES=(注册......
  • kafka集群启动脚本失效
    问题描述:之前写的kafka集群启动脚本,今天重启服务器后失效了,只启动了本地的kafka1,另一台虚拟机上的kafka2没启动也没有日志#!/bin/bashcase$1in"start"){......
  • Linux 常用命令脚本使用
    1、#删除/目录下大小为0的文件#!/bin/bashdir="/"find/-typef-size0-execrm-rf{}\;2、查看可登录系统的账户数量[root@libin5shell]#grep"bash$"/etc/......
  • Shell脚本防范Dos攻击
    #!/bin/bashDATE=$(date+%d/%b/%Y:%H:%M)LOG_FILE=/usr/local/nginx/logs/access.log#通过访问日志提取IP地址ABNORMAL_IP=$(tail-n5000$LOG_FILE|grep$DATE|a......
  • shell 脚本中包含环境变量的设置命令需要用 source 命令执行才会生效
    因为每次运行程序都得设置一些环境变量,所以我在想能不能把这些环境变量的设置放到一个shell脚本里,这样我每次执行这个脚本就行了,不用每次都去到处找环境变量的值应该设置成......
  • jenkins定时备份插件 ThinBackup
    一、概述jenkins备份插件是:ThinBackup 二、安装ThinBackupJenkins插件中心搜索ThinBackup,点击直接安装即可。插件官网:https://plugins.jenkins.io/thinBackup安......
  • 定时器方案红黑树,时间轮,最小堆
      腾讯面试题:定时器误差大,该怎么处理?答:利用定时信号加上定时器来解决。采用红黑树,定时信号打断epoll_wait。多线程情况下,单起一个线程做定时 定时器的本质是越近......
  • 【解决】PowerShell:因为在此系统上禁止运行脚本
    文章转自鲸鱼啊:https://www.jingyua.com在自己编PowerShell脚本的时候突然遇到这个问题:无法加载文件C:\Users\DH\Desktop\cs\rename.ps1,因为在此系统上禁止运行脚本。有......
  • 129-mysqldump备份与恢复
    编码:(utf8/utf8mb4)mysqldump--host=192.168.60.15--password=wellDone@123--port=3306–-default-character-set=utf8--user=zhgaSqgkSq-Rzhga-sqgk-sq>e:/qz_s......