工作需要,为了快捷部署,节省时间,避免部署可能出现的错误问题(数据库的备份,应用的备份),一次写好Linux和Python的脚本,多次使用,原创做个笔记:
此文与前面的 Linux和Python脚本自动部署应用一例(1) 完成相同的功能操作:
Linux python script:
#!/usr/bin/python ###dennis zhao python script automatic deploy application server ###2013-08-01 python 2.6 ###27202787@qq.com import sys,os,time,shutil, filecmp print ("start backup database ......."); oscommand = 'ver' backup_sql_path="/data2/backup/sql2" backup_war_path="/data2/backup/war2" logs_path='/tmp/log.log' current_date=time.strftime('%Y-%m-%d') current_datetime=time.strftime('%Y%m%d%H%M%S') db_name="laneige" password="xxxx2011" db_host="localhost" mysql_user="root" def writeLogs(filename,contents): f=open(filename,'a+'); f.write(contents); f.close(); if not os.path.exists(backup_sql_path): Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' if(os.mkdir(backup_sql_path))==None: Msg+='**succeed to create dir:'+backup_sql_path+'\n' writeLogs(logs_path,Msg) else: Msg+='!!create backup dir:'+backup_sql_path+'failed,check whether dir can write! nn' writeLogs(logs_path,Msg) if not os.path.exists(backup_war_path): Msg='-'*30+time.strftime('%Y-%m-%d,%H:%M:%S')+'-'*30+'\n' if(os.mkdir(backup_war_path))==None: Msg+='**succeed to create dir:'+backup_war_path+'\n' writeLogs(logs_path,Msg) else: Msg+='!!create backup dir:'+backup_war_path+'failed,check whether dir can write! nn' writeLogs(logs_path,Msg) sys.exit() param=raw_input("Please input deployment version(stress or staging or prod) :"); password="no password"; backup_name=backup_sql_path+"/" + current_datetime+".sql" if (param == "stress" or param == "" or param == "local"): password="xxxx2012" bak_cmd='mysqldump -h%s -u%s -p%s %s > %s ' %(db_host,mysql_user,password,db_name,backup_name); os.system(bak_cmd); os.system("pwd"); os.system("tar -zcvf %s.tar.gz %s --remove-files" %(backup_name,backup_name)); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Bacakup database finishded.\n"); elif (param == "staging" ): password="xxxx2011" elif (param == "prod"): password="xxxx2014" time.sleep(4); print "#"*60; print ("backup application war and update svn programme start.........."); resource="/data/src/laneige" tomcat_home="/opt/app/tomcat" webapps="/opt/app/tomcat/webapps" backup_cmd="cp %s/laneige_Web.war %s/laneige_Web_%s.war" %(webapps, backup_war_path,current_datetime); os.system(backup_cmd); time.sleep(6); svn_cmd="cd %s;svn up" %(resource); os.system(svn_cmd); time.sleep(3); print ("backup application war and update svn programme end ........"); print "#"*60; print "compile class file.............begin............." if (param == "stress" or param == "" or param == "local"): mvn_cmd="cd %s;mvn -U clean package -PSTRESS" %(resource); os.system(mvn_cmd); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Complie all java files to generate war.\n"); elif (param == "staging" ): mvn_cmd="cd %s;mvn -U clean package -PSTAGING" %(resource); os.system(mvn_cmd); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Complie all java files to generate war.\n"); elif (param == "prod"): mvn_cmd="cd %s;mvn -U clean package -PPROD" %(resource); os.system(mvn_cmd); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Complie all java files to generate war.\n"); time.sleep(2); print "compile class file.............end............." print "="*60; source_war="/data/src/laneige/laneige-web/target/laneige_Web.war" target_path="/data/src/temp1"; print "start application server.............begin............." if (param == "stress" or param == "" or param == "local"): kill_cmd="kill `ps -ef | grep tomcat | awk '{print $2,$8}' | grep 'java$'| awk '{print $1}'`"; os.system(kill_cmd); time.sleep(3); os.system("rm -rf %s/laneige_Web*" %(webapps)); time.sleep(3); os.system("cp -rf %s %s/" %(source_war,webapps)); time.sleep(3); os.system("%s/bin/startup.sh " %(tomcat_home)); time.sleep(3); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Deploy finished.\n"); elif (param == "staging" ): scp_cmd="scp -P 1122 %s root@192.168.10.125:%s" %(source_war, target_path); os.system(scp_cmd); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Copy to staging finished.\n"); elif (param == "prod"): scp_cmd="scp -P 60 %s root@192.168.11.185:%s" %(source_war,target_path); os.system(scp_cmd); Msg='-'*30+time.strftime('%Y-%m-%d %H:%M:%S')+'-'*30+'\n' writeLogs(logs_path,Msg+"Copy to prod finished.\n"); time.sleep(2); print "start application server.............end............."
注意:python脚本需要注意对齐格式,免得执行过程报错。
相关推荐
一、让Python随Linux开机自动运行 准备好要自启的脚本auto.py 用root权限编辑以下文件 sudo vim /ect/rc.local 在exit 0上面编辑启动脚本的命令 /usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto....
目前Linux下有一些使用Python语言编写的Linux系统监控工具比如inotify-sync(文件系统安全监控软件)、glances(资源监控工具)在实际工作中,Linux系统管理员可以根据自己使用的服务器的具体情况编写一下简单实用的...
自动例化verilog模块的python脚本
统信桌面操作系统PXE部署Python脚本 统信桌面操作系统PXE部署Python脚本 统信桌面操作系统PXE部署Python脚本
通过Python脚本,自动巡检网络设备,并将巡检结果输出到附件,邮件发送。使用的是Python的telnetlib模块
容易上手,广泛使用,拥有海量“开箱即用”的模块(它是一组包含Python语句的外部文件),Python理所当然地成为了美国计算机专业大学生在一年级时所上的程序设计课所用语言的不二之选。在这个由两篇文章构成的系列中,
Linux运维自动化运维脚本.zip,自动化运维脚本 介绍 Linux系统运维中各种一键脚本 使用教程 openssh一键升级脚本,支持centos6、centos7、centos8。 Linux运维自动化运维脚本.zip,自动化运维脚本 介绍 Linux系统...
py交换机自动备份脚本。支持华为、思科、华三、 Aruba交换机。将设备ip、账号、密码、写入配置文件运行即可。多台设备一键备份。记录日志,自定义备份位置。需要先安装调用的模块,怎么安装自行百度。
一、Python开机自动运行 假如Python自启动脚本为 auto.py 。那么用root权限编辑以下文件: sudo vim /etc/rc.local 如果没有 rc.local 请看 这篇文章 在exit 0上面编辑启动脚本的命令 /usr/bin/python3 /home/...
c#远程调用linux服务器的Python脚本(csdn)————程序
ceph一键部署python脚本
本文档主要讲述的是Python脚本使用详解;
默认linux 系统上就有python2 存在,我们不要去删除他,否则yum 和防火墙等无法使用,可以通过如下命令查看 python -V 一、安装依赖包 yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel...
为了监控 NAS主机的运行状态, 以流量和CPU温度为主.应该可运行所有linux环境. 参考网上资料整理代码 , 供参考. 监控 CPU 温度, 内存使用量MB, CPU单核使用率(此项不太准), 网络流入差量KB , 流出差量KB, 运行时间...
从win服务器上传文件到linux服务器的Python脚本
用python脚本过滤文本中的关键字并显示,例子
linux python 2.7.18 环境部署项目发布运行
可自动填写腾讯问卷的Python脚本,基于selenium实现,同时考虑了防止被封ip,进行了动态调整秒数,用户可根据自己的对选项的概率进行调整,再也不用发红包请别人填问卷啦!!
Python 脚本可自动执行 Simulia Abaqus 日常使用中的枯燥工作,从打开 GUI 到控制参数分析。 启动 Abaqus CAE open_abaqus.bat 启动 Abaqus CAE 并调用 open_abaqus.py 脚本。您可以为 bat 文件创建一个链接并将其...