码迷,mamicode.com
首页 > 编程语言 > 详细

python之tomcat自动化备份,更新

时间:2017-01-10 17:20:21      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:os.path   process   move   utf-8   root   err   war   oca   already   

由于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("\n> Tomcat close the failure, kill the pid %s" % tomcat_pid) 
   
#备份旧站点
print("\n--------Begin to backup webapps---------\n")
 
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("\n> Backup completed,Start to update the program...")
shutil.copyfile(update_file, webapps_file)

print("\n> 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("\n> Tomcat has not start, Please check u program!")
else:
    print("\n> Tomcat have already started !")

  

 

END!

 

python之tomcat自动化备份,更新

标签:os.path   process   move   utf-8   root   err   war   oca   already   

原文地址:http://www.cnblogs.com/changbo/p/6269902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!