-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsprinkle.py
executable file
·1066 lines (918 loc) · 34.8 KB
/
sprinkle.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
sprinkle : the cloud clustered backup utility
"""
__author__ = "Michael Montuori [[email protected]]"
__copyright__ = "Copyright 2017 Michael Montuori. All rights reserved."
__credits__ = ["Warren Crigger"]
__license__ = "GPLv3"
__version__ = "1.0"
__revision__ = "0"
__docformat__ = "reStructuredText"
from libsprinkle import clsync
from libsprinkle import rclone
from libsprinkle import config
from libsprinkle import common
from libsprinkle import smtp_email
from libsprinkle import sprinkle_daemon
import logging
import getopt
import sys
import traceback
import os
try:
from filelock import Timeout, FileLock
except:
print('FileLock library not found. run: "pip3 install filelock"')
quit()
lock = FileLock("sprinkle.lock", timeout=1)
def warranty():
"""
WARRANTY:
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR ITS 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.
"""
def authors():
"""
AUTHOR:
Michael Montuori, [[email protected]]
"""
def copyrights():
"""
COPYRIGHT:
(C)2018 Michael Montuori, [[email protected]]. All rights reserved.
"""
return
def credits():
"""
CREDITS:
Warren Crigger for development and testing support
"""
def usage_options():
"""
OPTIONS:
-c, --conf {config file} configuration file
-d, --debug debug output
-h, --help help
-v, --version print version
--check-prereq chech prerequisites
--comp-method {size|md5} compare method [size|md5] (default:size)
--daemon-interval interval for the daemon to execute in minutes (default:60)
--daemon-mode start sprinkle in daemon mode
--daemon-pidfile daemon pidfile (default:/var/run/sprinkle.pid or /tmp/sprinkle.pid)
--daemon-type type of daemon [interval|ondemand] (default:interval)
--delete-files do not delete files on remote end (default:false)
--display-unit {G|M|K|B} display unit (G)igabytes, (M)egabytes, (K)ilobytes, or (B)ites
--dist-type {mas} distribution type (default:mas)
--dry-run perform a dry run without actually backing up
--exclude-file {file} file containing the backup exclude paths
--exclude-regex {regex} regular expression to match for file backup exclusion
--log-file {file} logs output to the specified file
--no-cache turn off caching
--rclone-conf {config file} rclone configuration (default:None)
--rclone-exe {rclone_exe} rclone executable (default:rclone)
--restore-duplicates restore files if duplicates are found (default:false)
--retries {num_retries} number of retries (default:1)
--show-progress show progress
--single-instance make sure only 1 concurrent instance of sprinkle is running (default:False)
"""
return
def usage_commands():
"""
COMMANDS:
backup backup files to clustered drives
config configure rclone to access volumes
help displays the help fot the specific command
ls list files
lsmd5 list md5 of files
stats display volume statistics
restore restore files from clustered drives
removedups removes duplicate files
"""
return
def usage():
"""
NAME:
sprinkle - the cloud clustered backup utility
SYNOPSIS:
sprinkle.py [options} {command} {arg...arg}
DESCRIPTION:
Sprinkle is a volume clustering utility. It presents all the RClone available volumes as a single clustered volume.
It supports 1-way sync mainly for backup and recovery.
Sprinkle uses the excellent [RClone](https://rclone.org) software for cloud volume access.
EXAMPLES:
sprinkle.py ls /backup
sprinkle.py backup /dir_to_backup
sprinkle.py restore /backup /opt/restore_dir
sprinkle.py stats
sprinkle.py -c /home/sprinkle/sprinkle.conf ls /backup
"""
print(usage.__doc__)
version()
print(usage_commands.__doc__)
print(usage_options.__doc__)
print(authors.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
print(warranty.__doc__)
def usage_config():
"""
NAME:
sprinkle config - configure sprinkle/rclone
SYNOPSIS:
sprinkle.py [options] config
DESCRIPTION:
Configure sprinkle/rclone to communicate with remote volumes. This process might enable API access and may
require authorization before sprinkle/rclone can access the drives. For config documentation refer to rclone
documentation at https://rclone.org/docs/.
EXAMPLES:
sprinkle.py config
"""
print(usage_ls.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_ls():
"""
NAME:
sprinkle ls - List files on remote volumes
SYNOPSIS:
sprinkle.py [options] ls {path}
DESCRIPTION:
List files on the remote drive. The output generated by the command looks like:
--- NAME SIZE MOD TIME REMOTE
--- ---------------------------------------------------------------- --------- ------------------- ---------------
-d- /backup/directory -1 2018-10-21:00:18:53 volume1:
--- /backup/directory/file.txt 8580 2018-10-21:00:17:28 volume1:
-d- indicates that the file is a directory and --- indicates a regular file
ARGUMENTS:
path
the remote path to list files
EXAMPLES:
sprinkle.py ls /backup
sprinkle.py ls /
"""
print(usage_ls.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_lsmd5():
"""
NAME:
sprinkle lsmd5 - List file MD5 hash on remote volumes
SYNOPSIS:
sprinkle.py [options] lsmd5 {path}
DESCRIPTION:
List files on the remote drive with the respective MD5 hash. The output generated by the command looks like:
NAME MD5
---------------------------------------------------------------- --------------------------------
/backup/directory/file1.txt 92de4cde16da896dcc6289b92df42976
/backup/directory/file2.txt 86efff36b7b0df257f1779d974c8101b
ARGUMENTS:
path
the remote path to list files
EXAMPLES:
sprinkle.py lsmd5 /backup
sprinkle.py lsmd5 /
"""
print(usage_lsmd5.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_backup():
"""
NAME:
sprinkle backup - backs up the local directory to remote volumes
SYNOPSIS:
sprinkle.py [options] backup {local dir}
DESCRIPTION:
Backs up the local directory to the remote drives configured in rclone.
ARGUMENTS:
local dir
the local directory to backup
EXAMPLES:
sprinkle.py backup /backup
"""
print(usage_backup.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_restore():
"""
NAME:
sprinkle restores - restore files from a previously backed up directory
SYNOPSIS:
sprinkle.py [options] restore {remote dir} {local dir}
DESCRIPTION:
Restores the remote directories from the rclone drives to the local directory specified.
ARGUMENTS:
remote dir
the remote directory to restore
local dir
the local directory to use
EXAMPLES:
sprinkle.py restore /backup c:/backup
"""
print(usage_restore.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_help():
"""
NAME:
sprinkle help - display help for specific commands
SYNOPSIS:
sprinkle.py [options] help {command}
DESCRIPTION:
displays the general help about sprinkle
ARGUMENTS:
command
the command to display help for
EXAMPLES:
sprinkle.py help
"""
print(usage_help.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
print(warranty.__doc__)
def usage_stats():
"""
NAME:
sprinkle stats - display volume statistics
SYNOPSIS:
sprinkle.py [options] stats
DESCRIPTION:
display the statistics about all the remote volumes. The output should look like:
REMOTE SIZE FREE %FREE
=============== ==================== ==================== ==========
volume1: 15G 0G 1
volume2: 15G 1G 7
volume3: 15G 0G 3
--------------- -------------------- -------------------- ----------
total: 45G 1G 3
EXAMPLES:
sprinkle.py stats
sprinkle.py --display-unit=K stats
"""
print(usage_stats.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_removedups():
"""
NAME:
sprinkle removedups - remove duplicate files from remote volumes
SYNOPSIS:
sprinkle.py [options] removedups {path}
DESCRIPTION:
Removes duplicate files from remote volumes. Remote file can accumulate over time due to a variety of
conditions. Run this utility often to minimize chances of having corrupt data.
ARGUMENTS:
path
the remote path to list files
EXAMPLES:
sprinkle.py removedups /backup
"""
print(usage_removedups.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def usage_find():
"""
NAME:
sprinkle find - search for files on the remote volumes
SYNOPSIS:
sprinkle.py [options] find {regexp}
DESCRIPTION:
Finds files on all configured remote volumes specified with the regular expression.
ARGUMENTS:
regexp
the regular expression used to filter files
EXAMPLES:
sprinkle.py find "/backup/....sh"
"""
print(usage_find.__doc__)
print(usage_options.__doc__)
print(copyrights.__doc__)
print(credits.__doc__)
def version():
print("VERSION:\n " + __version__ + '.' + __revision__ +
", module version: " + clsync.__version__ + '.' + clsync.__revision__ +
", rclone module version: " + rclone.__version__ + '.' + rclone.__revision__)
def read_args(argv):
global __configfile
global __dirtosync
global __args
global __cmd_debug
global __dist_type
global __comp_method
global __rclone_exe
global __rclone_conf
global __display_unit
global __rclone_retries
global __show_progress
global __delete_files
global __restore_duplicates
global __dry_run
global __smtp_enable
global __smtp_from
global __smtp_to
global __smtp_server
global __smtp_port
global __smtp_user
global __smtp_password
global __no_cache
global __cl_sync
global __exclude_file
global __exclude_regex
global __log_file
global __single_instance
global __check_prereq
global __daemon_type
global __daemon_mode
global __daemon_interval
global __daemon_pidfile
__configfile = None
__cmd_debug = None
__dist_type = None
__comp_method = None
__rclone_exe = None
__rclone_conf = None
__display_unit = None
__rclone_retries = None
__show_progress = None
__delete_files = None
__restore_duplicates = False
__dry_run = None
__smtp_enable = None
__smtp_from = None
__smtp_to = None
__smtp_server = None
__smtp_port = None
__smtp_user = None
__smtp_password = None
__no_cache = None
__cl_sync = None
__exclude_file = None
__exclude_regex = None
__log_file = None
__single_instance = None
__check_prereq = None
__daemon_type = None
__daemon_mode = False
__daemon_interval = None
__daemon_pidfile = None
try:
opts, args = getopt.getopt(argv, "dvhc:s:",
["help",
"conf=",
"debug",
"version",
"dist-type=",
"comp-method=",
"rclone-exe=",
"rclone-conf=",
"stats=",
"display-unit=",
"rclone-retries=",
"show-progress",
"dry-run",
"delete-files",
"restore-duplicates",
"smtp-enable",
"smtp-from=",
"smtp-to=",
"smtp-server=",
"smtp-port=",
"smtp-user=",
"smtp-password=",
"no-cache",
"exclude-file=",
"exclude-regex=",
"log-file=",
"single-instance",
"check-prereq",
"daemon-type=",
"daemon-mode",
"daemon-interval=",
"daemon-pidfile="
])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(0)
elif opt in ('-v', '--version'):
version()
sys.exit(0)
elif opt in ("-c", "--conf"):
__configfile = arg
elif opt in ("-d", "--debug"):
__cmd_debug = True
elif opt in ("--dist-type"):
__dist_type = arg
elif opt in ("--comp-method"):
__comp_method = arg
elif opt in ("--rclone-exe"):
__rclone_exe = arg
elif opt in ("--rclone-conf"):
__rclone_conf = arg
elif opt in ("--display-unit"):
if arg != 'G' and arg != 'M' and arg != 'K' and arg != 'B':
logging.error('invalid UNIT ' + arg + ', only [G|M|K|B] accepted')
__display_unit = arg
elif opt in ("--rclone-retries"):
__rclone_retries = int(arg)
elif opt in ("--show-progress"):
__show_progress = True
elif opt in ("--delete-files"):
__delete_files = True
elif opt in ("--restore-duplicates"):
__restore_duplicates = True
elif opt in ("--dry-run"):
__dry_run = True
elif opt in ("--smtp-enable"):
__smtp_enable = True
elif opt in ("--smtp-from"):
__smtp_from = arg
elif opt in ("--smtp-to"):
__smtp_to = arg
elif opt in ("--smtp-server"):
__smtp_server = arg
elif opt in ("--smtp-port"):
__smtp_port = arg
elif opt in ("--smtp-user"):
__smtp_user = arg
elif opt in ("--smtp-password"):
__smtp_password = arg
elif opt in ("--no-cache"):
__no_cache = True
elif opt in ("--exclude-file"):
__exclude_file = arg
elif opt in ("--exclude-regex"):
__exclude_regex = arg
elif opt in ("--log-file"):
__log_file = arg
elif opt in ("--single-instance"):
__single_instance = True
elif opt in ("--check-prereq"):
__check_prereq = True
elif opt in ("--daemon-type"):
print("here1")
__daemon_type = arg
elif opt in ("--daemon-mode"):
__daemon_mode = True
elif opt in ("--daemon-interval"):
__daemon_interval = int(arg)
elif opt in ("--daemon-pidfile"):
__daemon_pidfile = arg
if len(args) < 1 and __check_prereq is None:
usage()
sys.exit()
__args = args
def configure(config_file):
global __config
_default_values = {
"debug": False,
"dry_run": False,
"show_progress": False,
"delete_files": False,
"restore_duplicates": False,
"smtp_enable": False,
"no_cache": False,
"distribution_type": "mas",
"compare_method": "size",
"display_unit": "G",
"rclone_retries": '1',
"log_file": None,
"single_instance": False,
"check_prereq": False,
"daemon_type": 'interval',
"daemon_mode": False,
"daemon_interval": 60,
"daemon_pidfile": '/var/run/sprinkle.pid'
}
if config_file is not None:
conf = config.Config(config_file)
__config = conf.get_config()
else:
__config = {}
for field in _default_values:
if field not in __config:
__config[field] = _default_values[field]
if __cmd_debug is True:
init_logging(True, __daemon_mode)
elif 'debug' in __config:
init_logging(__config['debug'], __daemon_mode)
if __dist_type is not None:
__config['distribution_type'] = __dist_type
if __comp_method is not None:
__config['compare_method'] = __comp_method
if __rclone_exe is not None:
__config['rclone_exe'] = __rclone_exe
if __rclone_conf is not None:
__config['rclone_config'] = __rclone_conf
if __rclone_retries is not None:
__config['rclone_retries'] = str(__rclone_retries)
if __show_progress is not None:
__config['show_progress'] = __show_progress
if __delete_files is not None:
__config['delete_files'] = __delete_files
if __dry_run is not None:
__config['dry_run'] = __dry_run
if __smtp_enable is not None:
__config['smtp_enable'] = __smtp_enable
if __display_unit is not None:
__config['display_unit'] = __display_unit
if __smtp_from is not None:
__config['smtp_from'] = __smtp_from
if __smtp_to is not None:
__config['smtp_to'] = __smtp_to
if __smtp_server is not None:
__config['smtp_server'] = __smtp_server
if __smtp_port is not None:
__config['smtp_port'] = __smtp_port
if __smtp_user is not None:
__config['smtp_user'] = __smtp_user
if __smtp_password is not None:
__config['smtp_password'] = __smtp_password
if __no_cache is not None:
__config['no_cache'] = __no_cache
if __exclude_file is not None:
__config['exclude_file'] = __exclude_file
if __exclude_regex is not None:
__config['exclude_regex'] = __exclude_regex
if __log_file is not None:
__config['log_file'] = __log_file
if __single_instance is not None:
__config['single_instance'] = __single_instance
if __check_prereq is not None:
__config['check_prereq'] = __check_prereq
if __daemon_type is not None:
__config['daemon_type'] = __daemon_type
if __daemon_mode is not None:
__config['daemon_mode'] = __daemon_mode
__config['no_cache'] = True
if __daemon_interval is not None:
__config['daemon_interval'] = __daemon_interval
if __daemon_pidfile is not None:
__config['daemon_pidfile'] = __daemon_pidfile
def verify_configuration():
logging.debug('verifying configuration ' + str(__config))
if __config['smtp_enable'] is True:
if 'smtp_from' not in __config:
raise Exception('smtp_from value is None')
if 'smtp_to' not in __config:
raise Exception('smtp_to value is None')
if 'smtp_server' not in __config:
raise Exception('smtp_server value is None')
if 'smtp_port' not in __config:
raise Exception('smtp_port value is None')
if 'exclude_file' in __config and __config['exclude_file'] is not None:
if not os.path.isfile(__config['exclude_file']):
raise Exception('exclude_file ' + __config['exclude_file'] + ' not found!')
global __exclusion_list
__exclusion_list = load_exclusion_file(__config['exclude_file'])
logging.debug('exclusion list: ' + str(__exclusion_list))
__config['__exclusion_list'] = __exclusion_list
if __daemon_mode is True and os.access(os.path.dirname(__config['daemon_pidfile']), os.W_OK) is not True:
logging.warning('cannot write to pidfile "' + __config['daemon_pidfile'] + '" switching to /tmp/sprinkle.pid')
__config['daemon_pidfile'] = '/tmp/sprinkle.pid'
def load_exclusion_file(exclude_file):
logging.debug('loading exclusion file ' + exclude_file)
lines = []
with open(exclude_file, 'r') as f:
for line in f:
line = line.strip()
line = line.replace('\\', '/')
lines.append(line)
f.close()
return lines
def init_logging(debug, daemon_mode=False):
if debug is True:
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.DEBUG,
filename=__log_file)
logging.getLogger('sprinkle').setLevel(logging.DEBUG)
else:
if daemon_mode is False:
logging.basicConfig(format='%(message)s',
level=logging.INFO,
filename=__log_file)
else:
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO,
filename=__log_file)
logging.getLogger('sprinkle').setLevel(logging.INFO)
def ls():
global __cl_sync
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) == 1:
logging.error('invalid ls command')
usage_ls()
sys.exit(-1)
files = __cl_sync.ls(common.remove_ending_slash(__args[1]))
largest_length = 25
keys = common.sort_dict_keys(files)
for tmp_file in keys:
filename_length = len(files[tmp_file].path)
if not files[tmp_file].is_dir and filename_length > largest_length:
largest_length = filename_length
common.print_line('---' + " " +
'NAME'.ljust(largest_length) + " " +
'SIZE'.rjust(9) + " " +
'MOD TIME'.ljust(19) + " " +
'REMOTE'
)
common.print_line('---' + " " +
''.join('-' for i in range(largest_length)) + " " +
''.join('-' for i in range(9)) + " " +
''.join('-' for i in range(19)) + " " +
''.join('-' for i in range(15))
)
for tmp_file in keys:
if files[tmp_file].is_dir is True:
first_chars = '-d-'
else:
first_chars = '---'
file_name = files[tmp_file].path
if file_name.startswith('//'):
file_name = file_name[1:len(file_name)]
common.print_line(first_chars + " " +
file_name.ljust(largest_length) + " " +
str(files[tmp_file].size).rjust(9) + " " +
common.get_printable_datetime(files[tmp_file].mod_time).ljust(19) + " " +
files[tmp_file].remote
)
def lsmd5():
global __cl_sync
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) == 1:
logging.error('invalid lsmd5 command')
usage_lsmd5()
sys.exit(-1)
files = __cl_sync.lsmd5(common.remove_ending_slash(__args[1]))
largest_length = 25
keys = common.sort_dict_keys(files)
for tmp_file in keys:
filename_length = len(tmp_file)
if filename_length > largest_length:
largest_length = filename_length
common.print_line('NAME'.ljust(largest_length) + " " +
'MD5'.ljust(32)
)
common.print_line(''.join('-' for i in range(largest_length)) + " " +
''.join('-' for i in range(32))
)
for tmp_file in keys:
file_name = tmp_file
common.print_line(file_name.ljust(largest_length) + " " +
files[tmp_file]
)
def backup():
global __cl_sync
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) == 1:
logging.error('invalid backup command')
usage_backup()
sys.exit(-1)
local_dir = common.remove_ending_slash(__args[1])
common.print_line('backing up ' + local_dir + '...')
__cl_sync.backup(local_dir, __config['delete_files'], __config['dry_run'])
def restore():
global __cl_sync
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) < 3:
logging.error('invalid remote command')
usage_restore()
sys.exit(-1)
remote_path = __args[2]
local_dir = common.remove_ending_slash(__args[1])
if __config['restore_duplicates'] is False:
common.print_line('checking if duplicates are present before restoring...')
duplicates = __cl_sync.remove_duplicates(local_dir, True)
if len(duplicates) > 0:
common.print_line('DUPLICATE FILES FOUND:')
for duplicate in duplicates:
common.print_line("\t" + duplicate)
common.print_line('restore cannot proceed! Use remove duplicates function before continuing')
return
common.print_line('restoring ' + remote_path + ' from ' + local_dir)
__cl_sync.restore(local_dir, remote_path, __config['dry_run'])
def stats():
global __cl_sync
logging.debug('display stats about the volumes')
common.print_line('calculating total and free space...')
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
common.print_line('REMOTE'.ljust(15) + " " +
'SIZE'.rjust(20) + " " +
'FREE'.rjust(20) + " " +
'%FREE'.rjust(10)
)
common.print_line(''.join('=' for i in range(15)) + " " +
''.join('=' for i in range(20)) + " " +
''.join('=' for i in range(20)) + " " +
''.join('=' for i in range(10))
)
sizes = __cl_sync.get_sizes()
frees = __cl_sync.get_frees()
display_unit = __config['display_unit']
for remote in sizes:
percent_use = frees[remote] * 100 / sizes[remote]
size_d = common.convert_unit(sizes[remote], display_unit)
free_d = common.convert_unit(frees[remote], display_unit)
common.print_line(remote.ljust(15) + " " +
"{:,}".format(size_d).rjust(19) + display_unit + " " +
"{:,}".format(free_d).rjust(19) + display_unit + " " +
"{:,}".format(int(percent_use)).rjust(10)
)
size = __cl_sync.get_size()
free = __cl_sync.get_free()
logging.debug('size: ' + "{:,}".format(size))
logging.debug('free: ' + "{:,}".format(free))
percent_use = free * 100 / size
common.print_line(''.join('-' for i in range(15)) + " " +
''.join('-' for i in range(20)) + " " +
''.join('-' for i in range(20)) + " " +
''.join('-' for i in range(10))
)
size_d = common.convert_unit(size, display_unit)
free_d = common.convert_unit(free, display_unit)
common.print_line("total:".ljust(15) + " " +
"{:,}".format(size_d).rjust(19) + display_unit + " " +
"{:,}".format(free_d).rjust(19) + display_unit + " " +
"{:,}".format(int(percent_use)).rjust(10)
)
def remove_duplicates():
global __cl_sync
common.print_line('removing duplicates')
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) == 1:
logging.error('invalid removedups command')
usage_removedups()
sys.exit(-1)
__cl_sync.remove_duplicates(common.remove_ending_slash(__args[1]))
def find():
global __cl_sync
if __cl_sync is None:
__cl_sync = clsync.ClSync(__config)
if len(__args) == 1:
logging.error('invalid find command')
usage_find()
sys.exit(-1)
files = __cl_sync.find(common.remove_ending_slash(__args[1]))
largest_length = 25
keys = common.sort_dict_keys(files)
for tmp_file in keys:
filename_length = len(files[tmp_file].path)
if not files[tmp_file].is_dir and filename_length > largest_length:
largest_length = filename_length
common.print_line('---' + " " +
'NAME'.ljust(largest_length) + " " +
'SIZE'.rjust(9) + " " +
'MOD TIME'.ljust(19) + " " +
'REMOTE'
)
common.print_line('---' + " " +
''.join('-' for i in range(largest_length)) + " " +
''.join('-' for i in range(9)) + " " +
''.join('-' for i in range(19)) + " " +
''.join('-' for i in range(15))
)
for tmp_file in keys:
if files[tmp_file].is_dir is True:
first_chars = '-d-'
else:
first_chars = '---'
file_name = files[tmp_file].path
if file_name.startswith('//'):
file_name = file_name[1:len(file_name)]
common.print_line(first_chars + " " +
file_name.ljust(largest_length) + " " +
str(files[tmp_file].size).rjust(9) + " " +
common.get_printable_datetime(files[tmp_file].mod_time).ljust(19) + " " +
files[tmp_file].remote
)
def check_single_instance():
if __single_instance is not None and __single_instance is True:
try:
lock.acquire(timeout=1)
except Timeout:
logging.error('sprinkle is running in another instance!')
sys.exit(-1)
def check_prerequisites():
logging.info('checking prerequisites, examine the error messages below...')
check_reault = True
try:
__cl_sync = clsync.ClSync(__config)
__cl_sync.get_remotes()
except:
check_reault = False
if check_reault is True:
logging.info('**** PASSED! ****')
else:
logging.info('**** FAILED! *****')
def main(argv):
read_args(argv)
configure(__configfile)
verify_configuration()
if __check_prereq is not None and __check_prereq is True:
check_prerequisites()
sys.exit(0)
check_single_instance()
logging.debug('config: ' + str(__config))
if __log_file is not None:
print('sprinkle is logging to file ' + __log_file + '...')
try:
if __args[0] == 'ls':
ls()
elif __args[0] == 'lsmd5':
lsmd5()
elif __args[0] == 'backup':
if __daemon_mode is True:
try:
sd = sprinkle_daemon.SprinkleDaemon(__config, __args[1])
sd.start()
except Exception as e:
logging.error('error starting daemon')
logging.error('error message: ' + str(e))
if __cmd_debug is True: