Python 提取文件,备份,压缩,并上传到远程FTP后,清空指定时间范围外的文件

backup-files.py
#! /usr/bin/env python     
# -*- coding: utf-8 -*-
# author:jinqinghua@gmail.com
# version:build 2014-02-22

import os
import platform
import time
import tarfile
import zipfile
from ftplib import FTP

os_name         = platform.system()
today           = time.strftime('%Y%m%d')
now             = time.strftime('%Y%m%d_%H%M%S')

################################################################################
# YOU CAN EDIT THE FOLLOWING VARIABLES
#-------------------------------------------------------------------------------
app_name	= 'sxhbc'

bak_dir         = r'E:\backup'
bak_src		= [r'D:\Java\workspace\project-svn\sxhbc-parent\sxhbc-web\src\main\webapp']

keep_days       = '90'

# ftp settings  
ftp_server      = '*.*.*.*'  
ftp_port        = '21'  
ftp_user        = 'user' 
ftp_password    = 'passwd'
#-------------------------------------------------------------------------------
bak_dest        = '%s/%s' %(bak_dir, app_name)
bak_web         = '%s/webapps' %(bak_dest)

prefix          = app_name
log_file        = '%s_%s.log' %(prefix, now)
log_dest        = '%s/%s' %(bak_dest, log_file)
zip_file        = '%s_%s.tar.gz' %(prefix, now)
zip_dest        = '%s/%s' %(bak_dest, zip_file)
###############################################################################
def execute_cmd(cmd):
    if 0 != os.system(cmd):
        print '!!!!!!!!!!ERROR, Please check your command --> %s' %(cmd)

def backup_files(src, dest):  
    if not os.path.exists(dest):
        os.makedirs(dest)
    cmd_copy = 'xcopy "%s" "%s" /I /Y /S /D >> %s' %(src, dest, log_dest) 
    if (os_name == 'Linux'):
        cmd_copy = 'cp -ruv %s, %s >> %s' %(src, dest, log_dest)
    print(cmd_copy)
    execute_cmd(cmd_copy)
        
def tar_files(src, dest):
    print('taring files to %s...' %(dest))  
    tar = tarfile.open(dest, 'w:gz')  
    tar.add(src, os.path.basename(src)) 
    tar.close() 

def ftp_stor_files(file):  
    cmd_stor = "STOR %s" %(os.path.split(file)[1])  
    print(cmd_stor)  
    ftp = FTP(ftp_server, ftp_user, ftp_password)  
    print(ftp.getwelcome())
    ftp.storbinary(cmd_stor, open(file, "rb"), 1024)  
    ftp.close()

def clear_files():
    #os.remove(dump_dest)
    
    cmd_clear = 'forfiles /s /p %s /m *.tar.gz /d -%s /c "cmd /c del @file"' %(bak_dest.replace('/', '\\'), keep_days)
    if (os_name == 'Linux'):
        cmd_clear = '/usr/bin/find %s -mtime +%s -name "*.tar.gz" -exec /usr/bin/rm -rf {} %s' %(bak_dest, keep_days, '''\;''')
    print(cmd_clear)
    execute_cmd(cmd_clear)

if __name__ == '__main__':
    backup_files(' '.join(bak_src), bak_web)
    #tar_files(bak_web, zip_dest)
    #ftp_stor_files(zip_dest)
    clear_files()
    print('done, pyhon is great!') 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值