-
Notifications
You must be signed in to change notification settings - Fork 3
/
lb.py
1277 lines (1051 loc) · 42.2 KB
/
lb.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
"""Link-Backup
Copyright (c) 2004 Scott Ludwig
http://www.scottlu.com
Link-Backup is a backup utility that creates hard links between a series
of backed-up trees, and intelligently handles renames, moves, and
duplicate files without additional storage or transfer.
Transfer occurs over standard i/o locally or remotely between a client and
server instance of this script. Remote backups rely on the secure remote
shell program ssh.
viewlb.cgi, a simple web based viewer of backups made by link-backup, is
also available from the link-backup page.
http://www.scottlu.com/Content/Link-Backup.html
Usage:
lb [options] srcdir dstdir
lb [options] user@host:srcdir dstdir
lb [options] srcdir user@host:dstdir
Source or dest can be remote. Backups are dated with the following entries:
dstdir/YYYY.MM.DD-HH.MM:SS/tree/ backed up file tree
dstdir/YYYY.MM.DD-HH.MM:SS/log logfile
Options:
--verify Run rsync with --dry-run to cross-verify
--numeric-ids Keep uid/gid values instead of mapping; requires root
--minutes <mins> Only run for <mins> minutes. Incremental backup.
--showfiles Don't backup, only list relative path files needing
backup
--catalogonly Update catalog only
--filelist <- or file> Specify filelist. Files relative to srcdir.
--lock Ensure only one backup to a given dest will run at a time
--verbose Show what is happening
--ssh-i <file> Select id file to use for authentication (ssh -i)
--ssh-C Use ssh compression (ssh -C)
--ssh-p <port> ssh port on remote host (ssh -p)
Comments:
Link-Backup tracks unique file instances in a tree and creates a backup that
while identical in structure, ensures that no file is duplicated unnecessarily.
Files that are moved, renamed, or duplicated won't cause additional storage or
transfer. dstdir/.catalog is a catalog of all unique file instances; backup
trees hard-link to the catalog. If a backup tree would be identical to the
previous backup tree, it won't be needlessly created.
How it works:
The src sends a file list to the dst. First dst updates the catalog by checking
to see if it knows about each file. If not, the file is retrieved from the src
and a new catalog entry is made:
For each file:
1. Check to see if the file path + file stat is present in the last tree.
2. If not, ask for md5sum from the src. See if md5sum+stat is in the
catalog.
3. If not, see if md5sum only is in the catalog. If so copy catalog entry,
rename with md5sum+new stat
4. If not, request file from src, make new catalog entry.
Catalog files are named by md5sum+stats and stored in flat directories. Once
complete, a tree is created that mirrors the src by hardlinking to the catalog.
Example 1:
python lb.py pictures pictures-backup
Makes a new backup of pictures in pictures-backup.
Example 2:
python lb.py pictures me@fluffy:~/pictures-backup
Backs up on remote machine fluffy instead of locally.
Example 3:
python lb.py --minutes 240 pictures me@remote:~/pictures-backup
Same as above except for 240 minutes only. This is useful if backing up over
the internet only during specific times (at night for example). Does what it
can in 240 minutes. If the catalog update completes, a tree is created
hardlinked to the catalog.
Example 4:
python lb.py --showfiles pictures pictures-backup | \
python lb.py --filelist - pictures pictures-backup
Same as example #1.
Example 5:
1)
python lb.py --showfiles pictures me@remote:~/pictures-backup | \
python lb.py --filelist - pictures me@laptop:~/pictures-transfer
2)
python lb.py --catalogonly pictures-transfer me@remote:~/pictures-backup
3)
python lb.py pictures me@remote:~/pictures-backup
If the difference between pictures and pictures-backup (for example) is too
large for internet backup, the steps above can be used. Step 1 transfers only
the differences to a laptop. Step 2 is at the location of machine "remote" and
is initiated from the laptop to machine "remote". Step 3 is back at the source
and will do a backup and notice all the files are present in the remote catalog,
and will build the tree.
Note the source in step 2 could be more perfectly specified as the backup tree
created underneath the pictures-transfer directory, although it is not necessary
since only the catalog is being updated (however it would be a speedup).
History:
v 0.8 12/23/2006 scottlu
- allow backups to occur while files are changing
- minor --verify command bug
- added --verbose logging to tree building
v 0.7 09/02/2006 scottlu
- Ignore pipe, socket, and device file types
- Added --ssh-i to select ssh id file to use (see ssh -i) (Damien Mascord)
- Added --ssh-C to perform ssh compression (see ssh -C) (David Precious)
- Added --ssh-p to specify remote port (see ssh -p) (David Precious)
v 0.6 06/17/2006 scottlu
- Ignore broken symlinks and other failed stats during filelist creation
(David Precious)
- Added --lock, which ensures only one backup to a given dest can occur
at a time (Joe Beda)
v 0.5 04/15/2006 scottlu
- Added 'latest' link from Joe Beda http://eightypercent.net (thanks Joe!)
- Fixed --verify. It wasn't specifying the remote machine (I rarely use
verify but sometimes it is nice to sanity check backups)
v 0.4 11/14/2004 scottlu
- Changed a central catalog design with trees hardlinking to the catalog.
This way catalog updating can be incremental.
- Removed filemaps - not required any longer
- Add catalog logging as well as backup logging.
- Added incremental backup feature --minutes <minutes>
- Make md5hash calculation incremental so a timeout doesn't waste time
- Created 0.3-0.4.py for 0.3 to 0.4 upgrading
- Added --showfiles, shows differences between src and dst
- Added --catalogonly, updates catalog only, doesn't create tree
- Added --filelist, specifies file list to use instead of tree
- Removed --rmempty
- Added --verbose
v 0.3 9/10/2004 scottlu
- Added backup stat query methods
- Changed log file format
- Added viewlb.cgi, a web interface for viewing backups
- added gzip compression of filemap
- added --numeric-ids
v 0.2 8/28/2004 scottlu
- filemap format change
- added --rmempty
- added --verify to run rsync in verify mode
- added uid/gid mapping by default unless --numeric-ids is specified
v 0.1 8/19/2004 scottlu
- Fully working backup, hardlinking between trees
License:
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.
"""
import os
import sys
import cPickle
from os.path import join
import time
import stat
import md5
import shutil
import tempfile
import struct
import re
import glob
import fcntl
fd_send = None
fd_recv = None
pickler = None
unpickler = None
date_format = '%Y.%m.%d-%H.%M.%S'
MODE = 0
SIZE = 1
MTIME = 2
UID = 3
GID = 4
CHMOD_BITS = int('6777', 8)
def send_object(object):
global pickler, fd_send
pickler.dump(object)
fd_send.flush()
def recv_object():
global unpickler
return unpickler.load()
def init_io(send, recv):
global fd_send, fd_recv, pickler, unpickler
fd_send = send
fd_recv = recv
pickler = cPickle.Pickler(fd_send, 1)
unpickler = cPickle.Unpickler(fd_recv)
def verbose_log(s):
if have_option('--verbose'):
sys.stderr.write('%s\n' % s)
class Log:
def __init__(self, logfile, mode):
self.mode = mode
try:
self.logfile = file(os.path.abspath(logfile), self.mode)
except:
self.logfile = None
self.re = re.compile(r'^(?P<time>....\...\...\-..\...\...)\: (?P<message>.*)$')
def __del__(self):
if self.logfile:
self.logfile.close()
def write(self, message):
if not self.logfile or self.mode == 'rt':
return
try:
strtime = time.strftime(date_format, time.localtime())
self.logfile.write('%s: %s\n' % (strtime, message))
self.logfile.flush()
except:
pass
def nextline(self):
if not self.logfile or self.mode == 'at':
return
line = self.logfile.readline()
if len(line) == 0:
return None
m = self.re.match(line)
return (time.strptime(m.group('time'), date_format), m.group('message'))
class Catalog:
"""Central store for files of different hash/stat combinations
Backup trees hard link to the catalog. The catalog can be updated
incrementally. A backup tree is not created until the catalog is
up to date.
"""
def __init__(self, path):
self.path = os.path.abspath(path)
self.lenbase = len('%s%s' % (self.path, os.sep))
self.logpath = join(self.path, 'log')
if not os.path.exists(self.path):
os.mkdir(self.path)
os.mkdir(self.logpath)
for n in xrange(256):
os.mkdir(join(self.path, '%03d' % n))
def get_logfiles(self):
list = []
for item in os.listdir(self.logpath):
s = os.stat(join(self.logpath, item))
if stat.S_ISDIR(s.st_mode):
continue
try:
datestr = item.rstrip('.log')
time.strptime(datestr, date_format)
list.append(datestr)
except:
pass
list.sort()
return [(time.strptime(datestr, date_format), join(self.logpath, '%s.log' % datestr)) for datestr in list]
def parse_log(self, logpath_abs):
log = Log(logpath_abs, 'rt')
parse = []
while True:
line = log.nextline()
if line == None:
break
elif line[1].startswith('+++'):
continue
elif line[1].startswith('copy from: '):
tT = line[0]
fromT = line[1][11:]
forT = ''
line = log.nextline()
if line[1].startswith('copy for: '):
forT = line[1][10:]
parse.append(('copy', tT, fromT, forT))
elif line[1].startswith('new from: '):
tT = line[0]
fromT = line[1][10:]
forT = ''
line = log.nextline()
if line[1].startswith('new for: '):
forT = line[1][9:]
parse.append(('new', tT, fromT, forT))
return parse
def file_from_hash(self, md5):
subdir = join(self.path, '%03d' % (hash(md5) & 255))
files = glob.glob1(subdir, '%s*' % md5)
if len(files) > 0:
return join(subdir, files[0])
return None
def file_from_hashstat(self, md5, s):
filepath_abs = self.getfilepath(md5, s)
if os.path.exists(filepath_abs):
return filepath_abs
return None
def getfilepath(self, md5, s):
mdate = time.strftime(date_format, time.localtime(s[MTIME]))
fn = '%s-%s-%08x-%05x-%04d-%04d' % (md5, mdate, s[SIZE], s[MODE] & CHMOD_BITS, s[UID], s[GID])
return join(join(self.path, '%03d' % (hash(md5) & 255)), fn)
def update(self, filelist, treepath_last, end_time):
# This is the slow (and expensive!) bandwidth eating portion
# of link-backup. If --minutes is specified, don't go beyond
# the minutes specified.
# For each file see if exists in the catalog; if not copy it
# if the md5 exists or download it
datestr = time.strftime(date_format, time.localtime())
log = Log(join(self.logpath, '%s.log' % datestr), 'wt')
dl_seconds = 0
dl_size = 0
md5hashes = [None for n in xrange(len(filelist))]
log.write('+++begin+++')
for n in xrange(len(filelist)):
# Only files
filepath_rel, s = filelist[n]
if stat.S_ISDIR(s[MODE]):
continue
# If stat equal we don't need a hash for this file
if treepath_last and is_stat_equal(join(treepath_last, filepath_rel), s):
verbose_log('dst: found file %s' % filelist[n][0])
continue
# Get the md5hash for this file
verbose_log('dst: request hash for %s' % filelist[n][0])
send_object(n)
md5hashes[n] = recv_object()
if not md5hashes[n]:
verbose_log('dst: did not receive hash?')
send_object(False)
continue
# File already present? Skip.
if self.file_from_hashstat(md5hashes[n], s):
verbose_log('dst: file present already %s' % filelist[n][0])
send_object(False)
continue
# File not present. Copy locally or from the source
fd, tmpfilepath_abs = tempfile.mkstemp(dir=self.path)
filepath_abs = self.getfilepath(md5hashes[n], s)
try:
copyfile_abs = self.file_from_hash(md5hashes[n])
if copyfile_abs:
# Found same file with different stats. Requires a copy
verbose_log('dst: using file with same hash %s' % filelist[n][0])
send_object(False)
shutil.copyfile(copyfile_abs, tmpfilepath_abs)
log.write('copy from: %s' % filepath_abs[self.lenbase:])
log.write('copy for: %s' % filepath_rel)
else:
# Enough time for this file?
if end_time != 0 and dl_seconds != 0:
est_seconds = s[SIZE] / (dl_size / dl_seconds)
if time.time() + est_seconds >= end_time:
verbose_log('dst: timeout')
send_object(False)
raise
# Time downloads to understand average download rate and use as
# an estimator of a given file's download time
verbose_log('dst: requesting file %s' % filelist[n][0])
dl_time_start = time.time()
# Copy from source
# The chunks are sized independent from stats for robustness
# Stat is resent to have most up to date copy
# Recalc the md5 hash along the way so it is right
send_object(True)
m = md5.new()
while True:
readcount = struct.unpack('!i', fd_recv.read(4))[0]
if readcount == 0:
break
if readcount < 0:
raise 'Error reading file'
bytes = fd_recv.read(readcount)
m.update(bytes)
os.write(fd, bytes)
# Delta accumulator
dl_seconds += time.time() - dl_time_start
os.fsync(fd)
dl_size += os.fstat(fd).st_size
# File might of changed during the update
# Update has and size and check to see if it already
# exists in the catalog
if md5hashes[n] != m.hexdigest():
verbose_log('dst: file changed during copy %s' % filelist[n][0])
md5hashes[n] = m.hexdigest()
s[SIZE] = os.stat(fd).st_size
if self.file_from_hashstat(md5hashes[n], s):
verbose_log('dst: file already in catalog %s' % filelist[n][0])
os.close(fd)
os.remove(tempfilepath_abs)
continue
log.write('new from: %s' % filepath_abs[self.lenbase:])
log.write('new for: %s' % filepath_rel)
except:
os.close(fd)
os.remove(tmpfilepath_abs)
send_object(-1)
log.write('+++end+++')
return False, dl_size, md5hashes
# Rename and set file stats
os.close(fd)
os.utime(tmpfilepath_abs, (s[MTIME], s[MTIME]))
os.chown(tmpfilepath_abs, s[UID], s[GID])
os.rename(tmpfilepath_abs, filepath_abs)
os.chmod(filepath_abs, s[MODE] & CHMOD_BITS)
# Done with file requests
verbose_log('dst: catalog update done')
send_object(-1)
log.write('+++end+++')
return True, dl_size, md5hashes
def get_showfiles(self, filelist, treepath_last):
# Get hashes for new files. If file doesn't exist in old backup with same
# stat, we need ask the client for a hash
md5requests = []
for n in xrange(len(filelist)):
# Only files
filepath_rel, s = filelist[n]
if stat.S_ISDIR(s[MODE]):
continue
# If stat equal we don't need a hash for this file
if treepath_last and is_stat_equal(join(treepath_last, filepath_rel), s):
continue
# Need hash for this file
md5requests.append(n)
# Retrieve hashes
send_object(md5requests)
md5hashes = recv_object()
if len(md5hashes) != len(md5requests):
raise AssertionError, 'Hash count mismatch'
# Make one sorted list to eliminate duplicates
# Check if already present in catalog
md5sort = [(md5requests[n], md5hashes[n]) for n in xrange(len(md5hashes)) if not self.file_from_hash(md5hashes[n])]
def sortme(a, b):
if a[1] == b[1]:
return 0
if a[1] > b[1]:
return 1
return -1
md5sort.sort(sortme)
# Eliminate duplicates and return
showfiles = []
md5 = None
for n in xrange(len(md5sort)):
if md5 == md5sort[n][1]:
continue
md5 = md5sort[n][1]
showfiles.append(md5sort[n][0])
return showfiles
# Backup
class Backup:
"""Represents a dated backup.
"""
def __init__(self, path):
self.path = os.path.abspath(path)
self.logpath_abs = join(self.path, 'log')
self.treepath = join(self.path, 'tree')
if not os.path.exists(self.treepath):
os.mkdir(self.treepath)
def parse_log(self):
log = Log(self.logpath_abs, 'rt')
parse = []
while True:
line = log.nextline()
if line == None:
break
if line[1] == '+++end+++' or line[1] == '+++begin+++':
continue
if line[1].startswith('new: '):
parse.append(('new', line[1][5:]))
elif line[1].startswith('copy: '):
parse.append(('copy', line[1][6:]))
elif line[1].startswith('link: '):
parse.append(('link', line[1][6:]))
return parse
def get_date(self):
return time.strptime(self.get_dirname(), date_format)
def get_dirname(self):
return os.path.basename(self.path)
def get_treepath(self):
return self.treepath
def get_files_since(self, backup_last, catalog):
# Get files added to the catalog since last tree was built
tlast = 0
if backup_last:
tlast = time.mktime(backup_last.get_date())
filessince = {}
for tm, logfile_abs in catalog.get_logfiles():
if time.mktime(tm) < tlast:
continue
for item in catalog.parse_log(logfile_abs):
filessince[item[3]] = item[0]
return filessince
def build_tree(self, backup_last, filelist, md5hashes, catalog):
"""All files are present and can be found either in the
previous backup or the catalog. Just build the structure.
"""
treepath_last = None
if backup_last:
treepath_last = backup_last.get_treepath()
filessince = self.get_files_since(backup_last, catalog)
log = Log(self.logpath_abs, 'at')
log.write('+++begin+++')
verbose_log('dst: creating tree %s' % self.treepath)
# Create directories (they are in depth last order)
# Set permissions later
verbose_log('dst: making directories...')
for filepath_rel, s in filelist:
if stat.S_ISDIR(s[MODE]):
verbose_log('dst: making dir %s' % filepath_rel)
dirpath_abs = join(self.treepath, filepath_rel)
os.mkdir(dirpath_abs)
# Link in files
verbose_log('dst: linking files...')
for n in xrange(len(filelist)):
# Skip dirs
filepath_rel, s = filelist[n]
if stat.S_ISDIR(s[MODE]):
continue
verbose_log('dst: inspecting file %s' % filepath_rel)
# If there is no hash, it's in the last backup, otherwise it's
# in the catalog
if not md5hashes[n]:
verbose_log('dst: found in last backup: %s' % filepath_rel)
linkpath_abs = join(treepath_last, filepath_rel)
else:
verbose_log('dst: found in catalog: %s' % filepath_rel)
linkpath_abs = catalog.file_from_hashstat(md5hashes[n], s)
# Only log files new to the catalog since last tree. This
# ensures file renames, dups, moves etc don't show up as new
# in the tree log
if filessince.has_key(filepath_rel):
log.write('%s: %s' % (filessince[filepath_rel], filepath_rel))
else:
log.write('link: %s' % filepath_rel)
# Hard-link the file
verbose_log('dst: hardlinking %s to %s' % (join(self.treepath, filepath_rel), linkpath_abs))
os.link(linkpath_abs, join(self.treepath, filepath_rel))
# Set permissions for directories depth-first.
verbose_log('dst: setting permissions on directories...')
for n in xrange(len(filelist) - 1, -1, -1):
dirpath_rel, s = filelist[n]
if stat.S_ISDIR(s[MODE]):
verbose_log('dst: setting permissions on: %s' % dirpath_rel)
dirpath_abs = join(self.treepath, dirpath_rel)
os.utime(dirpath_abs, (s[MTIME], s[MTIME]))
os.chown(dirpath_abs, s[UID], s[GID])
os.chmod(dirpath_abs, s[MODE] & CHMOD_BITS)
verbose_log('dst: done creating tree %s' % self.treepath)
log.write('+++end+++')
# Manager
class Manager:
"""Manages Backup instances
"""
def __init__(self, path):
self.path = os.path.abspath(path)
if not os.path.exists(self.path):
os.mkdir(self.path)
self.catalog = Catalog(join(self.path, '.catalog'))
def get_path(self):
return self.path
def new_backup(self):
dirpath = join(self.path, time.strftime(date_format, time.localtime()))
os.mkdir(dirpath)
return Backup(dirpath)
def delete_backup(self, backup):
dirpath_abs = join(self.path, backup.get_dirname())
if os.path.exists(dirpath_abs):
for root, dirs, files in os.walk(dirpath_abs, topdown=False):
for name in files:
os.remove(join(root, name))
for name in dirs:
os.rmdir(join(root, name))
os.rmdir(dirpath_abs)
def get_backup(self, backup):
return Backup(join(self.path, backup))
def get_backups(self):
list = []
for item in os.listdir(self.path):
s = os.stat(join(self.path, item))
if not stat.S_ISDIR(s.st_mode):
continue
try:
time.strptime(item, date_format)
list.append(item)
except:
pass
list.sort()
return [Backup(join(self.path, item)) for item in list]
# Helpers
def start_server(src, dst, is_source):
# Command line for server
cmd1 = "python -c 'import sys;import cPickle;exec(cPickle.Unpickler(sys.stdin).load())' --server"
if is_source:
cmd1 = "%s --source" % cmd1
for arg in sys.argv[1:-2]:
cmd1 = '%s %s' % (cmd1, arg)
cmd1 = "%s %s %s" % (cmd1, src['string'], dst['string'])
# Remote?
addr = dst
if is_source:
addr = src
# Add ssh and args if remote
if addr['remote']:
ssh_args = '%s \"%s\"' % (addr['remote'], cmd1)
if have_option('--ssh-p'):
ssh_args = '-p %s %s' % (get_option_value('--ssh-p'), ssh_args)
if have_option('--ssh-i'):
ssh_args = '-i %s %s' % (get_option_value('--ssh-i'), ssh_args)
if have_option('--ssh-C'):
ssh_args = '-C %s' % ssh_args
cmd2 = 'ssh %s' % ssh_args
else:
cmd2 = cmd1
# Start and pass this code
verbose_log('command: %s' % cmd2)
fdin, fdout = os.popen2(cmd2, mode='b')
init_io(fdin, fdout)
f = open(sys.argv[0])
send_object(f.read())
f.close()
def is_mode_ok(mode):
if stat.S_ISBLK(mode):
return False
if stat.S_ISCHR(mode):
return False
if stat.S_ISFIFO(mode):
return False
if stat.S_ISSOCK(mode):
return False
return True
def build_filelist_from_tree(treepath):
class ListBuilder:
def __init__(self, basepath):
self.lenbase = len('%s%s' % (basepath, os.sep))
def callback(self, arg, dirpath, filelist):
for file in filelist:
# Sometimes a stat may fail, like if there are broken
# symlinks in the file system
try:
# Collect stat values instead of stat objects. It's 6
# times smaller (measured) and mutuable
# (for uid/gid mapping at the dest)
filepath = join(dirpath, file)
s = os.stat(filepath)
if not is_mode_ok(s.st_mode):
continue
arg.append((filepath[self.lenbase:], [s.st_mode, s.st_size, s.st_mtime, s.st_uid, s.st_gid]))
except:
pass
treepath_abs = os.path.abspath(treepath)
filelist = []
os.path.walk(treepath_abs, ListBuilder(treepath_abs).callback, filelist)
return filelist
def build_filelist_from_file(treepath, file):
filelist = []
for line in file.readlines():
filepath_rel = line.rstrip('\n')
s = os.stat(join(treepath, filepath_rel))
if not is_mode_ok(s.st_mode):
continue
filelist.append((filepath_rel, [s.st_mode, s.st_size, s.st_mtime, s.st_uid, s.st_gid]))
return filelist
def build_filelist(treepath):
verbose_log('building filelist...')
for n in xrange(len(sys.argv)):
if sys.argv[n] == '--filelist':
if sys.argv[n + 1] == '-':
return build_filelist_from_file(treepath, sys.stdin)
else:
file = open(sys.argv[n + 1])
filelist = build_filelist_from_file(treepath, file)
file.close()
return filelist
return build_filelist_from_tree(treepath)
def build_uidgidmap(filelist):
"""Build a map of uid's to names and gid's to names
so mapping can occur at the destination
"""
import pwd
import grp
uidname_map = {}
gidname_map = {}
for filepath_rel, s in filelist:
if not uidname_map.has_key(s[UID]):
try:
uidname_map[s[UID]] = pwd.getpwuid(s[UID])[0]
except:
uidname_map[s[UID]] = str(s[UID])
if not gidname_map.has_key(s[GID]):
try:
gidname_map[s[GID]] = grp.getgrgid(s[GID])[0]
except:
gidname_map[s[GID]] = str(s[GID])
return uidname_map, gidname_map
def map_uidgid(filelist, idname_map):
"""Fix up uid / gid to dest values
"""
# If root and --numeric-ids specified, keep the numeric
# ids
if os.getuid() == 0 and have_option('--numeric-ids'):
return
# First build a uid->uid map. If not root, valid
# uid mapping is only current user. If root, attempt
# to map uid, if that fails keep the uid.
import pwd
import grp
uid_user = os.getuid()
uidname_map = idname_map[0]
uiduid_map = {}
for uid_source in uidname_map.keys():
if uid_user == 0:
try:
uid_dest = pwd.getpwnam(uidname_map[uid_source])[2]
uiduid_map[uid_source] = uid_dest
except:
uiduid_map[uid_source] = uid_source
else:
uiduid_map[uid_source] = uid_user
# Build gid->gid map. If not root, valid gid mapping is any group
# this user is a part of. First build a list of valid name->gids
# mappings
gid_user = os.getgid()
gid_name = grp.getgrgid(gid_user)[0]
namegid_map = {}
for group in grp.getgrall():
if uid_user == 0 or gid_name in group[3]:
namegid_map[group[0]] = group[2]
# Now build a gid map to valid gids for this user
gidname_map = idname_map[1]
gidgid_map = {}
for gid_source in gidname_map.keys():
gid_sourcename = gidname_map[gid_source]
if namegid_map.has_key(gid_sourcename):
gidgid_map[gid_source] = namegid_map[gid_sourcename]
else:
gidgid_map[gid_source] = gid_user
# Now map filelist entries
for filepath_rel, s in filelist:
# Continue if nothing to do. Unlikely in the mapping case
if uiduid_map[s[UID]] == s[UID] and gidgid_map[s[GID]] == s[GID]:
continue
# Map entries
s[UID] = uiduid_map[s[UID]]
s[GID] = gidgid_map[s[GID]]
def serve_files(treepath, filelist):
"""Serve requested files.
"""
global fd_recv
while True:
# Which file?
n = recv_object()
if n == -1:
break
# Calc hash and return it
verbose_log('src: calc hash for %s' % filelist[n][0])
filepath_rel, s = filelist[n]
filepath_abs = join(treepath, filepath_rel)
try:
f = open(filepath_abs)
m = md5.new()
while True:
bytes = f.read(1024 * 1024)
if len(bytes) == 0:
break
m.update(bytes)
f.close()
send_object(m.hexdigest())
except:
verbose_log('src: error calcing hash for %s' % filelist[n][0])
send_object(None)
# False means don't need the file
if not recv_object():
verbose_log('src: skipping file %s' % filelist[n][0])
continue
# Send size with data chunks in case the file is changing
# while this occurs
verbose_log('src: sending file %s' % filelist[n][0])
try:
f = open(filepath_abs)
while True:
bytes = f.read(1024 * 1024)
fd_send.write(struct.pack('!i', len(bytes)))
if len(bytes) == 0:
break
fd_send.write(bytes)
fd_send.flush()
f.close()
except:
verbose_log('src: error sending file %s' % filelist[n][0])
fd_send.write(struct.pack('!i', -1))
verbose_log('src: send complete %s' % filelist[n][0])
def serve_hashes(treepath, filelist):
"""Serve requested hashes
"""
hashrequests = recv_object()
hashlist = []
for n in xrange(len(hashrequests)):
filepath_rel, s = filelist[hashrequests[n]]
filepath_abs = join(treepath, filepath_rel)
f = open(filepath_abs)
m = md5.new()
while True:
bytes = f.read(1024 * 1024)
if len(bytes) == 0:
break
m.update(bytes)
f.close()
hashlist.append(m.hexdigest())
send_object(hashlist)
def is_stat_equal(filepath_abs, s):
try:
s2 = os.stat(filepath_abs)
if (s[MODE] & CHMOD_BITS) == (s2.st_mode & CHMOD_BITS) and s[SIZE] == s2.st_size and s[MTIME] == s2.st_mtime and s[UID] == s2.st_uid and s[GID] == s2.st_gid:
return True
except:
pass
return False
def is_tree_equal(filelist, treepath_last):
verbose_log('checking for need to build tree...')
if not treepath_last:
verbose_log('tree not equal: no last tree!')
return False
filelist_old = build_filelist_from_tree(treepath_last)
if len(filelist) != len(filelist_old):
verbose_log('tree not equal: filelists different sizes!')
return False
dict_new = dict(filelist)
dict_old = dict(filelist_old)
for key in dict_new.keys():
different = False
if not dict_old.has_key(key):
different = True
else:
s_old = dict_old[key]
s_new = dict_new[key]
different = False
if stat.S_ISDIR(s_old[MODE]):
if s_old[MODE] != s_new[MODE] or s_old[MTIME] != s_new[MTIME] or s_old[UID] != s_new[UID] or s_old[GID] != s_new[GID]:
different = True
else:
if s_old != s_new:
different = True
if different:
verbose_log('tree not equal: stats different %s' % key)
if dict_old.has_key(key):
verbose_log('old %s' % str(dict_old[key]))
verbose_log('new %s' % str(dict_new[key]))