Skip to content

Commit

Permalink
Support for rsync logging feature. EEsync logger improved. Rsync para…
Browse files Browse the repository at this point in the history
…ms moved to GeneralSettings. Other improvements.
  • Loading branch information
kamildzi committed Feb 19, 2023
1 parent f1b10cd commit 78bce77
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
36 changes: 36 additions & 0 deletions GeneralSettings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# General settings. Adjust these to your needs.

runner = {
# Command runner (OS Exec) settings.

# Prints the external (backup) commands that are executed on your OS.
# Asks for confirmation before running the command.
Expand All @@ -9,6 +10,7 @@
}

logger = {
# Logger settings.

# Choose the logging method.
# Available methods - uncomment one:
Expand All @@ -21,3 +23,37 @@
# Blank value ("") means that logs will be saved in application directory ("./SavedLogs").
"custom_log_dir_path": ""
}

sync_rsync = {
# Rsync settings and parameters.
# Please make sure that you know what you're doing.
# Options below will affect the file-synchronization process.
# It is recommended for you to check following before you change anything in this section:
# - rsync man pages: `https://linux.die.net/man/1/rsync`, `https://linux.die.net/man/5/rsyncd.conf`
# - SyncProvider module in this project: `Src/Service/SyncProvider.py`

# Base parameters that are provided for the rsync.
"rsync_base_params": [
"-ah", "--delete"
],

# ----

# Rsync has a nice logging feature ('--log-file' parameter).
# Here, you can decide if you would like to use it or not.
# Possible values: "True" or "False".
# NOTE: Value if this setting a will also enable (or disable) other logging settings ("rsync_logging_*").
"rsync_logging_enabled": True,

# Below you may define extra parameters for the logging feature.
"rsync_logging_extra_params": [
"--log-file-format=\"[%t] [%p] %o %m %f %l\""
],

# ----

# Parameters that are added only if dry run mode is selected.
"rsync_dry_run_params": [
"--dry-run"
]
}
2 changes: 1 addition & 1 deletion Src/EESync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class EESync:
__version = '0.9.0'
__version = '0.9.1'

sync_service = SyncProvider()
crypt_service = CryptProvider()
Expand Down
7 changes: 7 additions & 0 deletions Src/IO/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def init(cls):
else:
cls.__log_dir = default_logs_directory

@classmethod
def get_log_path(cls):
"""
Returns the application log directory.
"""
return cls.__log_dir

@classmethod
def log(cls, text: str, log_level: str = None):
"""
Expand Down
4 changes: 4 additions & 0 deletions Src/Service/CommandRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def gen_run_report(exec_command, stdout, stderr) -> str:
"""
Generates formatted string from command output.
"""
if stdout is None:
stdout = str(f"{stdout} - no output or output disabled.\n")
if stderr is None:
stderr = str(f"{stderr} - no output or output disabled.\n")
return str(f"--- STDOUT: {exec_command}: ---\n"
+ str(stdout)
+ f"--- STDERR: {exec_command}: ---\n"
Expand Down
6 changes: 6 additions & 0 deletions Src/Service/CryptProvider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import re
import time
from functools import wraps

from Src.Common.BackupAction import BackupAction
Expand Down Expand Up @@ -67,6 +68,11 @@ def __unmount_encfs(self):
print("... skipped - already unmounted!")
return

# It is very likely that the target is still busy.
# Sleep is surely a bad solution, but for now - it's enough.
# TODO: check if target is busy, get rid of sleep()
time.sleep(10.0)

which_result = self.os_exec(["which", "umount"], silent=True)
umount_binary = str(which_result.stdout).strip()
exec_command: list = [umount_binary, self.config.encfs_decryption_dir]
Expand Down
26 changes: 21 additions & 5 deletions Src/Service/SyncProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import os
import re
from datetime import datetime

import GeneralSettings
from Src.Common.BackupAction import BackupAction
from Src.Config.ConfigEntry import ConfigEntry
from Src.IO.Logger import Logger
Expand Down Expand Up @@ -82,16 +84,30 @@ def __exec_rsync(self, source_dir: str, target_dir: str, dry_run: bool):
raise SystemExit("Error! Not a directory: " + path)

# prepare the command
rsync_base_params: list = [
"-avh", "--delete"
]
rsync_settings = GeneralSettings.sync_rsync
rsync_base_params: list = rsync_settings['rsync_base_params']

if dry_run:
rsync_base_params += ["--dry-run"]
rsync_base_params += rsync_settings["rsync_dry_run_params"]

if rsync_settings["rsync_logging_enabled"]:
# rsync logging - prepare the path
current_date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_dir = Logger.get_log_path()
rsync_log_path = log_dir + f"/rsync_{current_date}.log"
if not os.path.isdir(log_dir) or os.path.isfile(rsync_log_path):
raise SystemExit('Error! Rsync logging paths misconfigured!')

# rsync logging - log to a file
rsync_base_params += [f"--log-file={rsync_log_path}"]

# rsync logging - extra params
rsync_base_params += rsync_settings["rsync_logging_extra_params"]

exec_command: list = [self.binary_path] + rsync_base_params + [source_dir, target_dir]

# run the command
rsync_result = self.os_exec(exec_command, confirmation_required=True)
rsync_result = self.os_exec(exec_command, confirmation_required=True, capture_output=False)

# save the report
run_report = self.gen_run_report(exec_command, rsync_result.stdout, rsync_result.stderr)
Expand Down

0 comments on commit 78bce77

Please sign in to comment.