Python Backup Files

近来书写 Python 脚本进行替换以前的 shell 脚本,发现 Python 优于 shell 的最直观的一点Python 结构明了,可读性高(本人认为)在此做一些记录

本次记录利用 Python Script
1,SVN 备份 ,封装 paramiko的 sftp 和 ssh connection 和 ssh_exec_command

2,删除过期文件

1,SVN 备份

  准备:Python Paramiko Install

    方法1: 直接使用 pip 进行安装

        pip install paramiko

    方法2:下载源码进行安装

        paramiko-1.15.2.tar.gz 主要文件

        ecdsa-0.13.tar.gz 依赖文件

        pycrypto-2.6.1.tar.gz 依赖文件

       

1.install ecdsa
    tar xzf ecdsa-0.13.tar.gz && cd ecdsa-0.13 && python setup.py install
2.install pycrypto
    tar xzf pycrypto-2.6.1.tar.gz && cd pycrypto-2.6.1 && python setup.py install
3.install paramiko
    tar xzf paramiko-1.15.2.tar.gz && cd paramiko-1.15.2 && python setup.py install
View Code

  Python Script

#!/usr/bin/env python
# _*_coding:utf-8_*_
#  author:  'lonny'
# dateTime:  '15/11/16'
#   motto:  'Good memory as bad written'

import datetime
import os
import tarfile
import subprocess


# usvn 备份--------------------------------------------------------------
class Usvn_Backend(object):
    # ------------------------------------------------------------------
    def __init__(self):
        self.date_time = datetime.datetime.now().strftime('%Y-%m-%d-%H')
        self.Root_Directory = "/software"
        self.back_directory = "usvn"
        self.Remote_Send_Dir = "/install/backup/usvnback"

    # 打包文件------------------------------------------------------------
    def Package(self):
        global tarfile_name
        print "33[32mWaitIng Packaging..........33[0m"
        os.chdir(self.Root_Directory)
        tarfile_name = "%s-%s.tar.gz" % (self.back_directory, self.date_time)
        tar = tarfile.open(tarfile_name, "w:gz")
        tar.add(self.back_directory)
        tar.close()
        if os.path.exists(tarfile_name):
            print "33[32m..........Packaging Is SuccessFul!!!33[32m"
        else:
            print "33[32m..........Packaging Is Failed!!!33[0m"


# 执行远程命令传送文件---------------------------------------------------------
class SSHConnection(object):
    """"""

    # ----------------------------------------------------------------------
    def __init__(self, host, username, password, port=22):
        """Initialize and setup connection"""
        self.sftp = None
        self.sftp_open = False

        # open SSH Transport stream
        self.transport = paramiko.Transport((host, port))

        self.transport.connect(username=username, password=password)

        self.session = self.transport.open_channel(kind='session')

    # ----------------------------------------------------------------------
    def _openSFTPConnection(self):
        """
        Opens an SFTP connection if not already open
        """
        if not self.sftp_open:
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
            self.sftp_open = True

    # ----------------------------------------------------------------------
    #下载文件时需要指定两端文件名
    def get(self, remote_path, local_path=None):
        """
        Copies a file from the remote host to the local host.
        """
        self._openSFTPConnection()
        self.sftp.get(remote_path, local_path)

    # ----------------------------------------------------------------------
    #传送文件是需要两端都要指定文件名称
    def put(self, local_path, remote_path=None):
        """
        Copies a file from the local host to the remote host
        """
        self._openSFTPConnection()
        self.sftp.put(local_path, remote_path)

    # ----------------------------------------------------------------------
    def run(self, command, nbytes=4096):
        # Run Command out|err
        stdout_data = []
        stderr_data = []
        self.session.exec_command(command)
        while True:
            if self.session.recv_ready():
                stdout_data.append(self.session.recv(nbytes))
            if self.session.recv_stderr_ready():
                stderr_data.append(self.session.recv_stderr(nbytes))
            if self.session.exit_status_ready():
                break
        print "33[31m*********************33[0m"
        print '33[32mExit status is: 33[0m', self.session.recv_exit_status()
        print "33[31m*********************33[0m"
        print ''.join(stdout_data)
        print ''.join(stderr_data)

    def close(self):
        """
        Close SFTP connection and ssh connection
        """
        if self.sftp_open:
            self.sftp.close()
            self.sftp_open = False
        self.transport.close()
        self.session.close()


if __name__ == '__main__':
    try:
        try:
            import paramiko
        except ImportError:
            print "33[32mInstalling Paramiko.........33[0m"
            install_paramiko = "pip install paramiko"
            subprocess.call(install_paramiko, shell=True)
        # Run Usvn Unpack--------------------------------------------------------------
        unpack = Usvn_Backend()
        unpack.Package()
        # Put UsvnBack Files To Remote Server
        Send_Files = SSHConnection(ipaddress, user, password)
        #Set local_path Names,remote_path Names
        local_path_files = "%s/%s" % (unpack.Root_Directory, tarfile_name)
        remote_path_files = "%s/%s" % (unpack.Remote_Send_Dir, tarfile_name)
        Send_Files.put(local_path_files, remote_path_files)
        #remove tarfiles
        os.chdir(unpack.Root_Directory)
        os.remove(tarfile_name)
        #remove end!!!!
        Send_Files.close()
    except KeyboardInterrupt:
        print "Contorl+C+Z"
View Code

2,删除过期文件

#!/usr/bin/env python
# _*_coding:utf-8_*_
#  author:  'lonny'
# dateTime:  '15/12/15'
#   motto:  'Good memory as bad written'

import os
import sys
import time


#删除文件-----------------------------------------------------------------
def remove(path):
    """
    Remove the file or directory
    """
    if os.path.isdir(path):
        try:
            os.rmdir(path)
        except OSError:
            print "Unable to remove folder: %s" % path
    else:
        try:
            if os.path.exists(path):
                os.remove(path)
        except OSError:
            print "Unable to remove file: %s" % path


# 遍历输入的文件夹,查询出number_of_days天前的文件,进行删除---------------------
def cleanup(number_of_days, path):
    """
    Removes files from the passed in path that are older than or equal
    to the number_of_days
    """
    time_in_secs = time.time() - (number_of_days * 24 * 60 * 60)
    """
    计算出当前时间与number_of_days天前的毫秒差
    """
    for root, dirs, files in os.walk(path, topdown=False):
        for file_ in files:
            full_path = os.path.join(root, file_)
            stat = os.stat(full_path)

            if stat.st_mtime <= time_in_secs:
                remove(full_path)

        if not os.listdir(root):
            remove(root)


# ----------------------------------------------------------------------
if __name__ == "__main__":
    #sys.argv[1]天数 sys.argv[2]要遍历的目录
    days, path = int(sys.argv[1]), sys.argv[2]
    cleanup(days, path)
View Code
原文地址:https://www.cnblogs.com/edwardlogs/p/5048716.html