• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python scp备份文件

python 水墨上仙 2965次浏览

python scp备份文件


#!/usr/bin/env python

import MySQLdb
import os
import pexpect

def conn_to_mysql():
    """Make a Connection to the mysql server"""
    db_host = 'localhost'
    db_user = 'root'
    db_pass = 'william'
    db_name = 'backup'
    conn = MySQLdb.connect(host=db_host, user=db_user,
                           passwd=db_pass,db=db_name)
    return conn

def check_path(path):
    """Check if a path exist, and create if it's not"""
    if not os.path.exists(path):
        mkdir_cmd = "mkdir -p " + path
        pexpect.run(mkdir_cmd)


def do_rotate(file, rotate):
    """Rotate files for $rotate times"""
    list = []
    for i in range(int(rotate)):
        list.insert(0, i)

    for i in list:
        if i == 0:
            src = backup_path + file
        else:
            src = backup_path + file + "." + str(i)

        dst = backup_path + file + "." + str(i+1)

        if os.path.exists(src):
            move_cmd = "mv " + src + " " + dst
            pexpect.run(move_cmd)

def get_new(instance_id, compute_node, file):
    """Get the new backup $file from $host"""
    src = "william@" + compute_node + ":/var/lib/openstack/" + \
           instance_id + "/" + file
    dst = "/var/lib/openstack-backup/" + instance_id + "/"
    copy_cmd = "scp " + src + " " + dst

    ssh_newkey = "Are you sure you want to continue connecting"
    p = pexpect.spawn(copy_cmd)
    i = p.expect([ssh_newkey, 'password:', pexpect.EOF])
    if i == 0:
        print "I say yes"
        p.sendline('yes')
        i = p.expect([ssh_newkey, 'password:', pexpect.EOF])
    if i == 1:
#        print "I give password",
        p.sendline("william")
        p.expect(pexpect.EOF)
    elif i == 2:
        print "I either got key or connection timeout"
        pass
#    print p.before




def do_backup(instance_id, compute_node, rotate, files):
    for file in files:
        do_rotate(file, rotate)
        get_new(instance_id, compute_node, file)

if __name__ == "__main__":
    db = conn_to_mysql()
    cursor = db.cursor()
    cursor.execute('select * from schedule')

    for result in cursor.fetchall():
        instance_id = result[0]
        count = int(result[5])
        interval = int(result[2])
        compute_node = result[1]
        rotate = result[3]
        files = result[4].split(",")

        backup_path = "/var/lib/openstack-backup/" + instance_id + "/"
        check_path(backup_path)

        if count == 0:
            do_backup(instance_id, compute_node, rotate, files)
            count += 1
        elif count == interval:
            count = 0
        else:
            count += 1

        # Update database
        update_cmd = "update backup.schedule set count='" + str(count) + \
                     "'where instance_id='" + str(instance_id) + "'"
        cursor.execute(update_cmd)

    db.close()


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python scp备份文件
喜欢 (0)
加载中……