-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestore.py
executable file
·76 lines (62 loc) · 1.92 KB
/
restore.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
#!/usr/bin/env python
#
# This will take a snapshot and convert it into a volume. To create a volume
# without any links to the old snapshot you need to convert it to a temporary
# volume first, convert that into an image and convert the image back into
# your final volume. Once this is all done, the temporary volume and image
# will be removed.
#
import argparse
import openstack
import time
import sys
from sdk import Snapshot
def main(args):
# Set up the connection to OpenStack -- this is read from clouds.yaml
openstack.enable_logging(debug=False)
api = openstack.connect(cloud=args.cloud)
snapshot_id = args.snapshot
server = args.volume
# Create a snapshot object
try:
snapshot = Snapshot(
api=api,
snapshot=api.volume.get_snapshot(snapshot_id),
)
except openstack.exceptions.ResourceNotFound:
print('Snapshot id {} not found.'.format(snapshot_id))
sys.exit(1)
today = time.strftime("%d-%m-%Y")
# Convert the snapshot to a volume
print('')
print('Converting snapshot to volume..')
volume = snapshot.to_volume('{}-restore-{}'.format(server, today))
print('Converting volume to image..')
image = volume.to_image('{}-restore-{}'.format(server, today))
print('Converting image to volume..')
image.to_volume(server, size=volume.volume.size)
image.delete()
volume.delete()
print('')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Restore snapshots')
parser.add_argument(
'--snapshot',
required=True,
help='',
metavar=('<snapshot_id>'),
)
parser.add_argument(
'--volume',
required=True,
help='',
metavar=('<volume name>'),
)
parser.add_argument(
'--cloud',
help='',
metavar=('<cloud in clouds.yaml>'),
default='fuga',
)
args = parser.parse_args()
main(args)