Add --external-hardlink parameter

This commit is contained in:
Maks Snegov 2021-10-24 21:20:05 +03:00
parent f01d24f1bd
commit 7daa8d74b3
3 changed files with 30 additions and 8 deletions

20
main.py
View File

@ -10,6 +10,7 @@ import sys
from spqr.curateipsum.backup import initiate_backup from spqr.curateipsum.backup import initiate_backup
_lg = logging.getLogger("spqr.curateipsum") _lg = logging.getLogger("spqr.curateipsum")
SUPPORTED_PLATFORMS = ("linux", "darwin")
def main(): def main():
@ -39,6 +40,10 @@ def main():
action="store_true", action="store_true",
default=False, default=False,
help="Use external rsync for copying") help="Use external rsync for copying")
parser.add_argument("--external-hardlink",
action="store_true",
default=False,
help="Use cp command for creating hardlink copies")
parser.add_argument("sources", parser.add_argument("sources",
nargs="+", nargs="+",
metavar="SOURCE", metavar="SOURCE",
@ -50,10 +55,20 @@ def main():
logging.basicConfig(level=loglevel, handlers=[console_handler]) logging.basicConfig(level=loglevel, handlers=[console_handler])
_lg.info("Starting %s: %s", parser.prog, args) _lg.info("Starting %s: %s", parser.prog, args)
if sys.platform not in SUPPORTED_PLATFORMS:
_lg.error(f"Not supported platform: {sys.platform}. Supported platforms: {SUPPORTED_PLATFORMS}")
return 1
if args.external_rsync and not shutil.which("rsync"): if args.external_rsync and not shutil.which("rsync"):
_lg.error("rsync should be installed to use --external-rsync option.") _lg.error("rsync should be installed to use --external-rsync option.")
return 1 return 1
cp_program = "gcp" if sys.platform == "darwin" else "cp"
if args.external_hardlink and not shutil.which(cp_program):
_lg.error(f"{cp_program} should be installed to use --external-hardlink option.")
return 1
backup_dir_abs = pathlib.Path(os.path.abspath(args.backup_dir)) backup_dir_abs = pathlib.Path(os.path.abspath(args.backup_dir))
if not os.path.isdir(backup_dir_abs): if not os.path.isdir(backup_dir_abs):
_lg.error("Backup directory %s does not exist, exiting", args.backup_dir) _lg.error("Backup directory %s does not exist, exiting", args.backup_dir)
@ -65,10 +80,11 @@ def main():
return 1 return 1
initiate_backup( initiate_backup(
args.sources, sources=args.sources,
backup_dir_abs, backup_dir=backup_dir_abs,
dry_run=args.dry_run, dry_run=args.dry_run,
external_rsync=args.external_rsync, external_rsync=args.external_rsync,
external_hardlink=args.external_hardlink,
) )

View File

@ -18,6 +18,8 @@ _lg = logging.getLogger(__name__)
def _is_backup_entity(entity_path: pathlib.Path) -> bool: def _is_backup_entity(entity_path: pathlib.Path) -> bool:
""" Check if entity_path is a single backup dir. """ """ Check if entity_path is a single backup dir. """
if not os.path.isdir(entity_path):
return False
try: try:
datetime.strptime(entity_path.name, BACKUP_ENT_FMT) datetime.strptime(entity_path.name, BACKUP_ENT_FMT)
return True return True
@ -46,7 +48,8 @@ def _get_latest_backup(backup_dir: pathlib.Path) -> Optional[pathlib.Path]:
def initiate_backup(sources, def initiate_backup(sources,
backup_dir: pathlib.Path, backup_dir: pathlib.Path,
dry_run: bool = False, dry_run: bool = False,
external_rsync: bool = False): external_rsync: bool = False,
external_hardlink: bool = False):
""" Main backup function """ """ Main backup function """
start_time = time.time() start_time = time.time()
@ -67,7 +70,8 @@ def initiate_backup(sources,
_lg.info("Copying data from latest backup %s to current backup %s", _lg.info("Copying data from latest backup %s to current backup %s",
latest_backup.name, cur_backup.name) latest_backup.name, cur_backup.name)
hl_res = fs.hardlink_dir(latest_backup, cur_backup) hl_res = fs.hardlink_dir(src_dir=latest_backup, dst_dir=cur_backup,
use_external=external_hardlink)
if not hl_res: if not hl_res:
_lg.error("Something went wrong during copying data from latest backup," _lg.error("Something went wrong during copying data from latest backup,"
" removing created %s", cur_backup.name) " removing created %s", cur_backup.name)

View File

@ -255,7 +255,7 @@ def rsync(src_dir, dst_dir, dry_run=False):
follow_symlinks=False) follow_symlinks=False)
def _hardlink_dir_ext(src, dst) -> bool: def _recursive_hardlink_ext(src: str, dst: str) -> bool:
""" """
Make hardlink for a directory using cp -al. Both src and dst should exist. Make hardlink for a directory using cp -al. Both src and dst should exist.
:param src: absolute path to source directory. :param src: absolute path to source directory.
@ -277,7 +277,7 @@ def _hardlink_dir_ext(src, dst) -> bool:
return not bool(exitcode) return not bool(exitcode)
def _recursive_hardlink(src, dst) -> bool: def _recursive_hardlink(src: str, dst: str) -> bool:
""" """
Do hardlink directory recursively using python only. Do hardlink directory recursively using python only.
Both src and dst directories should exist. Both src and dst directories should exist.
@ -313,11 +313,12 @@ def _recursive_hardlink(src, dst) -> bool:
return True return True
def hardlink_dir(src_dir, dst_dir) -> bool: def hardlink_dir(src_dir, dst_dir, use_external: bool = False) -> bool:
""" """
Make hardlink for a directory with all its content. Make hardlink for a directory with all its content.
:param src_dir: path to source directory :param src_dir: path to source directory
:param dst_dir: path to target directory :param dst_dir: path to target directory
:param use_external: whether to use external cp -al command
:return: success or not :return: success or not
""" """
_lg.info(f"Recursive hardlinking: {src_dir} -> {dst_dir}") _lg.info(f"Recursive hardlinking: {src_dir} -> {dst_dir}")
@ -335,4 +336,5 @@ def hardlink_dir(src_dir, dst_dir) -> bool:
_lg.debug(f"Creating directory: {dst_abs}") _lg.debug(f"Creating directory: {dst_abs}")
os.mkdir(dst_abs) os.mkdir(dst_abs)
return _recursive_hardlink(src_abs, dst_abs) hardlink_func = _recursive_hardlink_ext if use_external else _recursive_hardlink
return hardlink_func(src_abs, dst_abs)