summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2021-10-09 12:49:40 +0200
committerxengineering <me@xengineering.eu>2021-10-09 12:49:40 +0200
commit81c450a5cc5435b19a3655b4a3dc2bc7d63b93ce (patch)
treebe2d47826b83914adb502eea1f8fdd0baa906aeb
parentf28fe2c372eefd64bbe3e6b2837b0734e002c3cc (diff)
downloadxbackup-81c450a5cc5435b19a3655b4a3dc2bc7d63b93ce.tar
xbackup-81c450a5cc5435b19a3655b4a3dc2bc7d63b93ce.tar.zst
xbackup-81c450a5cc5435b19a3655b4a3dc2bc7d63b93ce.zip
Implement xbackup/utils Module and enable Prune
-rw-r--r--src/xbackup/backup.py31
-rw-r--r--src/xbackup/prune.py21
-rw-r--r--src/xbackup/utils.py36
3 files changed, 61 insertions, 27 deletions
diff --git a/src/xbackup/backup.py b/src/xbackup/backup.py
index 25b26f1..8b7ee78 100644
--- a/src/xbackup/backup.py
+++ b/src/xbackup/backup.py
@@ -3,15 +3,17 @@
# pylint: disable=too-few-public-methods
-"""Backup Functionality of xbackup"""
+"""backup.py
+
+This module contains the backup functionality of xbackup.
+"""
import os
import sys
import subprocess
-
-POSITIVE_ANSWERS = ["Y", "y", "Yes", "yes", "YES"]
+from xbackup.utils import POSITIVE_ANSWERS,shell
def backup(config, scripted):
@@ -26,7 +28,7 @@ def backup(config, scripted):
answer = input("\nDo you want to continue? [y/N] ")
if answer not in POSITIVE_ANSWERS:
print("\nTerminated.")
- sys.exit(0)
+ return
# init borg repository (accepting failure on already existing repo)
shell(f"borg init -e none {paths.borg_repo}", panic=False)
@@ -75,27 +77,6 @@ class Filepaths():
return retval
-def shell(command, panic=True):
- """Savely execute a Shell Command
-
- - set panic=False to continue with execution on non-zero return code
- """
-
- # print command
- print("\nExecuting '" + command + "' ...")
-
- # command execution
- return_code = subprocess.call(command, shell=True)
-
- # handle non-zero return code
- if return_code != 0 and panic:
- print(f"Command '{command}'\nfailed with return code {return_code}")
- sys.exit(return_code)
-
- # final message and return
- print("... done!")
-
-
def run_backup(paths):
"""Execute the Borg Backup"""
diff --git a/src/xbackup/prune.py b/src/xbackup/prune.py
index 77085d0..7b41b03 100644
--- a/src/xbackup/prune.py
+++ b/src/xbackup/prune.py
@@ -9,6 +9,9 @@ This module contains the pruning functionality of xbackup.
import os
+import sys
+
+from xbackup.utils import POSITIVE_ANSWERS,shell
def prune(backup_cfg, prune_cfg, scripted):
@@ -29,5 +32,19 @@ def prune(backup_cfg, prune_cfg, scripted):
cmd = f"borg prune -v {repo}"
cmd += f" -H {hourly} -d {daily} -w {weekly} -m {monthly} -y {yearly}"
- # print command for debugging
- print("\nCould execute prune command like this:\n'" + cmd + "'")
+ # ask if execution is wanted
+ if not scripted:
+ text = f"\nGoing to prune your backups at '{repo}'."
+ text += "\nThis will delete every backup except:"
+ text += f"\n- {hourly} hourly backups"
+ text += f"\n- {daily} daily backups"
+ text += f"\n- {weekly} weekly backups"
+ text += f"\n- {monthly} monthly backups"
+ text += f"\n- {yearly} yearly backups"
+ text += "\nDo you want to continue? [y/N] "
+ answer = input(text)
+ if answer not in POSITIVE_ANSWERS:
+ return
+
+ # execute pruning
+ shell(cmd, panic=True)
diff --git a/src/xbackup/utils.py b/src/xbackup/utils.py
new file mode 100644
index 0000000..e1480d1
--- /dev/null
+++ b/src/xbackup/utils.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+# vim: shiftwidth=4 softtabstop=4 tabstop=4 expandtab
+
+
+"""utils.py
+
+This module contains reusable code for xbackup.
+"""
+
+
+import sys
+import subprocess
+
+
+POSITIVE_ANSWERS = ["Y", "y", "Yes", "yes", "YES"]
+
+
+def shell(command, panic=True):
+ """Savely execute a Shell Command
+
+ - set panic=False to continue with execution on non-zero return code
+ """
+
+ # print command
+ print("\nExecuting '" + command + "' ...")
+
+ # command execution
+ return_code = subprocess.call(command, shell=True)
+
+ # handle non-zero return code
+ if return_code != 0 and panic:
+ print(f"Command '{command}'\nfailed with return code {return_code}")
+ sys.exit(return_code)
+
+ # final message and return
+ print("... done!")