玩蛇网提供最新Python编程技术信息以及Python资源下载!

用scp备份openstack的instance镜像方法

python 培训

以下python代码是完成用scp备份openstack的instance镜像方法。需要使用到python MySQLdb、os及pexpect模块方法。

#!/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)

#www.iplaypython.com
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()

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2346.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-20 09:53 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部