From 232f988e343c9824193d3a2b9121e6f2b5d1f43d Mon Sep 17 00:00:00 2001 From: Per Jensen Date: Fri, 9 Aug 2024 16:02:55 +0200 Subject: [PATCH] cleanup use PREREQ and systemd examples --- v2/README.md | 51 +++++++++++++++++++++++++++++++++++++ v2/dar_backup/cleanup.py | 19 +++++++++++++- v2/dar_backup/dar_backup.py | 2 +- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/v2/README.md b/v2/README.md index b598abe..4ce69cc 100644 --- a/v2/README.md +++ b/v2/README.md @@ -332,6 +332,57 @@ verbose: # -va ```` +# Systemctl examples +I have dar-backup scheduled to run via systemd --user settings. + +The files are located in: ~/.config/systemd/user + +Once the .service and .timer files are in place, timers must be enabled and started. + +```` +systemctl --user enable dar-inc-backup.timer +systemctl --user start dar-inc-backup.timer +systemctl --user daemon-reload +```` + +Verify your timers are set up as you want: + +```` +systemctl --user list-timers +```` + + +## Service: dar-back --incremental-backup + +File: dar-inc-backup.service +```` +[Unit] +Description=dar-backup INC +StartLimitIntervalSec=120 +StartLimitBurst=1 +[Service] +Type=oneshot +TimeoutSec=infinity +RemainAfterExit=no +ExecStart=/bin/bash -c '. /home/user/programmer/dar-backup.py/venv/bin/activate && dar-backup --incremental-backup --verbose' +```` + +## Timer: dar-back --incremental-backup + +File: dar-inc-backup.timer +```` +[Unit] +Description=dar-backup INC timer + +[Timer] +OnCalendar=*-*-04/3 19:03:00 +Persistent=true + +[Install] +WantedBy=timers.target +```` + + # list contents of an archive ``` . /bin/activate diff --git a/v2/dar_backup/cleanup.py b/v2/dar_backup/cleanup.py index d098a8e..43d2183 100644 --- a/v2/dar_backup/cleanup.py +++ b/v2/dar_backup/cleanup.py @@ -18,6 +18,7 @@ import logging import os import re +import subprocess import sys from datetime import datetime, timedelta @@ -158,6 +159,22 @@ def main(): args.verbose and (print(f"--cleanup-specific-archive: {args.cleanup_specific_archive}")) + # run PREREQ scripts + if 'PREREQ' in config_settings.config: + for key in sorted(config_settings.config['PREREQ'].keys()): + script = config_settings.config['PREREQ'][key] + try: + result = subprocess.run(script, shell=True, check=True) + logger.info(f"PREREQ {key}: '{script}' run, return code: {result.returncode}") + logger.info(f"PREREQ stdout:\n{result.stdout}") + except subprocess.CalledProcessError as e: + logger.error(f"Error executing {key}: '{script}': {e}") + if result: + logger.error(f"PREREQ stderr:\n{result.stderr}") + print(f"Error executing {script}: {e}") + sys.exit(1) + + if args.alternate_archive_dir: config_settings.backup_dir = args.alternate_archive_dir @@ -187,7 +204,7 @@ def main(): if len(error_lines) > 0: args.verbose and print("\033[1m\033[31mErrors\033[0m encountered") for line in error_lines: - print(line) + args.verbose and print(line) sys.exit(1) else: args.verbose and print("\033[1m\033[32mSUCCESS\033[0m No errors encountered") diff --git a/v2/dar_backup/dar_backup.py b/v2/dar_backup/dar_backup.py index 2b6d37a..580b2e3 100644 --- a/v2/dar_backup/dar_backup.py +++ b/v2/dar_backup/dar_backup.py @@ -660,7 +660,7 @@ def main(): if len(error_lines) > 0: args.verbose and print("\033[1m\033[31mErrors\033[0m encountered") for line in error_lines: - print(line) + args.verbose and print(line) sys.exit(1) else: args.verbose and print("\033[1m\033[32mSUCCESS\033[0m No errors encountered")