python2.7执行shell脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-"""reated on 2017-11-12@author: lichanling"""import shleximport datetimeimport subprocessimport timeimport calendar as cal"""执行一个SHELL命令封
·
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
reated on 2017-11-12
@author: lichanling
"""
import shlex
import datetime
import subprocess
import time
import calendar as cal
"""
执行一个SHELL命令
封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr
参数:
cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd
timeout: 超时时间,秒,支持小数,精度0.1秒
shell: 是否通过shell运行
Returns: return_code
Raises: Exception: 执行超时
"""
def execute_command(cmdstring, cwd=None, timeout=None, shell=False):
if shell:
cmdstring_list = cmdstring
else:
cmdstring_list = shlex.split(cmdstring)
if timeout:
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
#没有指定标准输出和错误输出的管道,因此会打印到屏幕上;
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,shell=shell,bufsize=4096)
#subprocess.poll()方法:检查子进程是否结束了,如果结束了,设定并返回码,放在subprocess.returncode变量中
while sub.poll() is None:
time.sleep(0.1)
if timeout:
if end_time <= datetime.datetime.now():
raise Exception("Timeout:%s"%cmdstring)
return str(sub.returncode)
"""
执行备份脚本
"""
def regul_backup():
#导出指定的数据库
database="apus"
#导出表名称
import_tables = ""
#指定备份输出文件名前缀,路径+文件名(会补上时间戳+.sql的后缀)
back_out_file = "/路径/文件名"
#日期格式化
FORMAT = "%d-%02d-%02d"
# 年份
year = 2017
#获取上个月的月份
last_month = time.localtime()[1]-1 or 12
#定义开始月份
start_month = 0;
#开始日
start_day = "" ;
#开始时间
start_time = "00:00:00" ;
#定义结束月份
end_month = 0;
#结束日
end_day = "";
#结束时间
end_time = "23:59:59";
#未设置开始月份,则使用last_month定义的月份
if start_month == 0:
start_month = last_month
#未设置结束月份,则使用last_month定义的月份
if end_month == 0:
end_month = last_month
print "year:%s,start_month:%s,end_month:%s" %(year,start_month,end_month)
# start_d = cal.monthrange(year, start_month)
end_d = cal.monthrange(year, end_month)
if start_day == "":
start_day = 1
if end_day == "":
end_day = end_d[1]
# 获取开始日期
start_date = FORMAT % (year,start_month, start_day)
# 获取结束日期
end_date = FORMAT %(year,end_month,end_day)
#拼接备份脚本
back_shell ="mysqldump -ubkuser -pbk2017 -S /data/mysql/3308/tmp/mysql.sock -t --databases %(database)s --tables $(import_tables)s where time between unix_timestamp('%(start_date)s %(start_time)s') AND unix_timestamp('%(end_date)s %(end_time)s') > %(back_out_file)s%(start_date)s-%(end_date)s.sql" % {'database':database,'import_tables':import_tables,'start_date':start_date,'start_time':start_time,'end_date':end_date,'end_time':end_time,'back_out_file':back_out_file}
print back_shell
#执行备份脚本并将输出打印出来
#print execute_command(back_shell)
#上传到aws
upload_shell = "aws s3 cp %(back_out_file)s%(start_date)s-%(end_date)s.sql s3://apus-bigdata-spark/filterregister/" % {'back_out_file':back_out_file,'start_date':start_date,'end_date':end_date}
print upload_shell
#print execute_command(upload_shell)
if __name__=="__main__":
# 执行脚本示例
# print execute_command("ls")
# 执行备份
regul_backup()
更多推荐
已为社区贡献2条内容
所有评论(0)