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

  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 "\033[32mWaitIng Packaging..........\033[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 "\033[32m..........Packaging Is SuccessFul!!!\033[32m"
        else:
            print "\033[32m..........Packaging Is Failed!!!\033[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 "\033[31m*********************\033[0m"
        print ‘\033[32mExit status is: \033[0m‘, self.session.recv_exit_status()
        print "\033[31m*********************\033[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 "\033[32mInstalling Paramiko.........\033[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"

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)

时间: 2024-12-20 21:23:11

Python Backup Files的相关文章

The log or differential backup cannot be restored because no files are ready to rollforward.

The log or differential backup cannot be restored because no files are ready to rollforward. If you have found this page, it is likely that you encountered the following error when you tried to restore a differential backup using Microsoft SQL Server

Python简单脚本之一 (备份压缩文件)

#功能:#1.备份系统重要文件以及mongodb文件.#2./etc/ /usr/local/mongodb/等#3.备份路径:/data/backup/20161103/system_backup.tar.gz#4.备份完成打印信息 首先我要建立一备份的目录  ,我需要先判断有没有这个目录 import time import os d_dir='/usr/local/py/backup/' #备份目标目录 d_files='system_back.tar.gz' #命名文件 s_dir =[

vim+python开发环境打造

1.一个现成的一键安装脚本 curl -O https://raw.githubusercontent.com/vince67/v7_config/master/vim.sh 2.bash vim.sh 3.我的.vimrc文件,亲测可用 let vim_plug_just_installed = 0let vim_plug_path = expand('~/.vim/autoload/plug.vim')if !filereadable(vim_plug_path) echo "Install

python备份脚本

本文参考简明python后修改 #!/usr/bin/python import time import os # 1. The files and directories to be backed up are specified in a list. source = ['/root', '/tmp'] # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that #

将Python脚本文件包装成可执行文件

将Python脚本文件包装成可执行文件,其目的有二: 一则: 不需要依赖Python编译器就可以运行软件 二则: 不想让自己的源码公布出去 常用的工具有: py2exe.cx_freeze等 [工具:py2exe] 安装py2exe 安装该工具很简单: 只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装. 安装后,执行import py2exe,不报错则表示安装成功! >>> import py2exe >>>

Python著名的lib和开发框架(均为转载)

第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Admin Panels Algorithms and Design Patterns Anti-spam Asset Management A

TFS Express backup and restore

 When we setup source control server, we should always make a backup and restore plan for it. This article is to describe how to backup and restore a TFS Express instance from one server to another server. This blog is an English version, for Chine

python系统模块

Python中大多数系统接口都集中在两个模块:sys和os.这么说有点过于简单化 还有一些其他的表转模块也属于这个领域他们包括: glob 用于文件名的扩展 socket 用于网络连接和进程间通信(IPC) threading,_threading,queue 用于运行和同步话并发线程 time,timeit 用于获取系统时间相关细节. subprocess,multiprocessing 用于启动和控制并行进程 signal,select,shutil,tempfile 用于多种系统相关任务

Python for everyone chapter 1

Chapter 1 10 试题 1. When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you? What is the next machine language instruction to run? What is your favourite color? What Python statem