Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
feature: add new option for generate default config file in default p…
Browse files Browse the repository at this point in the history
…ath (#448)

As discussed in #446 it can be valuable to add an commandline option to generate the default config file is this was somehow eliminated by setup.

--generate-config-file flag option was added. Sample run will be something like this:
autoxtrabackup --generate-config-file --verbose
2021-10-03 19:03:07 INFO [autoxtrabackup:278] Default config file is generated in /home/shako/.autoxtrabackup/autoxtrabackup.cnf
2021-10-03 19:03:07 INFO [autoxtrabackup:313] Xtrabackup command history:
2021-10-03 19:03:07 INFO [autoxtrabackup:315] ['command', 'xtrabackup_function', 'start time', 'end time', 'duration', 'exit code']
2021-10-03 19:03:07 INFO [autoxtrabackup:316] Autoxtrabackup completed successfully!
  • Loading branch information
ShahriyarR authored Oct 3, 2021
1 parent dfdf86b commit 3d5ff49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
22 changes: 17 additions & 5 deletions mysql_autoxtrabackup/autoxtrabackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from mysql_autoxtrabackup.backup_prepare.prepare import Prepare
from mysql_autoxtrabackup.general_conf import path_config
from mysql_autoxtrabackup.general_conf.generalops import GeneralClass
from mysql_autoxtrabackup.general_conf.generate_default_conf import GenerateDefaultConfig
from mysql_autoxtrabackup.process_runner.process_runner import ProcessRunner
from mysql_autoxtrabackup.utils import version

Expand Down Expand Up @@ -103,12 +104,11 @@ def validate_file(file: str) -> Optional[bool]:
# filename extension should be .cnf
pattern = re.compile(r".*\.cnf")

if pattern.match(file):
# Lastly the file should have all 5 required headers
if check_file_content(file):
return None
else:
if not pattern.match(file):
raise ValueError("Invalid file extension. Expecting .cnf")
# Lastly the file should have all 5 required headers
if check_file_content(file):
return None
return None


Expand All @@ -133,6 +133,12 @@ def validate_file(file: str) -> Optional[bool]:
show_default=True,
help="Read options from the given file",
)
@click.option(
"--generate-config-file",
is_flag=True,
is_eager=True,
help="Create a config file template in default directory"
)
@click.option("--tag", help="Pass the tag string for each backup")
@click.option("--show-tags", is_flag=True, help="Show backup tags and exit")
@click.option("-v", "--verbose", is_flag=True, help="Be verbose (print to console)")
Expand Down Expand Up @@ -188,6 +194,7 @@ def all_procedure(
log_file,
log,
defaults_file,
generate_config_file,
dry_run,
log_file_max_bytes,
log_file_backup_count,
Expand Down Expand Up @@ -256,6 +263,7 @@ def all_procedure(
and dry_run is False
and show_tags is False
and run_server is False
and generate_config_file is False
):
print_help(ctx, None, value=True)

Expand All @@ -264,6 +272,10 @@ def all_procedure(
elif show_tags and defaults_file:
backup_ = Backup(config=defaults_file)
backup_.show_tags(backup_dir=str(backup_options.get("backup_dir")))
elif generate_config_file:
gen_ = GenerateDefaultConfig()
gen_.generate_config_file()
logger.info(f"Default config file is generated in {defaults_file}")
elif prepare:
prepare_ = Prepare(config=defaults_file, dry_run=dry_run_, tag=tag)
prepare_.prepare_backup_and_copy_back()
Expand Down
18 changes: 8 additions & 10 deletions mysql_autoxtrabackup/general_conf/generalops.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,21 @@ def backup_archive_options(self) -> Dict[str, Union[str, float]]:
archive_max_size = self.con.get(section, "max_archive_size", fallback=None)
if archive_max_size:
archive_max_size = humanfriendly.parse_size(archive_max_size)
else:
if self.con.get(section, "archive_max_size", fallback=None):
archive_max_size = humanfriendly.parse_size(
self.con.get(section, "archive_max_size", fallback=None)
)
elif self.con.get(section, "archive_max_size", fallback=None):
archive_max_size = humanfriendly.parse_size(
self.con.get(section, "archive_max_size", fallback=None)
)

# backward compatible with old config 'max_archive_duration' and newer 'archive_max_duration'
archive_max_duration = self.con.get(
section, "max_archive_duration", fallback=None
)
if archive_max_duration:
archive_max_duration = humanfriendly.parse_timespan(archive_max_duration)
else:
if self.con.get(section, "archive_max_size", fallback=None):
archive_max_duration = humanfriendly.parse_timespan(
self.con.get(section, "archive_max_size", fallback=None)
)
elif self.con.get(section, "archive_max_size", fallback=None):
archive_max_duration = humanfriendly.parse_timespan(
self.con.get(section, "archive_max_size", fallback=None)
)

return {
"archive_dir": self.con.get(section, "archive_dir", fallback=None), # type: ignore
Expand Down

0 comments on commit 3d5ff49

Please sign in to comment.