当前位置:   article > 正文

python和shell脚本,每隔五分钟将远端服务器中的文件夹数据下载到跳板机

python和shell脚本,每隔五分钟将远端服务器中的文件夹数据下载到跳板机

python脚本

import subprocess
import datetime
import time


def run_scp_command(source_path, target_path):
    command = ['scp -r ', source_path, target_path]
    try:
        subprocess.run(command, check=True)
        print("File transferred successfully!")
    except subprocess.CalledProcessError as e:
        print("An error occurred:", e)


while True:
    dt_start = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    source_path = 'xx.xx.xx.xx:/root/Algo/dt_resource/dtt_dg_prod/resource/scada/100_20221221_transient'
    target_path = './' + dt_start
    run_scp_command(source_path, target_path)
    time.sleep(5*60)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

shell脚本

#!/bin/bash  
  
# 函数来执行scp命令  
run_scp_command() {  
    local source_path=$1  
    local target_path=$2  
    scp -r "$source_path" "$target_path"  
    if [ $? -eq 0 ]; then  
        echo "File transferred successfully!"  
    else  
        echo "An error occurred."  
    fi  
}  
  
# 无限循环  
while true; do  
    # 获取当前时间并格式化  
    dt_start=$(date +"%Y%m%d%H%M%S")  
      
    # 定义源文件路径和目标路径  
    source_path='xx.xx.xx.xx:/root/Algo/dt_resource/dtt_dg_prod/resource/scada/100_20221221_transient'  
    target_path="./${dt_start}"  
      
    # 运行scp命令  
    run_scp_command "$source_path" "$target_path"  
      
    # 等待5分钟  
    sleep 300  
done
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

添加权限

chmod +x download.sh
  • 1

执行

./download.sh
  • 1
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/252144
推荐阅读
相关标签
  

闽ICP备14008679号