python把文件从一个目录复制到另外一个目录,并且备份

时间:2021-05-26 20:59:36
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,sys,md5,datetime,shutil,time,zipfile,chardet

# coding=utf-8
#pub_path='/home/gdmt/mm'
pub_dir=''
src_dir='/home/gdmt/mm/GDSMT-8093'
bak_dir='/home/gdmt/mm/bak'
bbdir='/home/gdmt/mm/bak/gdsmt_bak'
def pub(pub_file):
#遍历filepath下所有文件,包括子目录
  files = os.listdir(pub_file)
  for fi in files:
    fi_d = os.path.join(pub_file,fi)
    if os.path.isdir(fi_d):
      is_exists=os.path.exists(fi_d.replace(pub_dir,src_dir))
      if not is_exists:
        shutil.copytree(fi_d,fi_d.replace(pub_dir,src_dir))
        #print chardet.detect(fi_d)
        #print fi_d.decode("gbk").encode("utf-8")
        print 'publish dir:'+fi_d.replace(pub_dir,src_dir)
      is_exists=os.path.exists(fi_d.replace(pub_dir,bak_dir))
      if not is_exists:         
        os.mkdir(fi_d.replace(pub_dir,bak_dir))
      pub(fi_d)                  
    else:
      #back
      #print 'pub_dir:'+pub_dir
      #print 'src_dir:'+src_dir
      #print 'bak_dir:'+bak_dir
      #print 'fi_d:'+fi_d
      #print 'publish file:'+fi_d.replace(pub_dir,src_dir)
      is_exists=os.path.exists(fi_d.replace(pub_dir,src_dir))
      #is_exists2=os.path.exists(fi_d.replace(pub_dir,bak_dir))
      #print is_exists
      #print fi_d.replace(pub_dir,bak_dir)
      if is_exists:
        print 'bak file:'+fi_d.replace(pub_dir,bak_dir)
        shutil.move(fi_d.replace(pub_dir,src_dir),fi_d.replace(pub_dir,bak_dir))
      #publish
      shutil.copyfile(fi_d,fi_d.replace(pub_dir,src_dir))
      print 'publish file:'+fi_d.replace(pub_dir,src_dir)

def un_zip(file_name,unzip_dir):  
    """unzip zip file"""  
    zip_file = zipfile.ZipFile(file_name)  
    if os.path.isdir(unzip_dir):  
        pass  
    else:  
        os.mkdir(unzip_dir)  
    for names in zip_file.namelist():  
       #print chardet.detect(names)
       #ss=names.decode('ascii')
       #print ss
       #ss=ss.encode('utf-8')
       #print ss
       zip_file.extract(names,unzip_dir)  
    zip_file.close()

def cov_unicode(s):
    return s.decode('ascii').encode('utf-8')

file_name=sys.argv[1]
#zip_file='/home/gdmt/mm/pub/'+sys.argv[1]+'.zip'
pub_file='/home/gdmt/mm/pub/'+sys.argv[1]
pub_dir='/home/gdmt/mm/pub'
now = datetime.datetime.now()
t_str = now.strftime('%Y%m%d%H%M%S')
bak_dir=bbdir+'_'+t_str
shutil.copytree(bbdir,bak_dir)
#is_exists=os.path.exists(pub_file)
print ''
#if is_exists:
#  print 'rm '+pub_file
#  os.popen('rm -rf '+pub_file)
#print 'unzip:'+zip_file
#os.unzip(pubfile)
#output=os.popen('unzip '+zip_file)
#un_zip(zip_file,pub_dir)
#print 'waiting unzip ...' 
#time.sleep(3)
#os.popen('mv '+file_name +' '+pub_dir)
#zip_dir='/home/gdmt/mm/py/'+file_name
#print ''
#print 'zip_dir:'+zip_dir
#print 'pub_dir:'+pub_file
#print ''

#shutil.move(zip_dir,pub_dir)
#time.sleep(1)
#os.popen('mv '+zip_dir+' '+pub_dir)
#os.popen('rm -rf /home/gdmt/mm/py/'+file_name)
# start publist ...
pub_dir=pub_file
pub(pub_file)

 配合自动发布的shell脚本:需要先做ssh信任

#!/bin/bash  
pub_path = $1

ssh gdmt@1.1.1.19   << remotessh  
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "===================138.19 end================================="

sleep 3

ssh gdmt@1.1.1.20   << remotessh  
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "===================138.20 end================================="

sleep 3

ssh gdmt@1.1.1.21   << remotessh  
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "===================138.21 end================================="

sleep 3

ssh gdmt@1.1.1.22   << remotessh  
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "====================138.22 end================================"

sleep 3

ssh gdmt@1.1.1.31   << remotessh  
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "===================138.31 end================================="

sleep 3

ssh gdmt@1.1.1.32   << remotessh  
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "==================138.32 end=================================="

sleep 3

ssh gdmt@1.1.1.33   << remotessh  
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit  
remotessh
echo "==================138.33 end=================================="

sleep 3

#ssh gdmt@1.1.1.37   << remotessh  
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
#exit  
#remotessh

echo "===================138.37 end=================================="
#ssh gdmt@1.1.1.33   << remotessh  

#exit
#remotessh