From 13bfef2b5686bfc53bbb3ae7ca56fadc11f7e334 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Wed, 20 Nov 2024 14:07:33 +0500 Subject: [PATCH] bug fix: maintain csf custom configs ref: https://github.com/usmannasir/cyberpanel/issues/1353 --- plogical/upgrade.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/plogical/upgrade.py b/plogical/upgrade.py index edb136ae0..b89b5a65e 100755 --- a/plogical/upgrade.py +++ b/plogical/upgrade.py @@ -3556,12 +3556,59 @@ def upgrade(branch): #Upgrade.executioner(command, 'fix csf if there', 0) if os.path.exists('/etc/csf'): + ##### Function to backup custom csf files and restore + + from datetime import datetime + + # List of files to backup + FILES = [ + "/etc/csf/csf.allow", + "/etc/csf/csf.deny", + "/etc/csf/csf.conf", + "/etc/csf/csf.ignore", + "/etc/csf/csf.rignore", + "/etc/csf/csf.blocklists", + ] + + # Directory for backups + BACKUP_DIR = f"/home/cyberpanel/csf_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}" + + # Backup function + def backup_files(): + os.makedirs(BACKUP_DIR, exist_ok=True) + for file in FILES: + if os.path.exists(file): + shutil.copy(file, BACKUP_DIR) + print(f"Backed up: {file}") + else: + print(f"File not found, skipping: {file}") + + # Restore function + def restore_files(): + for file in FILES: + backup_file = os.path.join(BACKUP_DIR, os.path.basename(file)) + if os.path.exists(backup_file): + shutil.copy(backup_file, file) + print(f"Restored: {file}") + else: + print(f"Backup not found for: {file}") + + # Backup the files + print("Backing up files...") + backup_files() + execPath = "sudo /usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/csf.py" execPath = execPath + " removeCSF" Upgrade.executioner(execPath, 'fix csf if there', 0) execPath = "sudo /usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/csf.py" execPath = execPath + " installCSF" + + # Restore the files + print("Restoring files...") + restore_files() + + Upgrade.executioner(execPath, 'fix csf if there', 0)