sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment.dsn))
sh.call("gzip", "--best", outfile)
except shell.CallError as e:
- shutil.rmtree(outdir)
raise BackupFailure(e.stderr)
def restore_database(backup_dir, deployment):
import logging
import decorator
import datetime
+import tempfile
import wizard
from wizard import app, git, old_log, scripts, shell, sql, util
"""
Performs a backup of database schemas and other non-versioned data.
"""
- backupdir = os.path.join(self.location, ".scripts", "backups")
- backup = str(self.version) + "-" + datetime.date.today().isoformat()
- outdir = os.path.join(backupdir, backup)
+ # There are retarded amounts of race-safety in this function,
+ # because we do NOT want to claim to have made a backup, when
+ # actually something weird happened to it.
+ backupdir = os.path.join(self.scripts_dir, "backups")
if not os.path.exists(backupdir):
- 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
+ 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
return backup
@chdir_to_location
def restore(self, backup, options):