python2.7 shellスクリプトの実行

6714 ワード

#!/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()