17bd3318208b8ce01e6363c68a628e15a80a55a7
2 """Stefan Huber's simplistic backup solution."""
12 Mode
= ["full", "incr", "diff"]
15 "hour" : datetime
.timedelta(0, 3600), \
16 "day" : datetime
.timedelta(1), \
17 "week" : datetime
.timedelta(7), \
18 "month" : datetime
.timedelta(30), \
19 "year" : datetime
.timedelta(365) }
21 Epoch
= dict(RealEpoch
, **{ \
22 "sporadic" : datetime
.timedelta(0,0) \
27 """A single backup has a date, an epoch and a mode."""
29 def __init__(self
, date
, epoch
, mode
):
35 def fromDirName(dirname
):
36 [strdate
, strtime
, epoch
, mode
] = dirname
.split("-")
38 if not epoch
in Epoch
.keys():
39 raise ValueError("Invalid epoch: " + epoch
)
42 raise ValueError("Invalid mode: " + mode
)
44 date
= datetime
.datetime(int(strdate
[0:4]),
45 int(strdate
[4:6]), int(strdate
[6:8]),\
46 int(strtime
[0:2]), int(strtime
[2:4]))
48 return Backup(date
, epoch
, mode
)
51 return "[date: " + self
.date
.ctime() + \
52 ", epoch: " + self
.epoch
+ \
53 ", mode: " + self
.mode
+ "]"
55 def colAlignedString(self
):
56 return "%16s %8s %4s" % ( \
57 self
.date
.strftime("%Y-%m-%d %H:%M"), self
.epoch
, self
.mode
)
60 def getDirName(date
, epoch
, mode
):
61 """Get directory name of backup by given properties."""
62 return date
.strftime("%Y%m%d-%H%M") + "-" + epoch
+ "-" + mode
65 def isBackupDir(dirname
):
66 """Is directory a backup directory?"""
67 p
= re
.compile(r
'^\d\d\d\d\d\d\d\d-\d\d\d\d-\w+-\w+$')
68 return p
.match(dirname
)
73 """Encapsules the configuration for the backup program."""
75 class ReadError(RuntimeError):
76 """An exception raised when reading configurations."""
77 def __init__(self
, value
):
82 """A fileset has a name and a list of directories."""
83 def __init__(self
, name
, dirs
):
88 return "[name: " + self
.name
+ ", dirs: " + str(self
.dirs
) + "]"
90 formats
= ["tar", "tar.gz", "tar.bz2", "tar.xz" ]
92 # Filename where checksum of config is saved
93 checksumfn
= "checksum"
96 self
.directory
= "/media/backup"
97 self
.format
= self
.formats
[0]
98 self
.epochkeeps
= { k
: 0 for k
in RealEpoch
.keys() }
99 self
.epochmodes
= { k
: "full" for k
in RealEpoch
.keys() }
100 self
.exclpatterns
= []
103 self
.lastchecksum
= None
106 return "[directory: " + self
.directory
+ \
107 ", format: " + self
.format
+ \
108 ", keeps: " + str(self
.epochkeeps
) + \
109 ", modes: " + str(self
.epochmodes
) + \
110 ", exclpatterns: " + str(self
.exclpatterns
) + \
111 ", sets: " + str([str(s
) for s
in self
.sets
]) + "]"
113 def read(self
, filename
):
114 """Read configuration from file"""
116 if not os
.path
.isfile(filename
):
117 raise Config
.ReadError("Cannot read config file '" + filename
+ "'.")
119 config
= configparser
.RawConfigParser()
120 config
.read(filename
)
122 for reqsec
in ["destination"]:
123 if not config
.has_section(reqsec
):
124 raise Config
.ReadError("Section '" + reqsec
+ "' is missing.")
126 self
.directory
= config
.get("destination", "directory")
127 if not os
.path
.isdir(self
.directory
):
128 raise Config
.ReadError("Directory '{0}' does not exist.".format(self
.directory
))
130 self
.format
= config
.get("destination", "format")
131 if not self
.format
in Config
.formats
:
132 raise Config
.ReadError("Invalid 'format' given.")
135 if config
.has_section("history"):
136 for opt
in config
.options("history"):
137 if opt
.startswith("keep"):
139 if not epoch
in RealEpoch
.keys():
140 raise Config
.ReadError("Invalid option 'keep" + epoch
+ "'.")
142 self
.epochkeeps
[epoch
] = int(config
.getint("history", opt
))
144 raise Config
.ReadError("Invalid integer given for '" + opt
+ "'.")
145 elif opt
.startswith("mode"):
147 if not epoch
in RealEpoch
.keys():
148 raise Config
.ReadError("Invalid option 'mode" + epoch
+ "'.")
149 self
.epochmodes
[epoch
] = config
.get("history", opt
)
150 if not self
.epochmodes
[epoch
] in Mode
:
151 raise Config
.ReadError("Invalid mode given.")
153 raise Config
.ReadError("Invalid option '" + opt
+ "'.")
155 if config
.has_section("input"):
156 for opt
in config
.options("input"):
157 if opt
.startswith("exclude"):
158 self
.exclpatterns
+= [ config
.get("input", opt
) ]
160 raise Config
.ReadError("Invalid option '" + opt
+ "'.")
162 for sec
in config
.sections():
163 if sec
in ["destination", "history", "input"]:
165 elif sec
.startswith("set "):
166 name
= sec
[4:].strip()
169 for opt
in config
.options(sec
):
170 if not opt
.startswith("dir"):
171 raise Config
.ReadError("Unknown option '" + opt
+ "'.")
173 dirs
+= [config
.get(sec
, opt
)]
174 self
.sets
+= [Config
.FileSet(name
, dirs
)]
176 raise Config
.ReadError("Unknown section '" + sec
+ "'.")
178 # Compute checksum of config file
180 f
= open(filename
, 'rb')
183 self
.checksum
= m
.hexdigest()
188 f
= open(os
.path
.join(self
.directory
, self
.checksumfn
), 'r')
189 self
.lastchecksum
= f
.read().strip()
192 self
.lastchecksum
= None
196 """List and create backups"""
198 def __init__(self
, conffn
, alwaysyes
):
200 self
.alwaysyes
= alwaysyes
201 self
.conf
.read(conffn
)
204 def listAllDirs(self
):
205 """List all dirs in destination directory"""
208 basedir
= self
.conf
.directory
209 dirs
= os
.listdir(basedir
)
211 return [ d
for d
in dirs
if os
.path
.isdir(os
.path
.join(basedir
, d
)) ]
214 def listOldBackups(self
):
215 """Returns a list of old backups."""
219 for entry
in [ b
for b
in self
.listAllDirs() if Backup
.isBackupDir(b
) ]:
220 backups
+= [ Backup
.fromDirName(entry
) ]
225 def getDesiredEpoch(self
, backups
, now
):
226 """Get desired epoch based on self.configuration and list of old backups"""
228 # Find the longest epoch for which we would like the make a backup
229 latest
= datetime
.datetime(1900, 1, 1)
230 for timespan
, e
in reversed(sorted( [ (Epoch
[e
], e
) for e
in RealEpoch
] )):
231 # We make backups of that epoch
232 if self
.conf
.epochkeeps
[e
] == 0:
235 # Get backups of that epoch
236 byepoch
= list(sorted( [ b
for b
in backups
if b
.epoch
==e
], \
237 key
=lambda b
: b
.date
))
239 # If there are any, determine the latest
241 latest
= max(latest
, byepoch
[-1].date
)
243 # the latest backup is too old
244 if now
-latest
> timespan
:
247 # No backup is to be made
252 def backupFileSet(self
, fileset
, targetdir
, since
=None):
253 """Create an archive for given fileset at given target directory."""
255 print("Running file set: " + fileset
.name
)
257 fsfn
= os
.path
.join(targetdir
, fileset
.name
) + "." + self
.conf
.format
262 taropts
+= ["-N", since
.strftime("%Y-%m-%d %H:%M:%S")]
264 for pat
in self
.conf
.exclpatterns
:
265 taropts
+= ["--exclude", pat
]
267 tarargs
= [tarpath
] + taropts
+ ["-f", fsfn
] + fileset
.dirs
268 #print("tarargs: ", tarargs)
269 tarp
= subprocess
.Popen( tarargs
)
273 print(tarpath
+ " returned with exit status " + str(rett
) + ":")
276 def backup(self
, epoch
=None, mode
=None):
277 """Make a new backup, if necessary. If epoch is None then determine
278 desired epoch automatically. Use given epoch otherwise. If mode is None
279 then use mode for given epoch. Use given mode otherwise."""
281 now
= datetime
.datetime
.now()
282 oldbackups
= self
.listOldBackups()
284 # Get epoch of backup
286 epoch
= self
.getDesiredEpoch(oldbackups
, now
)
288 print("No backup planned.")
293 mode
= self
.conf
.epochmodes
[epoch
]
294 print("Making a backup. Epoch: " + epoch
+ ", mode: " + mode
)
296 oldfullbackups
= [ b
for b
in oldbackups
if b
.mode
== "full" ]
298 # No old full backups existing
299 if mode
!= "full" and len(oldfullbackups
)==0:
300 print("No full backups existing. Making a full backup.")
302 # Checksum changed -> self.config file changed
303 if self
.conf
.checksum
!= self
.conf
.lastchecksum
:
304 print("Config file changed since last time.")
306 print("** Warning: full backup recommended!")
309 # If we have a full backup, we backup everything
312 since
= sorted(oldfullbackups
, key
=lambda b
: b
.date
)[-1].date
314 since
= sorted(oldbackups
, key
=lambda b
: b
.date
)[-1].date
317 print("Making backup relative to ", since
.ctime())
319 yesno
= self
.ask_user_yesno("Proceed? [Y, n] ")
323 # Create new target directory
324 basedir
= self
.conf
.directory
325 dirname
= Backup
.getDirName(now
, epoch
, mode
)
326 tmpdirname
= dirname
+ ("-%x" % (random
.random()*2e16
) )
327 targetdir
= os
.path
.join(basedir
, tmpdirname
)
328 os
.mkdir( targetdir
)
331 # Backup all file sets
332 for s
in self
.conf
.sets
:
333 self
.backupFileSet(s
, targetdir
, since
)
335 # Rename backup directory to final name
336 os
.rename( targetdir
, os
.path
.join(basedir
, dirname
) )
338 # We made a full backup -- recall checksum of config
340 f
= open( os
.path
.join(basedir
, self
.conf
.checksumfn
), "w")
341 f
.write( self
.conf
.checksum
)
346 """Prune old backup files"""
348 allDirs
= self
.listAllDirs()
349 # Collect all directories not matching backup name
350 removeDirs
= [ d
for d
in allDirs
if not Backup
.isBackupDir(d
) ]
352 # Get all directories which are kept
353 backups
= self
.listOldBackups()
355 byepoch
= { e
: list(sorted( [ b
for b
in backups
if b
.epoch
== e
], \
356 key
=lambda b
: b
.date
, reverse
=True)) for e
in RealEpoch
}
358 keep
= self
.conf
.epochkeeps
[e
]
359 old
= byepoch
[e
][keep
:]
360 removeDirs
+= [ Backup
.getDirName(b
.date
, b
.epoch
, b
.mode
) for b
in old
]
363 print("List of stale/outdated entries:")
366 print("[*] ", end
="")
368 print("[ ] ", end
="")
370 if Backup
.isBackupDir(d
):
371 print( Backup
.fromDirName(d
).colAlignedString())
375 # Check that dirs to be removed is in list of all dirs
377 assert( d
in allDirs
)
379 if len(removeDirs
) == 0:
380 print("No stale/outdated entries to remove.")
383 basedir
= self
.conf
.directory
384 yesno
= self
.ask_user_yesno("Remove entries marked by '*'? [y, N] ")
387 shutil
.rmtree(os
.path
.join(basedir
, d
))
389 def ask_user_yesno(self
, question
):
391 print(question
+ " y")
394 return input(question
)
398 """Print --help text"""
400 print("shbackup - a simple backup solution.")
403 print(" " + sys
.argv
[0] + " {options} [cmd]")
404 print(" " + sys
.argv
[0] + " --help")
407 print(" backup make a new backup, if necessary")
408 print(" list list all backups (default)")
409 print(" prune prune outdated/old backups")
412 print(" -h, --help print this usage text")
413 print(" -c, --conf <configfile> use given configuration file")
414 print(" default: /etc/shbackup.conf")
415 print(" -e, --epoch <epoch> force to create backup for given epoch:")
416 print(" year, month, week, day, hour, sporadic")
417 print(" -m, --mode <mode> override mode: full, diff, or incr")
418 print(" -y, --yes always assume 'yes' when user is asked")
421 if __name__
== "__main__":
423 conffn
= "/etc/shbackup.conf"
430 while i
< len(sys
.argv
)-1:
434 if opt
in ["-h", "--help"]:
438 elif opt
in ["-c", "--conf"]:
442 elif opt
in ["-y", "--yes"]:
445 elif opt
in ["-m", "--mode"]:
449 print("Unknown mode '" + mode
+ "'.")
452 elif opt
in ["-e", "--epoch"]:
455 if not epoch
in Epoch
:
456 print("Unknown epoch '" + epoch
+ "'.")
460 elif opt
in ["backup", "list", "prune"]:
464 print("Unknown option: " + opt
)
468 man
= BackupManager(conffn
, yes
)
471 man
.backup(epoch
, mode
)
474 for b
in sorted(man
.listOldBackups(), key
=lambda b
: b
.date
):
475 print(b
.colAlignedString())
480 except (Config
.ReadError
, configparser
.DuplicateOptionError
) as e
:
481 print("Error reading config file: " + e
.message
)