- 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:
- os.rename(tmpdir, outdir)
- except OSError:
- logging.warning("Backup: We lost the race, trying again in a second...")
- time.sleep(1)
- continue
- break
+ 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