-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- remove the use of ".env" - ref: project structure - deps: remove "dotenv"
- Loading branch information
Showing
8 changed files
with
95 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.3.3' | ||
__version__ = '0.4.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
import zipfile | ||
import typer | ||
import logging | ||
|
||
from .ibd_backup import ibd_backup | ||
from .ibd_restore import ibd_restore | ||
from .tools import zipfile_ls | ||
from .deps import complete_filename | ||
|
||
from ibdx.ibd_backup import ibd_backup | ||
from ibdx.ibd_restore import ibd_restore | ||
from ibdx.deps import complete_filename | ||
|
||
logging.basicConfig(level='INFO') | ||
cli = typer.Typer() | ||
|
||
|
||
@cli.command() | ||
def backup( | ||
dbname: str = typer.Option(..., '--db', '-d'), | ||
tables_pattern: str = typer.Option('*', '--tables', '-t'), | ||
output_filename: str = typer.Option(..., '--file', '-f', autocompletion=complete_filename), | ||
datadir: str = typer.Option(''), | ||
): | ||
try: | ||
ibd_backup(dbname, tables_pattern, output_filename, datadir) | ||
except Exception as e: | ||
typer.echo(f'ibdx error: {e}') | ||
|
||
|
||
@cli.command() | ||
def restore( | ||
dbname: str = typer.Option(..., '--db', '-d'), | ||
tables_pattern: str = typer.Option('*', '--tables', '-t'), | ||
input_filename: str = typer.Option(..., '--file', '-f', autocompletion=complete_filename), | ||
datadir: str = typer.Option(''), | ||
): | ||
try: | ||
ibd_restore(dbname, tables_pattern, input_filename, datadir) | ||
except Exception as e: | ||
typer.echo(f'ibdx error: {e}') | ||
cli.command('backup')(ibd_backup) | ||
cli.command('restore')(ibd_restore) | ||
|
||
|
||
@cli.command() | ||
def ls(zipfile_name: str = typer.Argument('', autocompletion=complete_filename)): | ||
try: | ||
for name in zipfile_ls(zipfile_name): | ||
if not zipfile.is_zipfile(zipfile_name): | ||
raise Exception('zipfile_name is not a zip file') | ||
zip_file = zipfile.ZipFile(zipfile_name, 'r', zipfile.ZIP_DEFLATED) | ||
|
||
for name in zip_file.namelist(): | ||
typer.echo(name) | ||
except Exception as e: | ||
typer.echo(f'ibdx error: {e}') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,66 @@ | ||
import zipfile | ||
from pathlib import Path | ||
import fnmatch | ||
import typer | ||
import logging | ||
|
||
from . import __version__ | ||
from .mysql_db_quick import MysqlConn | ||
from .configs import DB_CONFIG | ||
from ibdx import __version__ | ||
from ibdx.deps import complete_filename | ||
from ibdx.mysql_db_quick import MysqlConn | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def ibd_backup( | ||
dbname: str, | ||
tables_pattern: str, | ||
out_fpath: str, | ||
datadir: str = '', | ||
dbname: str = typer.Option(..., '--db', '-d'), | ||
host: str = typer.Option('127.0.0.1', '--host', '-h'), | ||
port: int = typer.Option(3306, '--port', '-p'), | ||
user: str = typer.Option('root', '--user', '-u'), | ||
password: str = typer.Option('', '--password', '-P'), | ||
tables_pattern: str = typer.Option('*', '--tables', '-t'), | ||
fout_path: str = typer.Option(..., '--file', '-f', autocompletion=complete_filename), | ||
datadir: str = typer.Option(''), | ||
) -> None: | ||
db = MysqlConn(dbname, DB_CONFIG) | ||
db = MysqlConn(dbname, host, port, user, password) | ||
|
||
if not datadir: | ||
res = db.query('show variables like \'datadir\';').fetchone() | ||
res = db.query("show variables like 'datadir';").fetchone() | ||
if res is None: | ||
raise Exception('cannot get datadir') | ||
logger.error('cannot get datadir') | ||
return | ||
datadir = res[1] | ||
if not Path(datadir).exists(): | ||
raise Exception('datadir does not exists') | ||
logger.error('datadir does not exists') | ||
return | ||
|
||
db_dpath = Path(datadir) / dbname | ||
assert db_dpath.is_dir() | ||
|
||
_out_fpath = Path(out_fpath) | ||
|
||
if not _out_fpath.exists(): | ||
with zipfile.ZipFile(_out_fpath, 'w', zipfile.ZIP_DEFLATED) as zip_file: | ||
zip_file.writestr(f'.ibdx.v{__version__}', f'{__version__}') | ||
_out_fpath = Path(fout_path) | ||
|
||
tables = fnmatch.filter(db.get_tables(), tables_pattern) | ||
if not tables: | ||
logger.error('no table macthes') | ||
return | ||
|
||
host_info = db.get_version() | ||
if _out_fpath.exists(): | ||
logger.error('can not write to an exists archive') | ||
return | ||
|
||
with zipfile.ZipFile(_out_fpath, 'a', zipfile.ZIP_DEFLATED) as zip_file: | ||
arc = zipfile.ZipFile(_out_fpath, 'w', zipfile.ZIP_DEFLATED) | ||
try: | ||
for table in tables: | ||
print(f"backup table: {table}") | ||
logger.info(f'backing up table: {table}') | ||
|
||
db.query(f"flush tables `{table}` for export;") | ||
db.query(f'flush tables `{table}` for export;') | ||
try: | ||
sql_create = db.query(f'show create table `{table}`;').fetchall()[0][1] | ||
zip_file.writestr(f'{table}.sql', sql_create) | ||
zip_file.write(db_dpath / f'{table}.ibd', f'{table}.ibd') | ||
zip_file.write(db_dpath / f'{table}.cfg', f'{table}.cfg') | ||
arc.writestr(f'{table}.sql', sql_create) | ||
arc.write(db_dpath / f'{table}.ibd', f'{table}.ibd') | ||
arc.write(db_dpath / f'{table}.cfg', f'{table}.cfg') | ||
arc.writestr(f'{table}.ibdx', f'ibdx.v{__version__}\n{host_info}') | ||
finally: | ||
db.query('unlock tables;') | ||
finally: | ||
arc.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters