- os.mkdir(backupdir)
- if os.path.exists(outdir):
- util.safe_unlink(outdir)
- os.mkdir(outdir)
- self.application.backup(self, outdir, options)
+ try:
+ os.mkdir(backupdir)
+ except OSError as e:
+ if e.errno == errno.EEXIST:
+ pass
+ else:
+ raise
+ tmpdir = tempfile.mkdtemp() # actually will be kept around
+ try:
+ self.application.backup(self, tmpdir, options)
+ except app.BackupFailure:
+ # the backup is bogus, don't let it show up
+ shutil.rmtree(tmpdir)
+ raise
+ backup = None
+ with util.LockDirectory(os.path.join(backupdir, "lock")):
+ while 1:
+ backup = str(self.version) + "-" + datetime.datetime.today().strftime("%Y-%m-%dT%H%M%S")
+ outdir = os.path.join(backupdir, backup)
+ if os.path.exists(outdir):
+ logging.warning("Backup: A backup occurred in the last second. Trying again in a second...")
+ time.sleep(1)
+ continue
+ try:
+ shutil.move(tmpdir, outdir)
+ except:
+ # don't leave half-baked stuff lying around
+ try:
+ shutil.rmtree(outdir)
+ except OSError:
+ pass
+ raise
+ break