Skip to content

Commit

Permalink
add mysql database backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMohammad20000 committed Nov 2, 2023
1 parent 7a1abaf commit 33e9f80
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 42 deletions.
67 changes: 39 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,52 @@ nano server_list.json

```json
{
"servers":[
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
"servers": [
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"mysql_db_directory": "mysql",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
}
```

if you use **mysql** database for your pannel set `"is_mysql_DB"` flag `true`

bot support multiple panel if you have another panel you can use this json

```json
{
"servers":[
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
},
{
"host": "host2",
"port": 22,
"user": "user2",
"pass": "pass2",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
"servers": [
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"mysql_db_directory": "mysql",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
},
{
"host": "host2",
"port": 22,
"user": "user2",
"pass": "pass2",
"is_mysql_DB": true,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"mysql_db_directory": "mysql",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
}
```

Expand Down
21 changes: 17 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _create_zipFile(ssh, scp_client_obj, zip_file_obj, remote_path, files):
return zip_file_obj


def create_zipFile(hostname, port, username, password, var_files, opt_files):
def create_zipFile(hostname, port, username, password, var_files, opt_files, is_mysql_DB, mysql_db_directory, mysql_db_path):
try:
ssh = createSSHClient(hostname, port, username, password)
with (
Expand All @@ -101,8 +101,18 @@ def create_zipFile(hostname, port, username, password, var_files, opt_files):
):
remote_var_files = get_list_dir(ssh, var_files)
remote_opt_files = get_list_dir(ssh, opt_files)
zf = _create_zipFile(ssh, scp, zf, var_files, remote_var_files)
zf = _create_zipFile(ssh, scp, zf, opt_files, remote_opt_files)

if mysql_db_directory in remote_var_files:
remote_var_files.remove(mysql_db_directory)

zf = _create_zipFile(ssh, scp, zf, var_files,
remote_var_files)
zf = _create_zipFile(ssh, scp, zf, opt_files,
remote_opt_files)
if is_mysql_DB:
remote_db_files = get_list_dir(ssh, mysql_db_path)
zf = _create_zipFile(
ssh, scp, zf, mysql_db_path, remote_db_files)
except Exception as e:
logging.info(e)
return
Expand All @@ -122,10 +132,13 @@ async def send_full_backups():
port = i["port"]
username = i['user']
password = i['pass']
is_mysql_DB: bool = i['is_mysql_DB']
mysql_db_directory = i['mysql_db_directory']
mysql_db_path = i['mysql_db_path']
var_files = i['var_files']
opt_files = i['opt_files']
bac = create_zipFile(hostname, port, username,
password, var_files, opt_files)
password, var_files, opt_files, is_mysql_DB, mysql_db_directory, mysql_db_path)
if not bac:
continue
date = get_date()
Expand Down
23 changes: 13 additions & 10 deletions server_list.json.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"servers":[
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
"servers": [
{
"host": "host",
"port": 22,
"user": "user",
"pass": "pass",
"is_mysql_DB": false,
"mysql_db_path": "/var/lib/marzban/mysql/marzban/",
"mysql_db_directory": "mysql",
"var_files": "/var/lib/marzban/",
"opt_files": "/opt/marzban/"
}
]
}

0 comments on commit 33e9f80

Please sign in to comment.