-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdistbackup.py
More file actions
968 lines (816 loc) · 29 KB
/
distbackup.py
File metadata and controls
968 lines (816 loc) · 29 KB
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
#! /usr/bin/python
#
# distbackup.py: script to backup your Linux distribution
#
# https://github.com/glatigny/distbackup
#
####################################################
import sys
import os
import stat
import re
import time
import shutil
import subprocess
import getopt
import datetime
import ConfigParser
import gzip
import ftplib
# import glob # Potential inclusion for cleanBackup
####################################################
#
#
#
class DatabaseBackup:
"""Backup a database.
Because there is several database driver, a class is used.
"""
@staticmethod
def process(settings, section):
"""Process the backup with the right database handler
"""
handler = settings.get(section, 'driver')
if not handler in ['mysql','pgsql','mongodb']:
return False
archive_format = 'gz'
output_folder = settings.get('default', 'output')
output_filename = settings.get(section, 'output') + '.' + archive_format
output_file = os.path.join(output_folder, output_filename)
# Call the right handler
syncMethod = getattr(DatabaseBackup, handler)
return syncMethod(settings, section, output_file)
@staticmethod
def mysql(settings, section, output_file):
"""Mysql backup handler
Should not be called directly
"""
params = ['mysqldump']
if settings.has_option(section, 'credentials'):
params.append('--defaults-file=' + settings.get(section, 'credentials'))
if settings.has_option(section, 'user'):
params.append('--user=' + settings.get(section, 'user'))
if settings.has_option(section, 'password'):
params.append('--password=' + settings.get(section, 'password'))
if settings.has_option(section, 'tables') and settings.has_option(section, 'database'):
params.append(settings.get(section, 'database'))
params = params + [x.strip(' ') for x in settings.get(section, 'tables').split(' ')]
elif not settings.has_option(section, 'database') or settings.get(section, 'database') == 'all':
params.append('-A')
else:
params.append('--databases')
params = params + settings.get(section, 'database').split(' ')
# Debug mode
if debug:
print DBG_MSG + "* MySQL dump -> " + output_file + DBG_MSG_END
return {'file': output_file}
# Processing the dump
mysqldump = subprocess.Popen(params, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dump_output = mysqldump.communicate()[0]
f = gzip.open(output_file, 'wb')
f.write(dump_output)
f.close()
return {
'file': output_file
}
@staticmethod
def pgsql(settings, section, output_file):
"""PostgreSQL backup handler
Should not be called directly
"""
params = ['mysqldump']
if not settings.has_option(section, 'database') or settings.get(section, 'database') == 'all':
params = ['pg_dumpall']
else:
params = ['pg_dump', settings.get(section, 'database')]
# Debug mode
if debug:
print DBG_MSG + "* PostgreSQL dump -> " + output_file + DBG_MSG_END
return {'file': output_file}
# Processing the dump
pgdump = subprocess.Popen(params, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dump_output = pgdump.communicate()[0]
f = gzip.open(output_file, 'wb')
f.write(dump_output)
f.close()
return {
'file': output_file
}
@staticmethod
def mongodb(settings, section):
"""MongoDB backup handler
Should not be called directly
"""
if not settings.has_option(section, 'database') or settings.get(section, 'database') == 'all':
params = ['mongodump', '--out', '-']
else:
params = ['mongodump', '--db', settings.get(section, 'database'), '--out', '-']
# Debug mode
if debug:
print DBG_MSG + "* MongoDB dump -> " + output_file + DBG_MSG_END
return {'file': output_file}
# Processing the dump
mongodump = subprocess.Popen(params, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dump_output = mongodump.communicate()[0]
f = gzip.open(output_file, 'wb')
f.write(dump_output)
f.close()
return {
'file': output_file
}
#
#
#
class SyncBackup:
"""Synchronization class
Allow to synchronize a file or a folder wiht another host.
Can handle different protocols, like FTP, Rsync.
It should also allow to dynamically crypt the data when sending it to the other host.
"""
@staticmethod
def process(settings, section, currentState):
"""Process the sync with the right protocol handler
"""
if not settings.has_option(section, 'protocol'):
return False
handler = settings.get(section, 'protocol')
if not handler in ['archive','copy','rsync','ftp']:
return False
# TODO : Manage group/file exclusion
# List processed files
files = []
for x in currentState:
if not x.has_key('ret') or not isinstance(x['ret'], dict):
continue
if not x['ret'].has_key('file') and not x['ret'].has_key('files'):
continue
file_section = x['section']
file_group = '/'
if settings.has_option(file_section, 'group'):
file_group = settings.get(file_section, 'group')
f = x['ret']['file'] if x['ret'].has_key('file') else x['ret']['files']
if isinstance(f, str):
files.append((f, file_section, file_group))
if isinstance(f, list):
files = files + [(x, file_section, file_group) for x in f]
# Call the right handler
syncMethod = getattr(SyncBackup, 'process' + handler.title())
return syncMethod(settings, section, files)
@staticmethod
def processArchive(settings, section, seqfiles):
"""Archive handler
The goal of that handler is to store the files in special folders and put a date on the copied files.
The organisation of the files should be done thanks to the processed files and the "group" in their configuration.
"""
# Get the "archive" folder (in 'default' section)
archive_folder = settings.get('default', 'archive')
# Get files organized in groups
groups = {
'/': []
}
for f in seqfiles:
filename = f[0]
group = f[2]
if not groups.has_key(group):
groups[group] = []
groups[group].append( filename )
# Copy the files in the different groups
date = datetime.datetime.now().strftime("%Y-%m-%d")
for group in groups:
for f in groups[group]:
if not debug and (not os.path.exists(f) or not os.path.isfile(f)):
continue
(filename, ext) = splitext(os.path.basename(f))
dest = os.path.join(archive_folder, group, filename + '_' + date + ext)
SyncBackup.copy(f, dest)
# If we do not want to display it in the report
return False
@staticmethod
def processCopy(settings, section, seqfiles):
"""Copy handler
It is a basic handler which will just copy files
"""
if not settings.has_option(section, 'dest'):
return False
dest = settings.get(section, 'dest')
# Debug mode
if debug:
print DBG_MSG + "* Copy -> " + dest + "\n" + "\n".join([x[0] for x in seqfiles]) + DBG_MSG_END
return True
# Check
if not os.path.exists(dest) or not os.path.is_dir(dest):
return False
# Performing the copy
ret = subprocess.call(['cp'] + [x[0] for x in seqfiles] + [dest])
return True
@staticmethod
def processRsync(settings, section, seqfiles):
"""Rsync handler
An handler which can transfer through network using rsync
"""
if not settings.has_option(section, 'host'):
return False
host = settings.get(section, 'host')
# Debug mode
if debug:
print DBG_MSG + "* Rsync -> " + host + "\n " + "\n ".join([x[0] for x in seqfiles]) + DBG_MSG_END
return True
# Performing the copy
ret = subprocess.call(['rsync', '-qt', '--chmod=o-rwx'] + [x[0] for x in seqfiles] + [host])
return True
@staticmethod
def processFtp(settings, section, seqfiles):
"""FTP handler
When rsync is not available...
"""
if not settings.has_option(section, 'host') or not settings.has_option(section, 'user') or not settings.has_option(section, 'password'):
return False
host = settings.get(section, 'host')
user = settings.get(section, 'user')
password = settings.get(section, 'password')
# Debug mode
if debug:
print DBG_MSG + "* FTP -> " + host + " (" + user + ")\n " + "\n ".join([x[0] for x in seqfiles]) + DBG_MSG_END
return True
session = ftplib.FTP(host, user, password)
for f in seqfiles:
filename = os.path.basename(f[0])
file = open(f[0], 'rb')
session.storbinary('STOR ' + filename, file)
file.close()
session.quit()
return True
@staticmethod
def copy(source, dest):
# Debug mode
if debug:
print DBG_MSG + "* Copy " + source + " -> " + dest + DBG_MSG_END
return True
# Performing the copy
ret = subprocess.call(['cp', source, dest])
return True
#
#
#
class SvnBackup:
"""Subversion Backup class
Allow to backup a SVN repository, with or without the 'svnadmin -hotcopy' folder
"""
@staticmethod
def process(settings, section):
# Check the folder
if not settings.has_option(section, 'folder'):
return False
folder = settings.get(section, 'folder')
if not os.path.exists(folder):
return False
archive_format = 'tar.gz'
archive_format_parameter = '--gzip' # --bzip2
output_folder = settings.get('default', 'output')
output_filename = settings.get(section, 'output') + '.' + archive_format
output_file = os.path.join(output_folder, output_filename)
# Create an hot-copy of the SVN in a temp folder
svn_hot_copy = False
svn_folder = folder
if settings.has_option(section, 'hot') and (settings.get(section, 'hot').lower().strip() == 'true'):
# Debug mode
if debug:
print DBG_MSG + "* Hotcopy archive SVN " + svn_folder + " -> " + output_file + DBG_MSG_END
return True
svn_hot_copy = True
svn_folder = SvnBackup.hotcopy(folder, settings, section)
# Parameters for the compression
params = [
'tar',
'--ignore-failed-read',
archive_format_parameter,
'-cf',
output_file,
'-C',
'/',
svn_folder.lstrip('/')
]
# Debug mode
if debug:
print DBG_MSG + "* Archive SVN " + svn_folder + " -> " + output_file + DBG_MSG_END
return { 'file': output_file }
# Processing the backup
ret = subprocess.call(params)
# Clean the temp SVN folder is needed
if folder != svn_folder:
if os.path.isdir(old_backup_item):
safe_rmtree(old_backup_item, 1)
else:
os.remove(old_backup_item)
#
return {
'file': output_file
}
@staticmethod
def hotcopy(folder, settings, section):
"""Create an hotcopy of a SVN repository
"""
output_folder = settings.get('default', 'output')
dest_folder = os.path.join(output_folder, 'svn-hot-copy')
err_code = subprocess.call([svnadmin, "hotcopy", folder, dest_folder, "--clean-logs"])
if err_code != 0:
return folder
return dest_folder
# For clearing away read-only directories (imported function)
@staticmethod
def safe_rmtree(dirname, retry=0):
"""Remove the tree at DIRNAME, making it writable first
"""
def rmtree(dirname):
SvnBackup.chmod_tree(dirname, 0666, 0666)
shutil.rmtree(dirname)
# Chmod recursively on a whole subtree
def chmod_tree(path, mode, mask):
for dirpath, dirs, files in os.walk(path):
for name in dirs + files:
fullname = os.path.join(dirpath, name)
if not os.path.islink(fullname):
new_mode = (os.stat(fullname)[stat.ST_MODE] & ~mask) | mode
os.chmod(fullname, new_mode)
# The function core
if not os.path.exists(dirname):
return
if retry:
for delay in (0.5, 1, 2, 4):
try:
rmtree(dirname)
break
except:
time.sleep(delay)
else:
rmtree(dirname)
else:
rmtree(dirname)
#
#
#
class ReportBackup:
"""Report class.
"""
@staticmethod
def process(settings, section):
"""Provide a report
"""
if not settings.has_option(section, 'report'):
return False
ret = []
handlers = settings.get(section, 'report').split(',')
for handler in handlers:
txt = False
if handler == 'text' or handler.startswith('text:'):
txt = ReportBackup.text(settings, section, handler)
elif handler == 'disk':
txt = ReportBackup.disk(settings, section)
if not txt == False:
ret.append(txt)
return "\r\n".join(ret)
@staticmethod
def text(settings, section, handler='text'):
"""Text report processing
"""
value = ''
if handler == 'text':
if not settings.has_option(section, 'value'):
return False
value = settings.get(section, 'value')
else:
key = handler[5:]
if not settings.has_option(section, key):
return False
value = settings.get(section, key)
value = getVars(value, settings, section)
# Return a string with some tiny processing before
return value.replace('\r\n', '\n').replace('\r', '\n').replace('\n_\n', '\n\n').strip('_').replace('\n', '\r\n')
@staticmethod
def disk(settings, section):
"""Disk usage report processing
"""
# Original linux cmd: df / /home -hP | column -t
if not settings.has_option(section, 'disks'):
disks = ['/']
else:
disks = settings.get(section, 'disks').split(',')
if len(disks) == 0:
return False
ret = "%-10s %8s %8s %8s %8s" % ( "Disk", "Size", "Used", "Avail", "Use%" )
for disk in disks:
try:
status = os.statvfs(disk.strip(' '))
except (OSError, IOError):
print disk
status = False
if status == False or status.f_blocks == 0:
continue
free = status.f_bfree * status.f_frsize
size = status.f_blocks * status.f_frsize
avail = status.f_bavail * status.f_frsize
used = (status.f_blocks - status.f_bfree) * status.f_frsize
used_percent = str( round(100. * used / size, 1) ) + '%'
ret += "\n%-10s %8s %8s %8s %8s" % ( disk, sizeof_fmt(size, ''), sizeof_fmt(used, ''), sizeof_fmt(avail, ''), used_percent )
return ret
#
#
#
def dirBackup(settings, section):
"""Backup a classical folder
"""
# Check the folder
if not settings.has_option(section, 'folder'):
return False
folder = settings.get(section, 'folder')
if not os.path.exists(folder):
return False
# Manage exclude list
excludes = []
if settings.has_option(section, 'exclude'):
excludes = [x.strip(' ') for x in settings.get(section, 'exclude').split(',')]
if len(excludes) == 0 and settings.has_option('default', 'exclude'):
excludes = [x.strip(' ') for x in settings.get('default', 'exclude').split(',')]
# Set archive format (improvement needed)
archive_format = 'tar.gz'
archive_format_parameter = '--gzip' # --bzip2
# Set the output files/folder
output_folder = settings.get('default', 'output')
output_filename = settings.get(section, 'output') + '.' + archive_format
output_file = os.path.join(output_folder, output_filename)
# Processing params
params = ['tar'] + [ ('--exclude="' + x + '"') for x in excludes ] + [
'--ignore-failed-read',
archive_format_parameter,
'-cf',
output_file,
'-C',
'/',
folder.lstrip('/')
]
if debug:
print DBG_MSG + "* Backup folder " + folder + " -> " + output_file + DBG_MSG_END
if len(excludes) > 0:
print DBG_MSG + " (exclude: " + ", ".join(excludes) + ")" + DBG_MSG_END
return {'file': output_file}
before_tar = datetime.datetime.now()
os.chdir('/')
ret = subprocess.call(params)
# We can have the backup duration
after_tar = datetime.datetime.now()
# Return if we do not have to create the info file
if (not settings.has_option(section, 'info') or (settings.get(section, 'info').lower().strip() != 'true')):
return {
'file': output_file
}
# Generation of the info file
info_file = output_file.replace('.'+archive_format, '.info.txt')
uname = os.uname()
hostname = uname[0] + '@' + uname[1]
md5_hash = generate_file_md5(output_folder, output_file)
file_content = '''
Directory backup :
Date : ''' + before_tar.strftime("%Y-%m-%d %H:%M") + '''
User : ''' + hostname + '''
Folder : ''' + folder + '''
MD5 : ''' + md5_hash + '''
'''
f = open(info_file, 'w')
f.write(file_content)
f.close()
# TODO
'''
find "$dir" -ls | grep -v ".svn" >> "$txt"
if [ -d "$dir/.svn" ]; then
echo >> "$txt"
echo "SVN Status" >> "$txt"
echo >> "$txt"
svn status -uv "$dir" >> "$txt"
fi
'''
return {
'files': [output_file, info_file]
}
#
#
#
def generate_file_md5(rootdir, filename, blocksize=2**20):
import hashlib
m = hashlib.md5()
try:
with open( os.path.join(rootdir, filename) , "rb" ) as f:
while True:
buf = f.read(blocksize)
if not buf:
break
m.update( buf )
except:
return '-- Hash Error --'
return m.hexdigest()
#
#
#
def cleanBackup(settings, section):
"""Clean Backup
"""
clean_days = 7
if settings.has_option(section, 'days'):
clean_days = int(settings.get(section, 'days').strip())
if clean_days <= 0:
clean_days = 7
keep_first_days = 0
if settings.has_option(section, 'keep_first_days'):
keep_first_days = int(settings.get(section, 'keep_first_days').strip())
base_date = datetime.datetime.today() - datetime.timedelta(days=clean_days)
now = datetime.datetime.now()
reg = re.compile(r'_(\d{4})-(\d{2})-(\d{2})\.', re.UNICODE)
archive_folder = settings.get('default', 'archive')
if settings.has_option(section, 'group'):
archive_folder = os.path.join(archive_folder, settings.get(section, 'group'))
for dirpath, dirs, files in os.walk(archive_folder):
for name in dirs + files:
fullname = os.path.join(dirpath, name)
if not os.path.isfile(fullname):
continue
mtime = 0
days = 0
try:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(fullname))
seconds = now - mtime
days, seconds = divmod(abs(int(seconds.total_seconds())), 86400)
except:
if debug:
print DBG_MSG + "* Error with file " + fullname + DBG_MSG_END
else:
print "Cannot access file " + fullname
# If the file is too recent or we couldn't get his "mtime"
if mtime == 0 or mtime >= base_date:
continue
# Test the file name to match the format criteria
match = reg.search(name)
if match == None:
continue
# Keep files on first day of each month
if keep_first_days < 0 and int(match.group(3)) == 1:
continue
# Limit the number of files for the first day of each month
if keep_first_days > 0 and int(match.group(3)) == 1 and keep_first_days > days:
continue
if debug:
print DBG_MSG + ("* Clean file %s (%d days) " % (fullname, days)) + DBG_MSG_END
else:
# Delete the file
try:
os.remove(fullname)
except:
print "Cannot delete file " + fullname
return False
#
#
#
def dpkgBackup(settings, section):
"""Dpkg Backup
It will generate a file with all installed packages.
This function is related to linux system using APT (Debian/Ubuntu)
"""
# Check the output variable
if not settings.has_option(section, 'output'):
return False
output = settings.get(section, 'output')
if debug:
print DBG_MSG + "* Create DPKG selection -> " + output + DBG_MSG_END
return True
# Call dpkg with pip support to write the output into a file
dpkg = subprocess.Popen(['dpkg', '--get-selections'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dpkg_output = dpkg.communicate()[0]
f = open(output, 'w')
f.write(dpkg_output)
f.close()
return True
#
#
#
def processBackup(settings, section, result=None):
"""Processing function
It read the type of the section and call the right handler (dir, svn, db...)
"""
if not settings.has_option(section, 'type'):
return 0
t = settings.get(section, 'type')
if t == 'folder' or t == 'dir':
return dirBackup(settings, section)
elif t == 'sync':
return SyncBackup.process(settings, section, result)
elif t == 'db' or t == 'database':
return DatabaseBackup.process(settings, section)
elif t == 'svn':
return SvnBackup.process(settings, section)
elif t == 'report':
return ReportBackup.process(settings, section)
elif t == 'clean':
return cleanBackup(settings, section)
elif t == 'dpkg':
return dpkgBackup(settings, section)
return 0
#
#
#
def folderSize(folder, suffix='B'):
total_size = 0
for root, dirs, files in os.walk(folder):
total_size += sum(os.path.getsize(os.path.join(root, name)) for name in files)
return sizeof_fmt(total_size, suffix)
#
#
#
def treeSize(folder, suffix='B'):
ret = ""
l = len(folder)
for root, dirs, files in os.walk(folder):
ret += folder if folder == root else " " + root[l:]
s = sum(os.path.getsize(os.path.join(root, name)) for name in files)
if s > 0:
ret += " [" + sizeof_fmt(s) + "]"
if len(files) > 0:
ret += " %d files" % len(files)
ret += "\n"
return ret.strip("\n")
#
#
#
def sizeof_fmt(num, suffix='B'):
"""Display function: Nice file size
"""
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Y', suffix)
#
#
#
def pretty_timedelta(data):
"""Display function: Nice time delta
"""
if isinstance(data, datetime.timedelta):
seconds = abs(int(data.total_seconds()))
else:
seconds = int(data)
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
if days > 0:
return '%dd %dh%dm%ds' % (days, hours, minutes, seconds)
elif hours > 0:
return '%dh%dm%ds' % (hours, minutes, seconds)
elif minutes > 0:
return '%dm%ds' % (minutes, seconds)
return '%ds' % (seconds,)
#
#
#
def splitext(path):
for ext in ['.tar.gz', '.tar.bz2']:
if path.endswith(ext):
return path[:-len(ext)], path[-len(ext):]
return os.path.splitext(path)
#
#
#
def getTextResult(settings, section, data):
"""Prepare some text for the final report
"""
if not settings.has_option(section, 'type'):
return 0
t = settings.get(section, 'type')
if t == 'report':
return data['ret']
elif t == 'dpkg':
return ""
if not t in ['folder', 'dir', 'db', 'database', 'svn', 'sync']:
return 0
# Name
name = ""
if settings.has_option(section, 'name'):
name = settings.get(section, 'name')
elif settings.has_option(section, 'folder'):
name = settings.get(section, 'folder')
# Duration
duration = " (" + pretty_timedelta(data['end'] - data['start']) + ")"
# Size
size = ""
if isinstance(data['ret'], dict) and (data['ret'].has_key('file') or data['ret'].has_key('files')):
files = data['ret']['file'] if data['ret'].has_key('file') else data['ret']['files']
totalsize = 0
if isinstance(files, str):
files = [files]
for f in files:
if os.path.exists(f) and os.path.isfile(f):
statinfo = os.stat(f)
totalsize += statinfo.st_size
if totalsize > 0:
size = " [" + sizeof_fmt(totalsize) + "]"
# Processing
if t == 'folder' or t == 'dir':
return "Backup folder \"" + name + "\"" + duration + size
elif t == 'db' or t == 'database':
return "Backup " + name + " database" + duration + size
elif t == 'svn':
return "Backup SVN repository \"" + name + "\"" + duration + size
elif t == 'sync' and name:
return "Synchronize with \"" + name + "\"" + duration + size
return 0
#
#
#
def getVars(text, settings, section):
"""Replace the special tags by their corresponding variable
"""
def getVar(key, settings, section):
"""Get variable content for text report
"""
# Formatted date time
if key == 'now':
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
# Direct link to datetime values
if key in ['year','month','day','hour','minute','second']:
return str(getattr(datetime.datetime.now(), key))
if key.startswith('size:') or key.startswith('tree:'):
folder = key[5:]
if folder == 'output':
folder = settings.get('default', 'output')
elif folder == 'archive':
folder = settings.get('default', 'archive')
if key.startswith('size:'):
return folderSize(folder)
return treeSize(folder)
return ""
#
reg = re.compile(r'{([\s\w\.\-\:\%]+)}', re.UNICODE)
for match in reg.finditer(text):
text = text.replace('{'+match.group(1)+'}', getVar(match.group(1), settings, section))
return text
#
#
#
def main(configFile = None):
"""Main function of the program
It will read the configuration file and process it.
It also retrieve the return of the processing to display the report at the end.
"""
if configFile == None:
configFile = '/etc/distbackup.cfg'
result = []
settings = ConfigParser.ConfigParser()
if settings.read(configFile) == []:
return False
for section in settings.sections():
if section == 'default':
continue
date_start = datetime.datetime.now()
# Perform the action
ret = processBackup(settings, section, result)
date_stop = datetime.datetime.now()
if ret != False and ret != 0:
data = {
'start': date_start,
'end': date_stop,
'section': section,
'ret': ret
}
display = getTextResult(settings, section, data)
if display != False and display != "" and display != 0:
print display
result.append( data )
return result
#
# Process arguments
#
try:
optlist, args = getopt.getopt(sys.argv[1:], 'c:', ['debug'])
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
# Configuration variables
configFile = None
debug = False
DBG_MSG = '\033[95m'
DBG_MSG_END = '\033[0m'
# Read parameters
for o, a in optlist:
if o == '-c':
configFile = a
elif o == "--debug":
debug = True
#
# Do the backup stuff
#
result = main(configFile)
if result == False:
sys.stderr.write("Configuration not found (" + configFile + ")\n")
sys.stderr.flush()
sys.exit(2)
if len(result) == 0:
sys.stderr.write("Nothing to do\n")
sys.stderr.flush()
sys.exit(1)