Add dry-run option

This commit is contained in:
Maks Snegov 2021-06-21 08:27:18 +03:00
parent 27b781a534
commit 126ae85388
3 changed files with 28 additions and 25 deletions

27
main.py
View File

@ -20,28 +20,25 @@ def main():
prog="cura-te-ipsum", description="cura-te-ipsum, my personal backup software.",
)
parser.add_argument("-V", "--version", action="version", version="%(prog)s 0.1")
parser.add_argument(
"-v",
"--verbose",
parser.add_argument("-v", "--verbose",
action="store_true",
default=False,
help="print verbose information",
)
parser.add_argument(
"-b",
type=pathlib.Path,
help="print verbose information")
parser.add_argument("-b",
dest="backup_dir",
metavar="BACKUP_DIR",
type=pathlib.Path,
required=True,
help="directory, where all backups will be stored",
)
parser.add_argument(
"sources",
help="directory, where all backups will be stored")
parser.add_argument("-n", "--dry-run",
action="store_true",
default=False,
help="Do not do create backup")
parser.add_argument("sources",
nargs="+",
metavar="SOURCE",
type=pathlib.Path,
help="backup source (file/dir/smth else)",
)
help="backup source (file/dir/smth else)")
args = parser.parse_args()
loglevel = logging.DEBUG if args.verbose else logging.INFO
@ -58,7 +55,7 @@ def main():
_lg.error("Source directory %s does not exist", src_dir)
return 1
initiate_backup(args.sources, backup_dir_abs)
initiate_backup(args.sources, backup_dir_abs, dry_run=args.dry_run)
if __name__ == "__main__":

View File

@ -5,6 +5,7 @@ Module with backup functions.
import logging
import os
import pathlib
import shutil
from datetime import datetime
from typing import Optional
@ -44,7 +45,7 @@ def _get_latest_backup(backup_dir: pathlib.Path) -> Optional[pathlib.Path]:
return None
def initiate_backup(sources, backup_dir: pathlib.Path):
def initiate_backup(sources, backup_dir: pathlib.Path, dry_run=False):
""" Main backup function """
cur_backup = pathlib.Path(
@ -77,3 +78,8 @@ def initiate_backup(sources, backup_dir: pathlib.Path):
# dst_abs = pathlib.Path(os.path.join(cur_backup, src_abs.name))
# _lg.info("Backing up directory %s to %s backup", src_abs, cur_backup.name)
# rsync(src_abs, cur_backup)
if dry_run:
_lg.info("Dry-run, removing created backup: %s", cur_backup.name)
shutil.rmtree(cur_backup, ignore_errors=True)
else:
_lg.info("Backup created: %s", cur_backup.name)

View File

@ -111,7 +111,7 @@ class Actions(enum.Enum):
create = enum.auto()
def rsync(src_dir, dst_dir=None):
def rsync(src_dir, dst_dir, dry_run=False):
"""
Do sync
:param src_dir: source dir