Add --external-hardlink parameter
This commit is contained in:
parent
f01d24f1bd
commit
7daa8d74b3
20
main.py
20
main.py
@ -10,6 +10,7 @@ import sys
|
||||
from spqr.curateipsum.backup import initiate_backup
|
||||
|
||||
_lg = logging.getLogger("spqr.curateipsum")
|
||||
SUPPORTED_PLATFORMS = ("linux", "darwin")
|
||||
|
||||
|
||||
def main():
|
||||
@ -39,6 +40,10 @@ def main():
|
||||
action="store_true",
|
||||
default=False,
|
||||
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",
|
||||
nargs="+",
|
||||
metavar="SOURCE",
|
||||
@ -50,10 +55,20 @@ def main():
|
||||
logging.basicConfig(level=loglevel, handlers=[console_handler])
|
||||
|
||||
_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"):
|
||||
_lg.error("rsync should be installed to use --external-rsync option.")
|
||||
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))
|
||||
if not os.path.isdir(backup_dir_abs):
|
||||
_lg.error("Backup directory %s does not exist, exiting", args.backup_dir)
|
||||
@ -65,10 +80,11 @@ def main():
|
||||
return 1
|
||||
|
||||
initiate_backup(
|
||||
args.sources,
|
||||
backup_dir_abs,
|
||||
sources=args.sources,
|
||||
backup_dir=backup_dir_abs,
|
||||
dry_run=args.dry_run,
|
||||
external_rsync=args.external_rsync,
|
||||
external_hardlink=args.external_hardlink,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user