Remove created backup if fail
This commit is contained in:
parent
126ae85388
commit
bddf311d99
@ -9,7 +9,7 @@ import shutil
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from spqr.curateipsum.fs import hardlink_dir, rsync
|
import spqr.curateipsum.fs as fs
|
||||||
|
|
||||||
BACKUP_ENT_FMT = "%y%m%d_%H%M"
|
BACKUP_ENT_FMT = "%y%m%d_%H%M"
|
||||||
_lg = logging.getLogger(__name__)
|
_lg = logging.getLogger(__name__)
|
||||||
@ -71,13 +71,12 @@ def initiate_backup(sources, backup_dir: pathlib.Path, dry_run=False):
|
|||||||
cur_backup.name,
|
cur_backup.name,
|
||||||
)
|
)
|
||||||
|
|
||||||
hardlink_dir(latest_backup, cur_backup)
|
hl_res = fs.hardlink_dir(latest_backup, cur_backup)
|
||||||
|
if not hl_res:
|
||||||
# for src in sources:
|
_lg.error("Something went wrong during copying data from latest backup,"
|
||||||
# src_abs = pathlib.Path(os.path.abspath(src))
|
" removing created %s", cur_backup.name)
|
||||||
# dst_abs = pathlib.Path(os.path.join(cur_backup, src_abs.name))
|
shutil.rmtree(cur_backup, ignore_errors=True)
|
||||||
# _lg.info("Backing up directory %s to %s backup", src_abs, cur_backup.name)
|
return
|
||||||
# rsync(src_abs, cur_backup)
|
|
||||||
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)
|
||||||
shutil.rmtree(cur_backup, ignore_errors=True)
|
shutil.rmtree(cur_backup, ignore_errors=True)
|
||||||
|
|||||||
@ -209,7 +209,7 @@ def rsync(src_dir, dst_dir, dry_run=False):
|
|||||||
copy_direntry(src_entry, dst_path)
|
copy_direntry(src_entry, dst_path)
|
||||||
|
|
||||||
|
|
||||||
def _hardlink_dir_ext(src, dst):
|
def _hardlink_dir_ext(src, dst) -> 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.
|
||||||
@ -222,16 +222,16 @@ def _hardlink_dir_ext(src, dst):
|
|||||||
cp = "cp"
|
cp = "cp"
|
||||||
src_content = glob.glob(f"{src}/*")
|
src_content = glob.glob(f"{src}/*")
|
||||||
cmd = [cp, "--archive", "--verbose", "--link", *src_content, dst]
|
cmd = [cp, "--archive", "--verbose", "--link", *src_content, dst]
|
||||||
_lg.debug("Executing external command: %s", " ".join(cmd))
|
_lg.info("Executing external command: %s", " ".join(cmd))
|
||||||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
with process.stdout:
|
with process.stdout:
|
||||||
for line in iter(process.stdout.readline, b""):
|
for line in iter(process.stdout.readline, b""):
|
||||||
logging.debug("%s: %s", cp, line.decode("utf-8").strip())
|
logging.debug("%s: %s", cp, line.decode("utf-8").strip())
|
||||||
exitcode = process.wait()
|
exitcode = process.wait()
|
||||||
return exitcode
|
return not bool(exitcode)
|
||||||
|
|
||||||
|
|
||||||
def _recursive_hardlink(src, dst):
|
def _recursive_hardlink(src, dst) -> 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.
|
||||||
@ -261,13 +261,15 @@ def _recursive_hardlink(src, dst):
|
|||||||
# something that is not a file, symlink or directory
|
# something that is not a file, symlink or directory
|
||||||
raise NotImplementedError(ent.path)
|
raise NotImplementedError(ent.path)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def hardlink_dir(src_dir, dst_dir):
|
|
||||||
|
def hardlink_dir(src_dir, dst_dir) -> 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
|
||||||
:return: None
|
:return: boolean result
|
||||||
"""
|
"""
|
||||||
_lg.info(f"Recursive hardlinking: {src_dir} -> {dst_dir}")
|
_lg.info(f"Recursive hardlinking: {src_dir} -> {dst_dir}")
|
||||||
src_abs = os.path.abspath(src_dir)
|
src_abs = os.path.abspath(src_dir)
|
||||||
@ -283,8 +285,5 @@ def hardlink_dir(src_dir, dst_dir):
|
|||||||
|
|
||||||
_lg.debug(f"Creating directory: {dst_abs}")
|
_lg.debug(f"Creating directory: {dst_abs}")
|
||||||
os.mkdir(dst_abs)
|
os.mkdir(dst_abs)
|
||||||
res = _hardlink_dir_ext(src_abs, dst_abs)
|
|
||||||
if res:
|
return _hardlink_dir_ext(src_abs, dst_abs)
|
||||||
_lg.error("Something went wrong during hardlink_dir, removing created dest dir: %s", dst_abs)
|
|
||||||
shutil.rmtree(dst_abs, ignore_errors=True)
|
|
||||||
return
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user