forked from code42/crashplan_api_examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpushRestore.sh
executable file
·267 lines (235 loc) · 8.11 KB
/
pushRestore.sh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
#######################################################################################
#
# pushRestore.sh [sourceComputer] [destComputer] [restorePath] [getDirs] [getFiles]
#
# Example:
# pushRestore.sh 'COMP1' 'COMP2' '/tmp/restore' '/tmp/dir1,/tmp/dir 2' '/tmp/file1,/tmp/file 2'
#
# This example script performs an automated Push Restore (via REST):
# * One or more source files/directories.
# * To local or remote destination computer. To remote directory of choice.
# * Using MPC or Cloud storage.
# * Recursive restore (includes subdirectories).
# * Between computers of different users (if Admin).
# * ...and more.
#
# Edit the variables near the top of this script, or pass in optional args.
#
# Notes:
# * Push destination should be running authenticated Crashplan client.
# * Archive adoption (or original owner) not requried.
#
# Author: Marc Johnson, Code 42 Software
# Last Modified: 09-19-2016 by Jack Phinney, Code42 Software
#
# --------
#
# # Copyright (c) 2016 Code42, Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#######################################################################################
###################################
# Local Variables (customize)
###################################
# PROe Master Server (e.g. http/4280 or https/4285)
MASTER='http://192.168.0.11:4280'
# Restore credentials
CPLOGIN='admin:admin00'
# Computers (both can be remote and different)
sourceComputer=C02HT6EGF57J
destComputer=C02HT6EGF57J
# Where on destination computer to restore
restorePath='/tmp/mjohnson'
# Add any number of Files or Directories.
# Restore still works if some not in archive.
# Note that Windows systems need "C:/" prepended
getDirs=(
"/Users/mjohnson/Documents/test"
"/Users/mjohnson/Documents/Scratch Projects"
)
# getFiles=(
# "/Users/mjohnson/Desktop/test.pdf"
# "/Users/mjohnson/Desktop/test2.pdf"
# "/Users/mjohnson/Desktop/test3.pdf"
#)
# Timeout limit for Web Restore
TIMEOUT=900 #seconds
# Flag to move restored files out of container restore directory.
# Performs additional status monitoring. Currently only works
# if script running on destination + MPC. See below.
DOMOVE=false
###################################
# Utilize Optional Commandline Args if Present
# [sourceComputer] [destComputer] [restorePath] [getDirs] [getFiles]
###################################
sourceComputer=${1:-$sourceComputer}
destComputer=${2:-$destComputer}
restorePath=${3:-$restorePath}
OLD_IFS=$IFS
IFS=','
if [ -n "$4" ]; then
getDirs=($4)
fi
if [ -n "$5" ]; then
getFiles=($5)
fi
IFS=$OLD_IFS
###################################
# Helper Functions
###################################
# API Get
function api {
echo `curl -sku $CPLOGIN --header "Accept: application/json" "$MASTER/api/${1}?${2}"`
}
# API Post
function apiPost {
echo `curl -sku $CPLOGIN --header "Content-Type: application/json" -X POST -d "${2}" "$MASTER/api/${1}"`
}
# Json value for given key
function jsonValue {
echo "${2}" | grep "${1}" | sed 's/^.*"'${1}'":"\([^"]*\)".*$/\1/'
}
###################################
# Setup Session
###################################
echo "Setting up Session..."
# Get source computer info
JSON=`api Computer 'q="'${sourceComputer}'"&incBackupUsage=true&active=true'`
sourceGuid=`jsonValue guid "${JSON}"`
serverName=`jsonValue serverName "${JSON}"`
# If provider node
if [[ "${serverName}" == *ProviderNode* ]]; then
echo "Detected Public Cloud storage source..."
destGuid=`echo ${JSON} | python -mjson.tool | grep serverGuid | cut -d'"' -f4`
else
# Get destination data is stored on
echo "Detected MPC storage source..."
JSON=`api Server 'q="'${serverName}'"'`
destGuid=`jsonValue guid "${JSON}"`
fi
# Get accepting computer guid
JSON=`api Computer 'q="'${destComputer}'"&active=true'`
acceptingGuid=`jsonValue guid "${JSON}"`
echo "Getting Data Key Token..."
# Get Data Key Token
DATA='{"computerGuid":'${sourceGuid}'}'
JSON=`apiPost DataKeyToken "${DATA}"`
dataKeyToken=`jsonValue dataKeyToken "${JSON}"`
# Get Web Restore Session Id
DATA='{"computerGuid":"'${sourceGuid}'", "dataKeyToken":"'${dataKeyToken}'" }'
JSON=`apiPost WebRestoreSession "${DATA}"`
webRestoreSessionId=`jsonValue webRestoreSessionId "${JSON}"`
###################################
# Build Push Restore
###################################
# Display status and build request
echo "Requesting restore from ${sourceComputer}:"
for var in "${getDirs[@]}"
do
echo " ${var}"
pathJSON=${pathJSON}'{"type":"directory", "path":"'"${var}"'","selected":true},'
done
for var in "${getFiles[@]}"
do
echo " ${var}"
pathJSON=${pathJSON}'{"type":"file", "path":"'"${var}"'","selected":true},'
done
pathJSON="${pathJSON%?}" #remove trailing comma
echo "...Pushed to ${restorePath} on ${destComputer}"
# Payload to POST
DATA=' {
"webRestoreSessionId":"'${webRestoreSessionId}'",
"sourceGuid":"'${sourceGuid}'",
"targetNodeGuid":"'${destGuid}'",
"acceptingGuid":"'${acceptingGuid}'",
"restorePath":"'"${restorePath}"'",
"pathSet":[
'${pathJSON}'
],
"numBytes":1,
"numFiles":1,
"showDeleted":true,
"restoreFullPath":true
}'
###################################
# Push Restore
###################################
JSON=`apiPost PushRestoreJob "${DATA}"`
restoreId=`jsonValue restoreId "${JSON}"`
###################################
# Result
###################################
echo
echo "Request / POST: ${DATA}"
echo
echo "Response from Server: ${JSON}"
echo
if [ -z "${restoreId}" ]
then
echo "Push Restore NOT successful."
exit
else
echo "Push Restore successfully submitted and accepted."
echo "In progress... may take a few minutes."
echo "Restored files in ${restorePath}"
fi
#################################################
# PERFORM LOCAL MOVE if flag set:
# Move restored files out of container directory.
# Perform additional status monitoring.
# Script must be running on destination + MPC.
#################################################
if [ "${DOMOVE}" == "true" ];
then
###################################
# Watch RestoreRecord
###################################
echo
echo -n "Monitoring restore progress"
counter=0
until [ -z "${JSON}" ] || [[ ${counter} -gt ${TIMEOUT} ]]; do
sleep 1
counter=$((counter + 1))
JSON=`api "RestoreRecord/${restoreId}" | grep "not found"`
echo -n .
done
echo
###################################
# Move to destination directory
###################################
JSON=`api "RestoreRecord/${restoreId}"`
completedDate=`jsonValue completedDate "${JSON}"`
if [ -z "${completedDate}" ]
then
echo "Push Restore NOT successful. Timed out."
else
echo "Push Restore completed successfully."
restoreDir=`ls -tr ${restorePath} | grep crashplan-restore | tail -n 1` # todo: timiestamp testing
if [ -z "${restoreDir}" ]
then
echo "Could not find Restore diretory."
else
echo "Contents of Restore directory:"
restoreDir=${restorePath}/${restoreDir}
find ${restoreDir} | sed s:"${restoreDir}"::
# move out of temporary container folder (prompt for overwrite)
sudo mv -i ${restoreDir}/* ${restorePath}
# sudo rm -rf ${restoreDir} # optional cleanup temp folder
exit
fi
fi
fi