Add --external-hardlink parameter

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

View File

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

View File

@@ -255,7 +255,7 @@ def rsync(src_dir, dst_dir, dry_run=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.
:param src: absolute path to source directory.
@@ -277,7 +277,7 @@ def _hardlink_dir_ext(src, dst) -> bool:
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.
Both src and dst directories should exist.
@@ -313,11 +313,12 @@ def _recursive_hardlink(src, dst) -> bool:
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.
:param src_dir: path to source directory
:param dst_dir: path to target directory
:param use_external: whether to use external cp -al command
:return: success or not
"""
_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}")
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)