forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbackup.cc
480 lines (452 loc) · 15.7 KB
/
zbackup.cc
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "zutils.hh"
#include "debug.hh"
#include "version.hh"
#include "utils.hh"
DEF_EX( exSpecifyTwoKeys, "Specify password flag (--non-encrypted or --password-file)"
" for import/export/passwd operation twice (first for source and second for destination)", std::exception )
DEF_EX( exNonEncryptedWithKey, "--non-encrypted and --password-file are incompatible", std::exception )
DEF_EX( exSpecifyEncryptionOptions, "Specify either --password-file or --non-encrypted", std::exception )
DEF_EX( exSourceInaccessible, "Backup source file/directory is inaccessible", std::exception )
int main( int argc, char *argv[] )
{
try
{
dPrintf( "ZBackup version %s\n", zbackup_version.c_str() );
bool printHelp = false;
vector< char const * > args;
vector< string > passwords;
Config config;
for( int x = 1; x < argc; ++x )
{
string option;
Config::OptionType optionType = Config::Runtime;
if ( strcmp( argv[ x ], "--password-file" ) == 0 && x + 1 < argc )
{
// Read the password
char const * passwordFile = argv[ x + 1 ];
string passwordData;
if ( passwordFile )
{
File f( passwordFile, File::ReadOnly );
passwordData.resize( f.size() );
f.read( &passwordData[ 0 ], passwordData.size() );
// If the password ends with \n, remove that last \n. Many editors will
// add \n there even if a user doesn't want them to
if ( !passwordData.empty() &&
passwordData[ passwordData.size() - 1 ] == '\n' )
passwordData.resize( passwordData.size() - 1 );
passwords.push_back( passwordData );
}
++x;
}
else
if ( strcmp( argv[ x ], "--non-encrypted" ) == 0 )
{
passwords.push_back( "" );
}
else
if ( strcmp( argv[ x ], "--silent" ) == 0 )
verboseMode = false;
else
if ( strcmp( argv[ x ], "--exchange" ) == 0 && x + 1 < argc )
{
fprintf( stderr, "%s is deprecated, use -O exchange instead\n", argv[ x ] );
option = argv[ x ] + 2;//; + "=" + argv[ x + 1 ];
option += "=";
option += argv[ x + 1 ];
goto parse_option;
}
else
if ( strcmp( argv[ x ], "--threads" ) == 0 && x + 1 < argc )
{
fprintf( stderr, "%s is deprecated, use -O threads instead\n", argv[ x ] );
option = argv[ x ] + 2;
option += "=";
option += argv[ x + 1 ];
goto parse_option;
}
else
if ( strcmp( argv[ x ], "--cache-size" ) == 0 && x + 1 < argc )
{
fprintf( stderr, "%s is deprecated, use -O cache-size instead\n", argv[ x ] );
size_t cacheSizeMb;
char suffix[ 16 ];
int n;
if ( sscanf( argv[ x + 1 ], "%zu %15s %n",
&cacheSizeMb, suffix, &n ) == 2 && !argv[ x + 1][ n ] )
{
option = argv[ x ] + 2;
option += "=" + Utils::numberToString( cacheSizeMb ) + "MiB";
goto parse_option;
}
}
else
if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc )
{
fprintf( stderr, "%s is deprecated, use -o bundle.compression_method instead\n", argv[ x ] );
option = argv[ x ] + 2;
option += "=";
option += argv[ x + 1 ];
optionType = Config::Storable;
goto parse_option;
}
else
if ( strcmp( argv[ x ], "--help" ) == 0 || strcmp( argv[ x ], "-h" ) == 0 )
{
printHelp = true;
}
else
if ( ( strcmp( argv[ x ], "-o" ) == 0 || strcmp( argv[ x ], "-O" ) == 0 )
&& x + 1 < argc )
{
option = argv[ x + 1 ];
if ( !option.empty() )
{
if ( strcmp( argv[ x ], "-O" ) == 0 )
optionType = Config::Runtime;
else
if ( strcmp( argv[ x ], "-o" ) == 0 )
optionType = Config::Storable;
if ( strcmp( option.c_str(), "help" ) == 0 )
{
config.showHelp( optionType );
return EXIT_SUCCESS;
}
else
{
parse_option:
if ( !config.parseOrValidate( option, optionType ) )
goto invalid_option;
}
}
else
{
invalid_option:
fprintf( stderr, "Invalid option specified: %s\n",
option.c_str() );
return EXIT_FAILURE;
}
++x;
}
else
args.push_back( argv[ x ] );
}
if ( args.size() < 1 || printHelp )
{
fprintf( stderr,
"ZBackup, a versatile deduplicating backup tool, version %s\n"
"Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and\n"
"ZBackup contributors.\n"
"Comes with no warranty. Licensed under GNU GPLv2 or later + OpenSSL.\n"
"Visit the project's home page at http://zbackup.org/\n\n"
"Usage: %s [flags] <command [action]> [command args]\n"
"\n"
" Flags: --non-encrypted|--password-file <file>\n"
" password flag should be specified twice if\n"
" import/export/passwd command specified\n"
" --silent (default is verbose)\n"
" --help|-h show this message\n"
" -O <option[=value]> (overrides runtime configuration,\n"
" can be specified multiple times,\n"
" for detailed runtime options overview run with -O help)\n"
" -o <option[=value]> (overrides storable repository\n"
" configuration, can be specified multiple times,\n"
" for detailed storable options overview run with -o help)\n"
"\n"
" Commands:\n"
" init <storage path> - initializes new storage\n"
" backup <backup file name> - performs a backup from stdin\n"
" backup <input file> <backup file name> - performs a backup from file\n"
" backup <input dir> <backup dir> - performs a backup from directory\n"
" restore <backup file name> - restores a backup to stdout\n"
" restore <backup file name> <output file name> - restores\n"
" a backup to file using two-pass \"cacheless\" process\n"
" restore <backup dir> <output dir> - restores all backups contained\n"
" in specified directory using two-pass \"cacheless\" process\n"
" preserving directory hierarchy\n"
" export <source storage path> <destination storage path> -\n"
" performs export from source to destination storage\n"
" import <source storage path> <destination storage path> -\n"
" performs import from source to destination storage,\n"
" for export/import storage path must point to\n"
" a valid (initialized) storage\n"
" inspect [fast|deep] <backup file name> - inspect backup (default\n"
" is fast)\n"
" gc [fast|deep] <storage path> - performs garbage\n"
" collection (default is fast)\n"
" passwd <storage path> - changes repo info file passphrase\n"
" config [show|edit|set|reset] <storage path> - performs\n"
" configuration manipulations (default is show)\n"
"", zbackup_version.c_str(), *argv );
return EXIT_FAILURE;
}
if ( passwords.size() > 1 &&
( ( passwords[ 0 ].empty() && !passwords[ 1 ].empty() ) ||
( !passwords[ 0 ].empty() && passwords[ 1 ].empty() ) ) &&
( strcmp( args[ 0 ], "export" ) != 0 &&
strcmp( args[ 0 ], "import" ) != 0 &&
strcmp( args[ 0 ], "passwd" ) ) )
throw exNonEncryptedWithKey();
else
if ( passwords.size() != 2 &&
( strcmp( args[ 0 ], "export" ) == 0 ||
strcmp( args[ 0 ], "import" ) == 0 ||
strcmp( args[ 0 ], "passwd" ) == 0 ) )
throw exSpecifyTwoKeys();
else
if ( passwords.size() < 1 )
throw exSpecifyEncryptionOptions();
if ( strcmp( args[ 0 ], "init" ) == 0 )
{
// Perform the init
if ( args.size() != 2 )
{
fprintf( stderr, "Usage: %s %s <storage path>\n", *argv, args[ 0 ] );
return EXIT_FAILURE;
}
ZBackup::initStorage( args[ 1 ],
passwords[ 0 ], !passwords[ 0 ].empty(), config );
}
else
if ( strcmp( args[ 0 ], "backup" ) == 0 )
{
// Perform the backup
if ( args.size() < 2 || args.size() > 3)
{
fprintf( stderr, "Usage 1: your_backup_command | %s %s <backup file name>\n"
"Usage 2: %s %s <file> <backup file name>\n"
"Usage 3: %s %s <directory> <backup subdirectory>\n",
*argv, args[ 0 ], *argv, args[ 0 ], *argv, args[ 0 ]);
return EXIT_FAILURE;
}
string backupSource, backupsDest;
bool dirBackupMode = false;
if ( args.size() == 2 )
backupsDest = args[ 1 ];
else
{
backupSource = args[ 1 ];
if ( Dir::exists( backupSource ) )
{
dirBackupMode = true;
backupsDest = Dir::addPath( args[ 2 ], Dir::getBaseName( Dir::getRealPath( backupSource ) ));
}
else
backupsDest = args[ 2 ];
}
ZBackup zb( ZBackup::deriveStorageDirFromBackupsFile( backupsDest ),
passwords[ 0 ], config );
if ( args.size() == 2 )
zb.backupFromStdin( backupsDest );
else
{
if ( dirBackupMode )
zb.backupFromDirectory( args[ 1 ], backupsDest );
else
zb.backupFromFile( args[ 1 ], backupsDest );
}
}
else
if ( strcmp( args[ 0 ], "restore" ) == 0 )
{
// Perform the restore
if ( args.size() != 2 && args.size() != 3 )
{
fprintf( stderr, "Usage: %s %s <backup file name> [output file name]\n",
*argv , args[ 0 ] );
return EXIT_FAILURE;
}
ZRestore zr( ZRestore::deriveStorageDirFromBackupsFile( args[ 1 ] ),
passwords[ 0 ], config );
if ( args.size() == 3 )
{
if ( Dir::exists( args[ 1 ] ) )
zr.restoreToDirectory( args[ 1 ], args[ 2 ] );
else
zr.restoreToFile( args[ 1 ], args[ 2 ] );
}
else
zr.restoreToStdin( args[ 1 ] );
}
else
if ( strcmp( args[ 0 ], "export" ) == 0 || strcmp( args[ 0 ], "import" ) == 0 )
{
if ( args.size() != 3 )
{
fprintf( stderr, "Usage: %s %s <source storage path> <destination storage path>\n",
*argv, args[ 0 ] );
return EXIT_FAILURE;
}
if ( config.runtime.exchange.none() )
{
fprintf( stderr, "Specify any --exchange flag\n" );
return EXIT_FAILURE;
}
int src = 1, dst = 2;
if ( strcmp( args[ 0 ], "export" ) == 0 )
{
src = 1, dst = 2;
}
else
if ( strcmp( args[ 0 ], "import" ) == 0 )
{
src = 2, dst = 1;
}
dPrintf( "%s src: %s\n", args[ 0 ], args[ src ] );
dPrintf( "%s dst: %s\n", args[ 0 ], args[ dst ] );
ZExchange ze( ZBackupBase::deriveStorageDirFromBackupsFile( args[ src ], true ),
passwords[ src - 1 ],
ZBackupBase::deriveStorageDirFromBackupsFile( args[ dst ], true ),
passwords[ dst - 1 ],
config );
ze.exchange();
}
else
if ( strcmp( args[ 0 ], "gc" ) == 0 )
{
// Perform the garbage collection
if ( args.size() < 2 || args.size() > 3 )
{
fprintf( stderr, "Usage: %s %s [fast|deep] <storage path>\n",
*argv, args[ 0 ] );
return EXIT_FAILURE;
}
int fieldStorage = 1, fieldAction = 2;
if ( args.size() == 3 )
{
fieldStorage = 2, fieldAction = 1;
}
if ( args.size() > 2 && strcmp( args[ fieldAction ], "fast" ) == 0 )
{
ZCollector zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], config );
zc.gc( false );
}
else
if ( args.size() > 2 && strcmp( args[ fieldAction ], "deep" ) == 0 )
{
ZCollector zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], config );
zc.gc( true );
}
else
{
ZCollector zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], config );
zc.gc( false );
}
}
else
if ( strcmp( args[ 0 ], "passwd" ) == 0 )
{
// Perform the password change
if ( args.size() != 2 )
{
fprintf( stderr, "Usage: %s %s <storage path>\n",
*argv, args[ 0 ] );
return EXIT_FAILURE;
}
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ 1 ], true ),
passwords[ 0 ], true );
if ( passwords[ 0 ].empty() != passwords[ 1 ].empty() )
{
fprintf( stderr,
"Changing repo encryption type (non-encrypted to encrypted and vice versa) is possible "
"only via import/export operations.\n"
"Current repo type: %s.\n", zbb.encryptionkey.hasKey() ? "encrypted" : "non-encrypted" );
return EXIT_FAILURE;
}
zbb.setPassword( passwords[ 1 ] );
}
else
if ( strcmp( args[ 0 ], "inspect" ) == 0 )
{
if ( args.size() < 2 || args.size() > 3 )
{
fprintf( stderr, "Usage: %s %s [full] <storage path>\n",
*argv, args[ 0 ] );
return EXIT_FAILURE;
}
int fieldStorage = 1, fieldAction = 2;
if ( args.size() == 3 )
{
fieldStorage = 2, fieldAction = 1;
}
if ( args.size() > 2 && strcmp( args[ fieldAction ], "deep" ) == 0 )
{
ZInspect zi( ZRestore::deriveStorageDirFromBackupsFile( args[ fieldStorage ] ),
passwords[ 0 ], config, true );
zi.inspect( args[ fieldStorage ] );
}
else
{
ZInspect zi( ZRestore::deriveStorageDirFromBackupsFile( args[ fieldStorage ] ),
passwords[ 0 ], config, false );
zi.inspect( args[ fieldStorage ] );
}
}
else
if ( strcmp( args[ 0 ], "config" ) == 0 )
{
if ( args.size() < 2 || args.size() > 3 )
{
fprintf( stderr, "Usage: %s %s [show|edit|set|reset] <storage path>\n",
*argv, args[ 0 ] );
return EXIT_FAILURE;
}
int fieldStorage = 1, fieldAction = 2;
if ( args.size() == 3 )
{
fieldStorage = 2, fieldAction = 1;
}
if ( args.size() > 2 && strcmp( args[ fieldAction ], "edit" ) == 0 )
{
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], true );
if ( zbb.editConfigInteractively() )
zbb.saveExtendedStorageInfo();
}
else
if ( args.size() > 2 && strcmp( args[ fieldAction ], "set" ) == 0 )
{
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], config, true );
zbb.config.show();
zbb.saveExtendedStorageInfo();
}
else
if ( args.size() > 2 && strcmp( args[ fieldAction ], "reset" ) == 0 )
{
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], true );
zbb.config.reset_storable();
zbb.config.show();
zbb.saveExtendedStorageInfo();
}
else
{
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ], true );
zbb.config.show();
}
}
else
{
fprintf( stderr, "Error: unknown command line option: %s\n", args[ 0 ] );
return EXIT_FAILURE;
}
}
catch( std::exception & e )
{
fprintf( stderr, "%s\n", e.what() );
return EXIT_FAILURE;
}
catch( ... )
{
fprintf( stderr, "Unknown exception!\n" );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}