Skip to content

Commit 7cc2264

Browse files
2020-04-24
1 parent 96eb1f5 commit 7cc2264

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

python/cloneView/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ chmod +x cloneView.py
2222
Place both files in a folder together and run the main script like so:
2323

2424
```bash
25-
./cloneView.py -s mycluster -u admin [ -d domain ] -v myview -n newview [ -w ]
25+
./cloneView.py -s mycluster -u admin [ -d domain ] -v myview -n newview [ -f '2020-04-18 18:00:00' ] [ -w ]
2626
Connected!
2727
Cloning View myview as newview...
2828
```
@@ -34,20 +34,17 @@ Cloning View myview as newview...
3434
* -d, --domain: (optional) domain of username, defaults to local
3535
* -v, --view: name of source view to be cloned
3636
* -n, --newname: name of clone view to create
37+
* -f, --filedate: (optional) select backup version at or after specified date (defaults to latest backup)
3738
* -w, --wait: (optional) wait for completion and report exit status
3839

39-
## The Python Helper Module - pyhesity.py
40+
## Dates
4041

41-
The helper module provides functions to simplify operations such as authentication, api calls, storing encrypted passwords, and converting date formats. The module requires the requests python module.
42+
Use the -f parameter to specify the date from which to clone the view. The date may be entered like: '2020-04-20 17:05:00' or '2020-04-20' (which is interpreted as '2020-04-20 00:00:00).
4243

43-
### Installing the Prerequisites
44+
The oldest snapshot that is equal to or newer than the specified date will be used. For example, if the view is backed up every night at 9PM and you enter -f '2020-04-20 12:00:00', backup from Monday night at 9PM would be selected (Monday night's backup contains the files as they were on Monday at noon).
4445

45-
```bash
46-
sudo yum install python-requests
47-
```
46+
If the -f parameter is omitted, then the latest backup is used by default.
4847

49-
or
48+
## The Python Helper Module - pyhesity.py
5049

51-
```bash
52-
sudo easy_install requests
53-
```
50+
Please find more info on the pyhesity module here: <https://github.com/bseltz-cohesity/scripts/tree/master/python>

python/cloneView/cloneView.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
parser.add_argument('-d', '--domain', type=str, default='local') # Cohesity User Domain
1616
parser.add_argument('-v', '--view', type=str, required=True) # name of source view to clone
1717
parser.add_argument('-n', '--newname', type=str, required=True) # name of target view to create
18+
parser.add_argument('-f', '--filedate', type=str, default=None) # date time to recover view to
1819
parser.add_argument('-w', '--wait', action='store_true') # wait for completion
1920

2021
args = parser.parse_args()
@@ -24,6 +25,7 @@
2425
domain = args.domain
2526
viewName = args.view
2627
newName = args.newname
28+
filedate = args.filedate
2729
wait = args.wait
2830

2931
### authenticate
@@ -41,21 +43,34 @@
4143
print("View %s not found" % viewName)
4244
exit()
4345

44-
viewResult = viewResults[0]
46+
doc = viewResults[0]['vmDocument']
4547

46-
view = api('get', 'views/%s?includeInactive=True' % viewResult['vmDocument']['objectName'])
48+
view = api('get', 'views/%s?includeInactive=True' % doc['objectName'])
49+
50+
if filedate is not None:
51+
if ':' not in filedate:
52+
filedate = '%s 00:00:00' % filedate
53+
filedateusecs = dateToUsecs(filedate)
54+
versions = [v for v in doc['versions'] if filedateusecs <= v['snapshotTimestampUsecs']]
55+
if versions:
56+
version = versions[-1]
57+
else:
58+
print('No backups from the specified date')
59+
exit(1)
60+
else:
61+
version = doc['versions'][0]
4762

4863
taskName = "Clone-View_%s_as_%s" % (viewName, newName)
4964

5065
cloneTask = {
5166
"name": taskName,
5267
"objects": [
5368
{
54-
"jobUid": viewResult['vmDocument']['objectId']['jobUid'],
55-
"jobId": viewResult['vmDocument']['objectId']['jobId'],
56-
"jobInstanceId": viewResult['vmDocument']['versions'][0]['instanceId']['jobInstanceId'],
57-
"startTimeUsecs": viewResult['vmDocument']['versions'][0]['instanceId']['jobStartTimeUsecs'],
58-
"entity": viewResult['vmDocument']['objectId']['entity']
69+
"jobUid": doc['objectId']['jobUid'],
70+
"jobId": doc['objectId']['jobId'],
71+
"jobInstanceId": version['instanceId']['jobInstanceId'],
72+
"startTimeUsecs": version['instanceId']['jobStartTimeUsecs'],
73+
"entity": doc['objectId']['entity']
5974
}
6075
],
6176
"viewName": newName,
@@ -64,7 +79,7 @@
6479
"sourceViewName": view['name'],
6580
"cloneViewName": newName,
6681
"viewBoxId": view['viewBoxId'],
67-
"viewId": viewResult['vmDocument']['objectId']['entity']['id']
82+
"viewId": doc['objectId']['entity']['id']
6883
}
6984
}
7085

@@ -75,6 +90,9 @@
7590
exit(1)
7691

7792
print("Cloning View %s as %s..." % (viewName, newName))
93+
print("Backup date: %s" % usecsToDate(version['instanceId']['jobStartTimeUsecs']))
94+
95+
# wait for completion and report status
7896
taskId = response['restoreTask']['performRestoreTaskState']['base']['taskId']
7997
status = api('get', '/restoretasks/%s' % taskId)
8098

0 commit comments

Comments
 (0)