利用AWS boto实现EC2 存储卷的自动快照
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用AWS boto实现EC2 存储卷的自动快照相关的知识,希望对你有一定的参考价值。
boto是AWS的Python SDK,可以利用boto自动生成ec2的存储卷快照,实现ec2数据的备份
1.安装boot
pip install boto ,如果没有安装pip,需要提前安装
2.配置boto配置文件
~/.aws/credentials #设置aws认证凭据
[default] aws_access_key_id = AAAAAAAAAAAAAAA aws_secret_access_key = TNAAAAXXXXXXXXXXXXXXXXXXXXXX
~/.aws/config #设置默认区域
[default] region = us-east-1
3.编辑脚本
#Date: #Author: #Usage: python snap.py volume_id number_of_snapshots_to_keep import boto import sys import pdb from datetime import datetime conn = boto.connect_ec2() conn_us = conn if len(sys.argv) < 2: print "Usage: snap.py vol-id days-to-keep" exit() vol_id = sys.argv[1] keep = int(sys.argv[2]) volumes = conn_us.get_all_volumes([vol_id]) volume = volumes[0] description = ‘hostname‘ + ‘-‘ + datetime.today().isoformat(‘ ‘)#镜像描述 volume.create_snapshot(description) snapshots = volume.snapshots() def date_compare(snap1, snap2): if snap1.start_time < snap2.start_time: return -1 elif snap1.start_time == snap2.start_time: return 0 return 1 snapshots.sort(date_compare) delta = len(snapshots) - keep for i in range(delta): print ‘Deleting snapshot ‘ + snapshots[i].description try: snapshots[i].delete() except Exception, e: pass
4.加入crontab
volum-id可以在控制台中找到
#每天凌晨自动生成磁盘快照
00 00 * * * python /opt/scripts/snap.py vol-00c69ee40b3ad1bf4 10 #Disk snap
以上是关于利用AWS boto实现EC2 存储卷的自动快照的主要内容,如果未能解决你的问题,请参考以下文章
Lambda,AWS和Python的自动化管理操作 - 自动创建快照,删除快照