poprawka w przywracaniu backupu

This commit is contained in:
Mateusz Gruszczyński 2025-03-11 09:17:03 +01:00
parent a754dcea53
commit a6d252e627

47
app.py
View File

@ -951,18 +951,7 @@ def restore_backup(backup_id):
return redirect(url_for('backups'))
try:
if host.type == 'mikrotik':
ssh = open_ssh_connection(host)
ssh.exec_command("/ip dns static remove [find]")
import time
time.sleep(1)
commands = []
for line in backup.content.splitlines():
line = line.strip()
if line.startswith("add "):
commands.append("/ip dns static " + line)
full_command = " ; ".join(commands)
ssh.exec_command(full_command)
ssh.close()
deploy_mikrotik(host, backup.content)
flash(f'Backup restored to {format_host(host)} successfully.', 'success')
else:
ssh = open_ssh_connection(host)
@ -1301,18 +1290,18 @@ def deploy_user(user_id):
db.session.add(DeployLog(details=error_log, user_id=user_id))
db.session.commit()
def deploy_mikrotik(host, hosts_content):
ssh = open_ssh_connection(host)
stdin, stdout, stderr = ssh.exec_command("/ip dns static export")
exported = stdout.read().decode('utf-8').splitlines()
existing_dns = {}
# Przetwarzamy aktualne wpisy z urządzenia
for line in exported:
line = line.strip()
if not line.startswith('add '):
continue
line = line[4:].strip()
line = line[4:].strip() # usuwamy "add "
parts = line.split()
address_val = None
name_val = None
@ -1325,23 +1314,28 @@ def deploy_mikrotik(host, hosts_content):
existing_dns[name_val] = address_val
desired_dns = {}
# Parsujemy zawartość backupu w taki sam sposób jak wpisy aktualne
for line in hosts_content.splitlines():
line = line.strip()
if (not line
or line.startswith('#')
or 'Auto-hosts_upload:' in line
or 'End_of_auto-hosts_upload:' in line):
if not line:
continue
if line.startswith('add '):
line = line[4:].strip() # usuwamy "add "
if line.startswith('#') or 'Auto-hosts_upload:' in line or 'End_of_auto-hosts_upload:' in line:
continue
parts = line.split()
if len(parts) < 2:
continue
ip_address = parts[0]
hostnames = parts[1:]
for hname in hostnames:
desired_dns[hname] = ip_address
address_val = None
name_val = None
for part in parts:
if part.startswith('address='):
address_val = part.replace('address=', '')
elif part.startswith('name='):
name_val = part.replace('name=', '')
if address_val and name_val:
desired_dns[name_val] = address_val
# Dodajemy lub aktualizujemy wpisy, a zbędne usuwamy
for name_val, ip_val in desired_dns.items():
if name_val not in existing_dns:
add_cmd = f"/ip dns static add address={ip_val} name={name_val}"
@ -1353,13 +1347,14 @@ def deploy_mikrotik(host, hosts_content):
ssh.exec_command(remove_cmd)
add_cmd = f"/ip dns static add address={ip_val} name={name_val}"
ssh.exec_command(add_cmd)
for existing_name, existing_ip in existing_dns.items():
for existing_name in existing_dns:
if existing_name not in desired_dns:
remove_cmd = f"/ip dns static remove [find where name={existing_name}]"
ssh.exec_command(remove_cmd)
ssh.close()
@app.route('/deploy-now')
def deploy_now():
if 'user_id' not in session: