Loading
0

通过shell和expect脚本实现自动打包压缩并上传到另一台服务器
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器
前言:需要将服务器A中的某些目录打包压缩后自动上传到服务器B的某个目录下,如果没有这个目录,则通过脚本获取A服务器的版本信息自动创建对应的目录。经过一番折腾,实现了这个需求。可以用来做数据备份。
脚本很乱,只适合我的需求,仅供参考。
需求:服务器A中的某些目录打包压缩后自动上传到服务器B的某个目录下
脚本分两部分:
1、打包脚本

#!/bin/bash  
mkdir /zipbak  
if [ -f /etc/debian_version ]; then  
    sudo apt update      
    sudo apt install acl  
    sudo apt install -y zip unzip expect
else  
    sudo yum -y install acl
    sudo yum install -y zip unzip expect
fi
#打包/www/wwwroot下的data和web目录,并将压缩包放到/zipbak/下面。
cd /www/wwwroot
zip -r /zipbak/data.zip data
zip -r /zipbak/web.zip web
#调用上传脚本
expect /root/up/up.sh

上传脚本:
上传脚本是用 expect  写的。规则如下:(具体青根据自己的情况修改)

#!/usr/bin/expect

# 设定超时时间为 60000 秒
set timeout 60000

# 远程服务器信息,修改为自己的
set remote_ip "8.8.8.8"
set remote_port "22"
set remote_user "root"
set remote_password "123456"

# 本地目录路径
set local_dir "/zipbak"

# 获取本地操作系统名称和版本号的函数
proc get_local_os_info {} {
    global xitong

    # 检查是否存在 /etc/os-release 文件 如果您没有这些需求,可以将对应的脚本去掉即可。
    if {[file exists "/etc/os-release"]} {
        set output [exec cat /etc/os-release]

        # 提取 VERSION_ID
        if {[regexp {VERSION_ID="([^"]+)"} $output match xitong]} {
            set xitong [string map { "\"" "" } $xitong] ; # 去除双引号(如果有的话)
        } else {
            puts "未能获取 VERSION_ID"
            exit 1
        }
    } else {
        puts "找不到 /etc/os-release 文件"
        exit 1
    }
    
    # 如果是CentOS,尝试获取更详细的版本信息 如果您没有这些需求,可以将对应的脚本去掉即可。
    if {[string match "*centos*" $output]} {
        set output_redhat [exec cat /etc/redhat-release]
        
        # 提取 CentOS 版本号 如果您没有这些需求,可以将对应的脚本去掉即可。
        if {[regexp {CentOS Linux release ([0-9.]+)} $output_redhat match version]} {
            set xitong $version
        } else {
            puts "未能获取 CentOS 版本号"
            exit 1
        }
    }
}

# 登录远程服务器并上传文件的函数
proc upload_files {} {
    global remote_ip remote_port remote_user remote_password local_dir xitong

    # 获取操作系统名称,如果您没有这些需求,可以将对应的脚本去掉即可。
    set os ""
    set output [exec cat /etc/os-release]
    if {[string match "*rocky*" $output]} {
        set os "Rocky"
    } elseif {[string match "*Alibaba*" $output]} {
        set os "Alibaba_Cloud_Linux"
    } elseif {[string match "*Anolis*" $output]} {
        set os "Anolis"
    } elseif {[string match "*centos*" $output]} {
        set os "CentOS"
    } elseif {[string match "*Ubuntu*" $output]} {
        set os "Ubuntu"
    } elseif {[string match "*debian*" $output]} {
        set os "Debian"        
    } elseif {[string match "*opensuse*" $output]} {
        set os "OpenSUSE"
    } elseif {[string match "*freebsd*" $output]} {
        set os "FreeBSD"
    } else {
        puts "未知的操作系统"
        exit 1
    }

    # 设置远程目标路径
    set remote_dir "/opt/aaa/data/public/Linux/$os/$xitong"

    # 登录远程服务器并创建目录
    spawn ssh -p $remote_port $remote_user@$remote_ip
    expect {
        "yes/no" {
            send "yes\r"
            exp_continue
        }
        "password:" {
            send "$remote_password\r"
        }
    }
    expect {
        "# " {
            send "mkdir -p $remote_dir\r"
            expect "# "
            send "exit\r"
        }
        timeout {
            puts "登录超时"
            exit 1
        }
        eof {
            puts "登录失败"
            exit 1
        }
    }

    # 上传本地文件到远程服务器(自动输入密码
    foreach file [glob -nocomplain $local_dir/*] {
        if {[file isfile $file]} {
            spawn scp -P $remote_port $file $remote_user@$remote_ip:$remote_dir
            expect {
                "yes/no" {
                    send "yes\r"
                    exp_continue
                }
                "password:" {
                    send "$remote_password\r"
                }
            }
            expect {
                "100%" {
                    send "\r"
                    puts "文件 [file tail $file] 上传成功"
                }
                "# " {
                    send "echo \"文件 [file tail $file] 上传失败\"\r"
                }
                timeout {
                    puts "上传文件 [file tail $file] 超时"
                    exit 1
                }
                eof {
                    puts "上传文件 [file tail $file] 失败"
                    exit 1
                }
            }
        }
    }
}

# 主程序流程
get_local_os_info
upload_files

我将脚本上传到了我网站,在服务器中执行即可。

centos:

cd ~
yum install -y zip unzip expect && wget https://www.zfcdn.xyz/soft/test.zip && test.zip && cd up && chmod +x dabao.sh && chmod +x up.sh && sh dabao.sh

乌班图:

cd~
sudo apt update  && sudo apt install -y zip unzip expect && wget https://www.zfcdn.xyz/soft/test.zip && unzip test.zip && cd up && chmod +x dabao.sh && chmod +x up.sh && sh dabao.sh


 
301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-3-36346-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?
上一篇:解决:无法通过IPv6地址登录SSH22端口方法
下一篇:返回列表