• python之tomcat自动化备份,更新


    由于tomcat开始集群,部署较为不变,于是写了个python自动化更新备份脚本

    #!/usr/local/bin/python3
    #coding:utf-8
    
    import os, sys, subprocess, time, shutil
    
    site_file = 'xxxxxi'
    update_file = '/home/sourcedir/' + site_file + '.war'
    webapps_file = '/var/tomcat2/webapps/' + site_file + '.war'
    webapps_dir = '/var/tomcat2/webapps/' + site_file
    bakwebapps_file = '/var/tomcat2/bakwebapps/' + site_file + '.war'
    bakwebapps_dir = '/var/tomcat2/bakwebapps/' + site_file
    
    tomcat_pid =  ((subprocess.Popen("lsof -i :8081 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()
    
    print("Tomcat will shutdown after 6s, u can enter Ctrl + c interrupt it ! ")
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
    
    print()
    if len(tomcat_pid) == 0:
        print("> tomcat already shutdown!")
    else:
        subprocess.Popen("/usr/local/tomcat/bin/shutdown.sh > /dev/null 2>&1", shell=True, stdout=subprocess.PIPE)
    
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
        
    if len(tomcat_pid) == 0: 
        pass 
    else :
       subprocess.Popen("kill -9 " + tomcat_pid, shell=True, stdout=subprocess.PIPE)
       print("
    > Tomcat close the failure, kill the pid %s" % tomcat_pid) 
       
    #备份旧站点
    print("
    --------Begin to backup webapps---------
    ")
     
    if os.path.exists(bakwebapps_file):
        shutil.rmtree(bakwebapps_dir)
        print("> Old bak webapps has been deleted!")
        os.remove(bakwebapps_file)
    else:
        pass
    
    
    shutil.copyfile(webapps_file, bakwebapps_file)    
    shutil.copytree(webapps_dir, bakwebapps_dir)
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
    print("
    > Backup completed,Start to update the program...")
    shutil.copyfile(update_file, webapps_file)
    
    print("
    > Update completed,start tomcat")
    subprocess.Popen("/usr/local/tomcat/bin/startup.sh", shell=True, stdout=subprocess.PIPE)
    
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
    tomcat_pid2 =  ((subprocess.Popen("lsof -i :8081 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()
    
    if len(tomcat_pid2) == 0:
        print("
    > Tomcat has not start, Please check u program!")
    else:
        print("
    > Tomcat have already started !")
    
      

    END!

  • 相关阅读:
    vue 移动端商城搭建 (一)
    关于mantisBT2.22安装插件Inline column configuration 2.0.0时提示缺少依赖jQuery UI Library 1.8
    mantis2.22.1中添加管理员密码修改框
    mantisbt2.22.1 中使用自带的phpmailer发送邮件(实测可用)
    Mac配置Jenkins(构建Allure模板报告)
    ATX插件机制-学习学习
    查看包名和Activity的小工具
    pytest跳过指定的测试或模块
    pytest常用命令参数
    关于pytest使用allure生成报告时,报一堆警告和缺少XX模块
  • 原文地址:https://www.cnblogs.com/changbo/p/6269902.html
Copyright © 2020-2023  润新知