aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Resnick <max@ofmax.li>2024-01-15 14:17:36 -0800
committerMax Resnick <max@ofmax.li>2024-01-15 14:17:36 -0800
commitf3f3eb73aa8a1029698226f80e6e3e12b5b2c2d4 (patch)
treecc676cecc48e12b03683f80444347d7494349cce
parente6978fb2f433ce4ff414520769a01a34eb0545bf (diff)
downloadrestic-wrapper-f3f3eb73aa8a1029698226f80e6e3e12b5b2c2d4.tar.gz
loggging updates
-rw-r--r--VERSION2
-rw-r--r--restic.py73
-rw-r--r--setup.py1
3 files changed, 46 insertions, 30 deletions
diff --git a/VERSION b/VERSION
index 8acdd82..99d85ec 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.0.1
+0.0.6 \ No newline at end of file
diff --git a/restic.py b/restic.py
index 0d8a0b0..06ad12c 100644
--- a/restic.py
+++ b/restic.py
@@ -20,22 +20,31 @@ import collections
import shlex
# logging
-LOGLEVELS = {'DEBUG': logging.DEBUG,
- 'INFO': logging.INFO,
- 'WARNING': logging.WARNING,
- 'ERROR': logging.ERROR}
+LOGLEVELS = {
+ 'DEBUG': logging.DEBUG,
+ 'INFO': logging.INFO,
+ 'WARNING': logging.WARNING,
+ 'ERROR': logging.ERROR
+}
+
HOME_DIR = os.path.expanduser('~')
DEFAULTZ = {
'namespace': f'{getpass.getuser()}-{socket.gethostname()}',
'exclude_file': pathlib.Path(f'{HOME_DIR}/.local/etc/restic-wrapper/exclude.conf'),
'password_file': pathlib.Path(f'{HOME_DIR}/.ssh/resticpass'),
}
+
ResticCmdConfig = collections.namedtuple(
'ResticCmdConfig', ['exec_bin', 'base_args', 'exclude']
)
# Log Config
-log_args = {'filename': f'{HOME_DIR}/.local/logs/restic.log'}
+log_args = {
+ 'filename': f'{HOME_DIR}/.local/logs/restic.log',
+ 'format': '%(asctime)s %(levelname)s %(message)s',
+ #'datefmt': '%m/%d/%Y %I:%M:%S %p'
+}
+
if sys.stdin.isatty():
log_file = sys.stdout
log_args = {'stream': sys.stdout}
@@ -100,7 +109,7 @@ def run(cmd, *cmd_args, stdin=None, cstdout=False, cstderr=False, cwd=None):
_r = subprocess.run(shlex.split(runcmd), stdout=stdout_pipe, stderr=stderr_pipe,
input=stdin, cwd=cwd, check=False)
stdout = _r.stdout
- stderr =_r.stderr
+ stderr = _r.stderr
if not stdout:
stdout = b''
if not stderr:
@@ -121,6 +130,8 @@ def resolve_restic():
return ''
# pylint: disable=unused-argument
+
+
def run_pre_operation_check(rconf, args):
"""validate authentication, repo access, and configuration"""
log.info('validating config')
@@ -140,15 +151,18 @@ def run_pre_operation_check(rconf, args):
def check_auth():
"""check authentication with restic backend"""
- try:
- # pylint: disable=pointless-statement
- os.environ['AWS_SECRET_ACCESS_KEY']
- # pylint: disable=pointless-statement
- os.environ['AWS_ACCESS_KEY_ID']
- except KeyError:
- log.error('AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID are required.')
- return False
- return True
+ sec_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
+ access_key = os.environ.get('AWS_ACCESS_KEY_ID')
+ cred_file = os.environ.get('AWS_SHARED_CREDENTIALS_FILE')
+ if sec_key and access_key:
+ return True
+ if cred_file:
+ return True
+ log.error("""
+ AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID
+ or AWS_SHARED_CREDENTIALS_FILE are required.
+ """)
+ return False
def run_backup(rconf, argz):
@@ -167,6 +181,7 @@ def run_backup(rconf, argz):
log.info('restic-backup finished')
return True
+
def run_prune(rconf, argz):
"""run prune task"""
try:
@@ -179,16 +194,17 @@ def run_prune(rconf, argz):
return False
return True
+
def run_forget(rconf, argz):
"""forget backups that are no longer needed"""
try:
status, _, stderr = run(rconf.exec_bin,
- 'forget',
- '--keep-daily', 7,
- '--keep-weekly', 5,
- '--keep-monthly', 12,
- '--keep-yearly', 75,
- *rconf.base_args)
+ 'forget',
+ '--keep-daily', 7,
+ '--keep-weekly', 5,
+ '--keep-monthly', 12,
+ '--keep-yearly', 75,
+ *rconf.base_args)
if not status:
log.error('restic-prune %s', stderr)
return False
@@ -198,6 +214,7 @@ def run_forget(rconf, argz):
return False
return True
+
def run_check(rconf, args):
"""restic check command"""
try:
@@ -229,12 +246,12 @@ def run_check(rconf, args):
# yield line
#
#
-# def run_watch(args):
-# logfile = open("restic.log", "r")
-# loglines = follow(logfile, 'ERROR')
-# for line in loglines:
-# print(line)
-#
+def run_watch(args):
+ logfile = open("restic.log", "r")
+ loglines = follow(logfile, 'ERROR')
+ for line in loglines:
+ print(line)
+
def main():
"""main"""
@@ -258,4 +275,4 @@ def main():
sys.exit(1)
if not success:
sys.exit(1)
- sys.exit(1)
+ sys.exit(0)
diff --git a/setup.py b/setup.py
index 226e1e8..9275282 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,6 @@ setup(name='restic',
'console_scripts': ['rw=restic:main']
},
packages=[],
- install_requires=['sh'],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',