Skip to content

Commit 2ab1f2d

Browse files
committed
Update lock logic dstack-backup.py
1 parent 30bbfa0 commit 2ab1f2d

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

scripts/dstack-backup.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,6 @@ def perform_backup(self, vm_id: str, vm_name: str, backup_type: str, hd: str) ->
201201
# Create or update latest symlink
202202
latest_dir = backup_dir / "latest"
203203

204-
if backup_level == "full":
205-
# Create timestamped directory for this backup
206-
timestamp = datetime.now().strftime("%Y%m%dT%H%M%S")
207-
backup_timestamp_dir = backup_dir / f"{timestamp}"
208-
logger.info(f"Creating backup directory: {backup_timestamp_dir}")
209-
backup_timestamp_dir.mkdir(parents=True, exist_ok=True)
210-
try:
211-
latest_dir.unlink()
212-
except FileNotFoundError:
213-
pass
214-
latest_dir.symlink_to(timestamp)
215-
216204
def do_backup():
217205
# For full backups, clear bitmaps first
218206
if backup_level == "full":
@@ -250,7 +238,6 @@ def do_backup():
250238
f"Running: qmpbackup --socket {abs_qmp_socket} backup -i {hd} --no-subdir -t {abs_latest_dir} -l {backup_level}")
251239
if qmp_socket.exists():
252240
try:
253-
backup_lock.touch(exist_ok=False)
254241
# Use Popen for real-time output
255242
process = subprocess.Popen(
256243
[
@@ -285,24 +272,47 @@ def do_backup():
285272
except Exception as e:
286273
logger.error(f"Error performing backup: {e}")
287274
return False
288-
finally:
289-
backup_lock.unlink()
290275
else:
291276
logger.error(f"QMP socket not found at {qmp_socket}")
292277
return False
278+
279+
if backup_level == "full":
280+
# Create timestamped directory for this backup
281+
timestamp = datetime.now().strftime("%Y%m%dT%H%M%S")
282+
backup_timestamp_dir = backup_dir / f"{timestamp}"
283+
logger.info(f"Creating backup directory: {backup_timestamp_dir}")
284+
backup_timestamp_dir.mkdir(parents=True, exist_ok=True)
285+
293286
try:
294-
suc = do_backup()
287+
backup_lock.touch(exist_ok=False)
288+
locked = True
295289
except Exception as e:
296-
logger.error(f"Error performing backup: {e}")
297-
suc = False
298-
if not suc and backup_type == "full":
299-
# Remove the latest backup dir suc = self.perform_backup(vm_id, vm_name, "incremental", hd)
300-
logger.info(
301-
f"Removing {os.path.basename(backup_timestamp_dir)}")
290+
logger.error(f"Error creating backup lock: {e}")
291+
locked = False
292+
if locked:
302293
try:
303-
shutil.rmtree(backup_timestamp_dir)
294+
suc = do_backup()
304295
except Exception as e:
305-
logger.error(f"Error removing old backup: {e}")
296+
logger.error(f"Error performing backup: {e}")
297+
suc = False
298+
finally:
299+
backup_lock.unlink()
300+
else:
301+
suc = False
302+
if backup_type == "full":
303+
if not suc:
304+
logger.info(
305+
f"Removing {os.path.basename(backup_timestamp_dir)}")
306+
try:
307+
shutil.rmtree(backup_timestamp_dir)
308+
except Exception as e:
309+
logger.error(f"Error removing old backup: {e}")
310+
else:
311+
try:
312+
latest_dir.unlink()
313+
except FileNotFoundError:
314+
pass
315+
latest_dir.symlink_to(timestamp)
306316

307317
return suc
308318

0 commit comments

Comments
 (0)