mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-02-18 16:36:27 +00:00
Update flask_server.py
This commit is contained in:
@@ -5610,7 +5610,7 @@ def api_backup_storages():
|
|||||||
@app.route('/api/vms/<int:vmid>/backup', methods=['POST'])
|
@app.route('/api/vms/<int:vmid>/backup', methods=['POST'])
|
||||||
@require_auth
|
@require_auth
|
||||||
def api_create_backup(vmid):
|
def api_create_backup(vmid):
|
||||||
"""Create a backup for a VM or LXC container"""
|
"""Create a backup for a VM or LXC container using Proxmox API"""
|
||||||
try:
|
try:
|
||||||
data = request.get_json() or {}
|
data = request.get_json() or {}
|
||||||
storage = data.get('storage', 'local')
|
storage = data.get('storage', 'local')
|
||||||
@@ -5650,21 +5650,23 @@ def api_create_backup(vmid):
|
|||||||
notes = notes.replace('{{vmid}}', str(vmid))
|
notes = notes.replace('{{vmid}}', str(vmid))
|
||||||
notes = notes.replace('{{node}}', node or '')
|
notes = notes.replace('{{node}}', node or '')
|
||||||
|
|
||||||
# Create backup using vzdump
|
# Build pvesh command to create backup via Proxmox API
|
||||||
|
# This creates a vzdump task through the API which handles clustering properly
|
||||||
cmd = [
|
cmd = [
|
||||||
'vzdump', str(vmid),
|
'pvesh', 'create', f'/nodes/{node}/vzdump',
|
||||||
|
'--vmid', str(vmid),
|
||||||
'--storage', storage,
|
'--storage', storage,
|
||||||
'--mode', mode,
|
'--mode', mode,
|
||||||
'--compress', compress,
|
'--compress', compress
|
||||||
'--node', node
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Add protected flag if enabled
|
# Add protected flag if enabled (use 1 for true)
|
||||||
if protected:
|
if protected:
|
||||||
cmd.extend(['--protected', '1'])
|
cmd.extend(['--protected', '1'])
|
||||||
|
|
||||||
# Add notification mode
|
# Add notification mode (mailnotification for older versions, notification-mode for newer)
|
||||||
if notification and notification != 'auto':
|
if notification and notification != 'auto':
|
||||||
|
# Try notification-mode first (newer Proxmox versions)
|
||||||
cmd.extend(['--notification-mode', notification])
|
cmd.extend(['--notification-mode', notification])
|
||||||
|
|
||||||
# Add notes if provided
|
# Add notes if provided
|
||||||
@@ -5672,23 +5674,34 @@ def api_create_backup(vmid):
|
|||||||
cmd.extend(['--notes-template', notes])
|
cmd.extend(['--notes-template', notes])
|
||||||
|
|
||||||
# Add PBS change detection mode (only for LXC with PBS storage)
|
# Add PBS change detection mode (only for LXC with PBS storage)
|
||||||
if pbs_change_detection and vm_type == 'lxc':
|
if pbs_change_detection and pbs_change_detection != 'default' and vm_type == 'lxc':
|
||||||
cmd.extend(['--pbs-change-detection-mode', pbs_change_detection])
|
cmd.extend(['--pbs-change-detection-mode', pbs_change_detection])
|
||||||
|
|
||||||
# Run vzdump in background
|
# Execute the backup command
|
||||||
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||||||
|
|
||||||
|
if result.returncode != 0:
|
||||||
|
error_msg = result.stderr or result.stdout or 'Unknown error'
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': f'Backup failed: {error_msg}',
|
||||||
|
'command': ' '.join(cmd)
|
||||||
|
}), 500
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
'message': f'Backup started for VM {vmid}',
|
'message': f'Backup task started for {vm_type.upper()} {vmid}',
|
||||||
'storage': storage,
|
'storage': storage,
|
||||||
'mode': mode,
|
'mode': mode,
|
||||||
'compress': compress,
|
'compress': compress,
|
||||||
'protected': protected,
|
'protected': protected,
|
||||||
'notification': notification,
|
'notification': notification,
|
||||||
'notes': notes
|
'notes': notes,
|
||||||
|
'task_output': result.stdout
|
||||||
})
|
})
|
||||||
|
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
return jsonify({'error': 'Backup command timed out'}), 500
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user