Created
January 29, 2015 06:38
-
-
Save fakechris/dadf838416205c81d4cb to your computer and use it in GitHub Desktop.
Backup your qingcloud resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding: utf8 | |
import gflags | |
import sys | |
import subprocess | |
import json | |
from operator import itemgetter, attrgetter, methodcaller | |
gflags.DEFINE_integer('limit', 2, 'how many full backup point') | |
gflags.DEFINE_boolean('backup', True, 'is backup') | |
gflags.DEFINE_boolean('cleanup', False, 'is auto cleaup') | |
gflags.DEFINE_boolean('list', False, 'is list') | |
gflags.DEFINE_enum('resource', '', [], "which resource to be backup") | |
FLAGS = gflags.FLAGS | |
RESOURCE_MAP ={ | |
'RESOURCE_NAME':'PUT_YOUR_QINGCLOUD_ID_HERE', | |
} | |
def gflags_init(): | |
try: | |
argv = FLAGS(sys.argv)[1:] # parse flags | |
except gflags.FlagsError, e: | |
print '%s\\nUsage: %s ARGS\\n%s' % (e, sys.argv[0], FLAGS) | |
sys.exit(1) | |
return argv | |
def run(cmd): | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
return p.stdout.read() | |
def run_cmd(cmd): | |
print "running ", cmd | |
r = run(cmd) | |
try: | |
return json.loads(r) | |
except: | |
print "failed parse", r | |
return {} | |
def backup_one(resource_name, rid): | |
cmd = ['qingcloud', 'iaas', 'create-snapshots', '-r', rid, '-N', resource_name] | |
backup_result = run_cmd(cmd) | |
if backup_result['ret_code'] == 1400: | |
print "try perform fullbackup" | |
# try perform full backup | |
cmd = ['qingcloud', 'iaas', 'create-snapshots', '-r', rid, '-F', '1', '-N', resource_name] | |
backup_result = run_cmd(cmd) | |
if backup_result['ret_code'] == 1400 and backup_result['message'].find("up to [2]")>0: | |
cleanup(resource_name, 1) # delete oldest backup and try again | |
cmd = ['qingcloud', 'iaas', 'create-snapshots', '-r', rid, '-F', '1', '-N', resource_name] | |
backup_result = run_cmd(cmd) | |
if backup_result['ret_code'] == 0: | |
return True | |
else: | |
print "backup failed", backup_result | |
return False | |
def backup(resource_name): | |
rid = RESOURCE_MAP[resource_name] | |
return backup_one(resource_name, rid) | |
def backup_list(rid): | |
cmd = ['qingcloud', 'iaas', 'describe-snapshots', '-r', rid, '-L', '1000', '-s', 'available', '-t', '1'] | |
print cmd | |
result = json.loads(run(cmd)) | |
if result['ret_code'] == 0: # OK | |
return result['snapshot_set'] | |
else: | |
return [] | |
def delete_one(item): | |
cmd = ['qingcloud', 'iaas', 'delete-snapshots', '-s', item['snapshot_id']] | |
print cmd | |
result = json.loads(run(cmd)) | |
if result['ret_code'] != 0: | |
print result | |
def cleanup(resource_name, limit): | |
rid = RESOURCE_MAP[resource_name] | |
results = backup_list(rid) | |
order_results = sorted(results, key=itemgetter('snapshot_time')) | |
print "total backups for", rid, len(order_results) | |
if len(order_results) > limit: | |
to_del = order_results[:len(order_results)-limit] | |
for item in to_del: | |
if FLAGS.cleanup: | |
delete_one(item) | |
def main(): | |
if FLAGS.backup: | |
backup(FLAGS.resource) | |
if FLAGS.cleanup or FLAGS.list: | |
cleanup(FLAGS.resource, FLAGS.limit) | |
if __name__ == '__main__': | |
gflags_init() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment