forked from arqbackup/arq_restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArqRestoreCommand.m
269 lines (253 loc) · 10.6 KB
/
ArqRestoreCommand.m
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
268
269
/*
Copyright (c) 2009, Stefan Reitshamer http://www.haystacksoftware.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the names of PhotoMinds LLC or Haystack Software, nor the names of
their contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "ArqRestoreCommand.h"
#import "SetNSError.h"
#import "S3AuthorizationProvider.h"
#import "S3Service.h"
#import "RegexKitLite.h"
#import "DictNode.h"
#import "HTTP.h"
#import "Restorer.h"
#import "NSErrorCodes.h"
#import "NSError_extra.h"
#import "UserAndComputer.h"
#import "ArqSalt.h"
#import "ArqRepo.h"
#import "BackupSet.h"
#import "ReflogPrinter.h"
@interface ArqRestoreCommand (internal)
- (BOOL)printArqFolders:(NSError **)error;
- (BOOL)processPath:(NSError **)error;
- (BOOL)validateS3Keys:(NSError **)error;
@end
@implementation ArqRestoreCommand
- (id)init {
if (self = [super init]) {
char *theAccessKey = getenv("ARQ_ACCESS_KEY");
if (theAccessKey != NULL) {
accessKey = [[NSString alloc] initWithUTF8String:theAccessKey];
}
char *theSecretKey = getenv("ARQ_SECRET_KEY");
if (theSecretKey != NULL) {
secretKey = [[NSString alloc] initWithUTF8String:theSecretKey];
}
char *theEncryptionPassword = getenv("ARQ_ENCRYPTION_PASSWORD");
if (theEncryptionPassword != NULL) {
encryptionPassword = [[NSString alloc] initWithUTF8String:theEncryptionPassword];
}
if (accessKey != nil && secretKey != nil) {
S3AuthorizationProvider *sap = [[S3AuthorizationProvider alloc] initWithAccessKey:accessKey secretKey:secretKey];
s3 = [[S3Service alloc] initWithS3AuthorizationProvider:sap useSSL:NO retryOnTransientError:YES];
[sap release];
}
}
return self;
}
- (void)dealloc {
[accessKey release];
[secretKey release];
[encryptionPassword release];
[s3 release];
[path release];
[commitSHA1 release];
[super dealloc];
}
- (BOOL)readArgc:(int)argc argv:(const char **)argv {
for (int i = 1; i < argc; i++) {
if (*argv[i] == '-') {
if (!strcmp(argv[i], "-l")) {
if (argc <= i+1) {
fprintf(stderr, "missing log_level argument (error,warn,info,debug or trace)\n");
return NO;
}
i++;
NSString *level = [NSString stringWithUTF8String:argv[i]];
setHSLogLevel(hsLogLevelForName(level));
} else if (!strcmp(argv[i], "-v")) {
printf("%s version 20-Aug-2012\n", argv[0]);
exit(0);
} else {
fprintf(stderr, "unknown option %s\n", argv[i]);
return NO;
}
} else if (path == nil) {
path = [[NSString alloc] initWithUTF8String:argv[i]];
} else if (commitSHA1 == nil) {
commitSHA1 = [[NSString alloc] initWithUTF8String:argv[i]];
} else {
fprintf(stderr, "warning: ignoring argument '%s'\n", argv[i]);
}
}
return YES;
}
- (BOOL)execute:(NSError **)error {
BOOL ret = YES;
if (path == nil) {
ret = [self printArqFolders:error];
} else {
ret = [self processPath:error];
}
return ret;
}
@end
@implementation ArqRestoreCommand (internal)
- (BOOL)printArqFolders:(NSError **)error {
if (![self validateS3Keys:error]) {
return NO;
}
NSArray *backupSets = [BackupSet allBackupSetsForAccessKeyID:accessKey secretAccessKey:secretKey error:error];
if (backupSets == nil) {
return NO;
}
for (BackupSet *backupSet in backupSets) {
NSString *s3BucketName = [backupSet s3BucketName];
NSString *computerUUID = [backupSet computerUUID];
UserAndComputer *uac = [backupSet userAndComputer];
printf("S3 bucket: %s", [s3BucketName UTF8String]);
if (uac != nil) {
printf(" %s (%s)", [[uac computerName] UTF8String], [[uac userName] UTF8String]);
} else {
printf(" (unknown computer)");
}
printf(" UUID %s", [computerUUID UTF8String]);
NSString *computerBucketsPrefix = [NSString stringWithFormat:@"/%@/%@/buckets", s3BucketName, computerUUID];
NSArray *s3BucketUUIDPaths = [s3 pathsWithPrefix:computerBucketsPrefix error:error];
if (s3BucketUUIDPaths == nil) {
return NO;
}
if ([s3BucketUUIDPaths count] == 0) {
printf(" (no folders found)");
}
printf("\n");
for (NSString *uuidPath in s3BucketUUIDPaths) {
NSData *data = [s3 dataAtPath:uuidPath error:error];
if (data == nil) {
return NO;
}
DictNode *plist = [DictNode dictNodeWithXMLData:data error:error];
if (plist == nil) {
return NO;
}
printf(" %s\n", [[[plist stringNodeForKey:@"LocalPath"] stringValue] UTF8String]);
printf(" UUID: %s\n", [[uuidPath lastPathComponent] UTF8String]);
printf(" reflog command: arq_restore %s reflog\n", [uuidPath UTF8String]);
printf(" restore command: arq_restore %s\n", [uuidPath UTF8String]);
printf("\n");
}
}
return YES;
}
- (BOOL)processPath:(NSError **)error {
if (![self validateS3Keys:error]) {
return NO;
}
if (encryptionPassword == nil) {
SETNSERROR(@"ArqErrorDomain", -1, @"missing ARQ_ENCRYPTION_PASSWORD environment variable");
return NO;
}
NSString *pattern = @"^/([^/]+)/([^/]+)/buckets/([^/]+)";
NSRange s3BucketNameRange = [path rangeOfRegex:pattern capture:1];
NSRange computerUUIDRange = [path rangeOfRegex:pattern capture:2];
NSRange bucketUUIDRange = [path rangeOfRegex:pattern capture:3];
if (s3BucketNameRange.location == NSNotFound || computerUUIDRange.location == NSNotFound || bucketUUIDRange.location == NSNotFound) {
SETNSERROR(@"ArqErrorDomain", -1, @"invalid S3 path");
return NO;
}
NSData *data = [s3 dataAtPath:path error:error];
if (data == nil) {
return NO;
}
DictNode *plist = [DictNode dictNodeWithXMLData:data error:error];
if (plist == nil) {
return NO;
}
NSString *s3BucketName = [path substringWithRange:s3BucketNameRange];
NSString *computerUUID = [path substringWithRange:computerUUIDRange];
NSString *bucketUUID = [path substringWithRange:bucketUUIDRange];
NSString *bucketName = [[plist stringNodeForKey:@"BucketName"] stringValue];
NSError *uacError = nil;
NSData *uacData = [s3 dataAtPath:[NSString stringWithFormat:@"/%@/%@/computerinfo", s3BucketName, computerUUID] error:&uacError];
UserAndComputer *uac = nil;
if (uacData != nil) {
uac = [[[UserAndComputer alloc] initWithXMLData:uacData error:&uacError] autorelease];
}
NSError *saltError = nil;
ArqSalt *arqSalt = [[[ArqSalt alloc] initWithAccessKeyID:accessKey secretAccessKey:secretKey s3BucketName:s3BucketName computerUUID:computerUUID] autorelease];
NSData *salt = [arqSalt salt:&saltError];
if (salt == nil) {
if ([saltError code] != ERROR_NOT_FOUND) {
if (error != NULL) {
*error = saltError;
}
return NO;
}
}
ArqRepo *repo = [[[ArqRepo alloc] initWithS3Service:s3 s3BucketName:s3BucketName computerUUID:computerUUID bucketUUID:bucketUUID encryptionPassword:encryptionPassword salt:salt error:error] autorelease];
if (repo == nil) {
return NO;
}
if ([commitSHA1 isEqualToString:@"reflog"]) {
printf("printing reflog for %s\n", [bucketName UTF8String]);
ReflogPrinter *printer = [[[ReflogPrinter alloc] initWithS3BucketName:s3BucketName computerUUID:computerUUID bucketUUID:bucketUUID s3:s3 repo:repo] autorelease];
if (![printer printReflog:error]) {
return NO;
}
} else {
printf("restoring %s from ", [bucketName UTF8String]);
if (uac != nil) {
printf("%s (%s)", [[uac computerName] UTF8String], [[uac userName] UTF8String]);
} else {
printf("(unknown computer)");
}
printf(" to %s/%s\n", [[[NSFileManager defaultManager] currentDirectoryPath] UTF8String], [bucketName UTF8String]);
Restorer *restorer = [[[Restorer alloc] initWithRepo:repo bucketName:bucketName commitSHA1:commitSHA1] autorelease];
if (![restorer restore:error]) {
return NO;
}
printf("restored files are in %s\n", [bucketName fileSystemRepresentation]);
NSDictionary *errorsByPath = [restorer errorsByPath];
if ([errorsByPath count] > 0) {
printf("Errors occurred:\n");
NSArray *sortedKeys = [[errorsByPath allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (NSString *key in sortedKeys) {
printf("%s\t\t%s\n", [key UTF8String], [[[errorsByPath objectForKey:key] localizedDescription] UTF8String]);
}
}
}
return YES;
}
- (BOOL)validateS3Keys:(NSError **)error {
if (accessKey == nil) {
SETNSERROR(@"ArqErrorDomain", -1, @"missing ARQ_ACCESS_KEY environment variable");
return NO;
}
if (secretKey == nil) {
SETNSERROR(@"ArqErrorDomain", -1, @"missing ARQ_SECRET_KEY environment variable");
return NO;
}
return YES;
}
@end