Skip to content

Commit

Permalink
fix: mysql backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMohammad20000 committed Jan 4, 2024
1 parent 7b434d4 commit f1d4efe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ nano server_list.json
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"is_mysql_DB": false,
"mysql_user": "root",
"mysql_password": "password",
"database_name": "marzban",
"mysql_contaner_name": "marzban-mysql-1",
"exclude": [
"mysql"
],
Expand All @@ -73,8 +76,11 @@ bot support multiple panel if you have another panel you can use this json
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"is_mysql_DB": false,
"mysql_user": "root",
"mysql_password": "password",
"database_name": "marzban",
"mysql_contaner_name": "marzban-mysql-1",
"exclude": [
"mysql"
],
Expand All @@ -86,8 +92,11 @@ bot support multiple panel if you have another panel you can use this json
"port": 22,
"user": "user2",
"pass": "pass2",
"is_mysql_DB": true,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"is_mysql_DB": true,
"mysql_user": "root",
"mysql_password": "password",
"database_name": "marzban",
"mysql_contaner_name": "marzban-mysql-1",
"exclude": [
"mysql"
],
Expand Down
34 changes: 21 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,27 @@ def exclude_files_and_dirctories(exclude: list, _list: list):
_list.remove(e)


def create_zipFile(hostname, port, username, password, var_files, opt_files, is_mysql_DB, exclude, mysql_db_path):
def mysql_backup(ssh: paramiko.SSHClient, mysql_user: str, mysql_password: str, mysql_contaner_name: str, database_name: str, path: str):
stdin, stdout, stderr = ssh.exec_command(
f'docker exec {mysql_contaner_name} mysqldump -u {mysql_user} --password={mysql_password} {database_name} > "{path}/{database_name}.sql"'
)
# stdin, stdout, stderr = ssh.exec_command(
# f'mysqldump -u {mysql_user} --password={mysql_password} {database_name} > "{path}/{database_name}.sql"'
# )
if stderr:
logging.error(stderr.read().decode().strip())


def create_zipFile(hostname, port, username, password, var_files, opt_files, is_mysql_DB, exclude, mysql_user: str, mysql_password: str, mysql_contaner_name: str, database_name: str):
try:
ssh = createSSHClient(hostname, port, username, password)
with (
SCPClient(ssh.get_transport()) as scp,
zipfile.ZipFile(BACKUP_FILE_NAME, "w", zipfile.ZIP_DEFLATED) as zf
):
if is_mysql_DB:
mysql_backup(ssh, mysql_user, mysql_password,
mysql_contaner_name, database_name, var_files)
remote_var_files = get_list_dir(ssh, var_files)
remote_opt_files = get_list_dir(ssh, opt_files)
exclude_files_and_dirctories(exclude, remote_var_files)
Expand All @@ -126,15 +140,7 @@ def create_zipFile(hostname, port, username, password, var_files, opt_files, is_
zf, opt_files,
remote_opt_files, exclude
)
if is_mysql_DB:
remote_db_files = get_list_dir(ssh, mysql_db_path)
exclude_files_and_dirctories(exclude, remote_db_files)

zf = _create_zipFile(
ssh, scp,
zf, mysql_db_path,
remote_db_files, exclude
)

except Exception as e:
logging.info(e)
return
Expand All @@ -156,13 +162,15 @@ async def send_full_backups():
password = i['pass']
is_mysql_DB = i['is_mysql_DB']
exclude = i['exclude']
mysql_db_path = i['mysql_db_path']
mysql_user = i['mysql_user']
mysql_password = i['mysql_password']
database_name = i['database_name']
mysql_contaner_name = i['mysql_contaner_name']
var_files = i['var_files']
opt_files = i['opt_files']
bac = create_zipFile(hostname, port,
username, password, var_files,
opt_files, is_mysql_DB, exclude, mysql_db_path,
)
opt_files, is_mysql_DB, exclude, mysql_user, mysql_password, mysql_contaner_name, database_name)
if not bac:
continue
date = get_date()
Expand Down
7 changes: 5 additions & 2 deletions server_list.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"is_mysql_DB": false,
"mysql_user": "root",
"mysql_password": "password",
"database_name": "marzban",
"mysql_contaner_name": "marzban-mysql-1",
"exclude": [
"mysql"
],
Expand Down

0 comments on commit f1d4efe

Please sign in to comment.