Add --external-rsync option

This commit is contained in:
Maks Snegov 2021-06-23 20:11:42 +03:00
parent eaf2b3c0ea
commit 14552db4c8
3 changed files with 32 additions and 4 deletions

11
main.py
View File

@ -34,6 +34,10 @@ def main():
action="store_true", action="store_true",
default=False, default=False,
help="Do not do create backup") help="Do not do create backup")
parser.add_argument("--external-rsync",
action="store_true",
default=False,
help="Use external rsync for copying")
parser.add_argument("sources", parser.add_argument("sources",
nargs="+", nargs="+",
metavar="SOURCE", metavar="SOURCE",
@ -55,7 +59,12 @@ def main():
_lg.error("Source directory %s does not exist", src_dir) _lg.error("Source directory %s does not exist", src_dir)
return 1 return 1
initiate_backup(args.sources, backup_dir_abs, dry_run=args.dry_run) initiate_backup(
args.sources,
backup_dir_abs,
dry_run=args.dry_run,
external_rsync=args.external_rsync,
)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -43,7 +43,10 @@ def _get_latest_backup(backup_dir: pathlib.Path) -> Optional[pathlib.Path]:
return None return None
def initiate_backup(sources, backup_dir: pathlib.Path, dry_run=False): def initiate_backup(sources,
backup_dir: pathlib.Path,
dry_run: bool = False,
external_rsync: bool = False):
""" Main backup function """ """ Main backup function """
start_time = time.time() start_time = time.time()
@ -71,11 +74,13 @@ def initiate_backup(sources, backup_dir: pathlib.Path, dry_run=False):
shutil.rmtree(cur_backup, ignore_errors=True) shutil.rmtree(cur_backup, ignore_errors=True)
return return
rsync_func = fs.rsync_ext if external_rsync else fs.rsync
for src in sources: for src in sources:
src_abs = pathlib.Path(os.path.abspath(src)) src_abs = pathlib.Path(os.path.abspath(src))
dst_abs = cur_backup / src_abs.name dst_abs = cur_backup / src_abs.name
_lg.info("Backing up directory %s to %s backup", src_abs, cur_backup.name) _lg.info("Backing up directory %s to %s backup", src_abs, cur_backup.name)
fs.rsync_ext(src_abs, dst_abs, dry_run=dry_run) rsync_func(src_abs, dst_abs, dry_run=dry_run)
if dry_run: if dry_run:
_lg.info("Dry-run, removing created backup: %s", cur_backup.name) _lg.info("Dry-run, removing created backup: %s", cur_backup.name)

View File

@ -13,6 +13,20 @@ from typing import Iterable
_lg = logging.getLogger(__name__) _lg = logging.getLogger(__name__)
# *deleting will_be_deleted
# >f.st.... .gitignore
# >f+++++++ LICENSE
# >f+++++++ LICENSE-sym
# >f+++++++ README.md
# >f+++++++ find_stale_torrents.py
# >f+++++++ rootfile
# cL+++++++ test -> rootfile
# cd+++++++ folder/
# >f+++++++ folder/in-folder
# cd+++++++ java-alg/
def rsync_ext(src, dst, dry_run=False): def rsync_ext(src, dst, dry_run=False):
"""Call external rsync command""" """Call external rsync command"""
rsync_args = ["rsync"] rsync_args = ["rsync"]
@ -308,4 +322,4 @@ 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 _hardlink_dir_ext(src_abs, dst_abs) return _recursive_hardlink(src_abs, dst_abs)