-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenstack_cinder_backup.py
96 lines (75 loc) · 3.07 KB
/
openstack_cinder_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
#
# Backup all cinder volumes which names start with backupme
#
# Copyright 2014 ETH Zurich, ISGINF, Bastian Ballmann
# Email: [email protected]
# Web: http://www.isg.inf.ethz.ch
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License.
# If not, see <http://www.gnu.org/licenses/>.
# TODO: needs to be able to delete images on disk that have been deleted in cinder
#
# Loading modules
#
import os
import sys
import keystoneclient.v2_0.client as keystone_client
from cinderclient.exceptions import ClientException, BadRequest, Unauthorized
from openstack_lib import get_keystone_client, get_cinder_client, get_backup_base_path, ensure_dir_exists
from openstack_lib import backup_cinder_volume, wait_for_glance_upload_to_finish, attach_volume
#from openstack_lib import nova_glance_check_upload, cinder_glance_check_upload
import openstack_lib
#
# Subroutines
#
def backup_tenant(tenant):
cinder = get_cinder_client(tenant)
volumes = cinder.volumes.list()
if len(volumes) == 0:
return (tenant.id, None)
# Check that admin user is in the tenant we want to backup
# otherwise add him
if not filter(lambda x: x.username == os.environ['OS_USERNAME'], tenant.list_users()):
keystone = get_keystone_client()
tenant.add_user(keystone.users.find(name = os.environ['OS_USERNAME']),
keystone.roles.find(name = 'admin'))
backups = {}
attached = {}
ensure_dir_exists(get_backup_base_path(tenant.id))
ensure_dir_exists(os.path.join(get_backup_base_path(tenant.id), "cinder"))
for volume in volumes:
if volume.display_name.startswith("backupme"):
(backup_id, backup_name) = backup_cinder_volume(tenant, volume)
if backup_id:
backups[backup_id] = (tenant.id, backup_name)
if len(volume.attachments) > 0:
attached[backup_id] = {}
attached[backup_id]['vm'] = volume.attachments[0]['server_id']
attached[backup_id]['volume'] = volume.id
attached[backup_id]['device'] = volume.attachments[0]['device']
wait_for_glance_upload_to_finish(backups, tenant, output_dir="")
# Reattach volumes
for info in attached.values():
attach_volume(tenant, info['volume'], info['vm'], info['device'])
return tenant.id, True
#
# MAIN PART
#
# dont buffer stdout
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# Get keystone client
keystone = get_keystone_client()
openstack_lib.BACKUP_BASE_PATH = "/var/cinder_backup"
for tenant in keystone.tenants.list():
backup_tenant(tenant)