aboutsummaryrefslogtreecommitdiff
path: root/restic.py
blob: 06ad12c3d78af1d883b5a043b1477db3846da4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
BSD 2-Clause License
restic subprocess wrapper
Copyright (c) 2018, Max Resnick <max@ofmax.li>
All rights reserved.

restic-py is simple wrapper to restic binary for backups and monitoring
"""
import os
import configparser
import argparse
import logging
import sys
import time
import socket
import getpass
import subprocess
import pathlib
import collections
import shlex

# logging
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',
    '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}
log_args['level'] = LOGLEVELS[os.getenv('RESTIC_LOGLEVEL', 'INFO')]
logging.basicConfig(**log_args)
log = logging.getLogger(__name__)
# End Log Config


def prepare_args():
    """prepare arg parser"""
    parser = argparse.ArgumentParser(description='wrapper to restic')
    parser.add_argument('-c', '--config',
                        default=f'{HOME_DIR}/.local/etc/restic-wrapper/restic.ini',
                        type=pathlib.Path)
    parser.add_argument('-e', '--exclude-file',
                        type=pathlib.Path)
    parser.add_argument('-n', '--namespace')
    parser.add_argument('-u', '--restic-uri')
    parser.add_argument('-k', '--password-file')
    subparsers = parser.add_subparsers(title='wrapper commands',
                                       description='valid subcommands',
                                       required=True)
    backup_parser = subparsers.add_parser('backup')
    backup_parser.set_defaults(func=run_backup)
    backup_parser.add_argument('path',
                               nargs='?',
                               default=pathlib.Path('.'),
                               help='path to backup',
                               type=pathlib.Path)
    prune_parser = subparsers.add_parser('prune')
    prune_parser.set_defaults(func=run_prune)
    check_parser = subparsers.add_parser('check')
    check_parser.set_defaults(func=run_check)
    watch_parser = subparsers.add_parser('watch')
    watch_parser.set_defaults(func=run_watch)
    pre_operation_check_parser = subparsers.add_parser('validate-config')
    pre_operation_check_parser.set_defaults(func=run_pre_operation_check)
    args = parser.parse_args()
    # load ini file
    cfg = configparser.ConfigParser()
    cfg.read(args.config)
    # build a dictionary of from restic ini section
    config_file_args = dict(cfg.items('restic'))
    # build dictionary of the args
    command_line_args = {k: v for k, v in vars(args).items() if v is not None}
    combined = collections.ChainMap(command_line_args,
                                    os.environ,
                                    config_file_args,
                                    DEFAULTZ)
    return combined


# subprocess utils
def run(cmd, *cmd_args, stdin=None, cstdout=False, cstderr=False, cwd=None):
    """runs a given command"""
    runcmd = [cmd, *cmd_args]
    runcmd = [str(s) for s in runcmd]
    runcmd = shlex.join(runcmd)
    stderr_pipe = subprocess.PIPE if cstderr else None
    stdout_pipe = subprocess.PIPE if cstdout else 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
    if not stdout:
        stdout = b''
    if not stderr:
        stderr = b''
    exit_clean = _r.returncode == 0
    return exit_clean, stdout, stderr
# end suprocess utils


def resolve_restic():
    """resolve path torestic binary"""
    env_paths = os.environ.get('PATH', '').split(os.pathsep)
    for env_path in env_paths:
        exec_bin = pathlib.Path(env_path) / 'restic'
        if not exec_bin.is_file():
            continue
        return exec_bin
    return ''

# pylint: disable=unused-argument


def run_pre_operation_check(rconf, args):
    """validate authentication, repo access, and configuration"""
    log.info('validating config')
    if not check_auth():
        return False
    status, _, stderr = run(
        rconf.exec_bin,
        'cat', 'config',
        *rconf.base_args,
        cstderr=1,
        cstdout=1)
    if not status:
        log.error('pre repo check failed: %s', stderr)
        return False
    return True


def check_auth():
    """check authentication with restic backend"""
    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):
    """backup a given directory"""
    try:
        log.info('restic-backup start')
        run(rconf.exec_bin,
            'backup',
            '--exclude-caches',
            *rconf.exclude,
            argz['path'],
            *rconf.base_args)
    except Exception as error:
        log.error('restic-run failed %s', error)
        raise error
    log.info('restic-backup finished')
    return True


def run_prune(rconf, argz):
    """run prune task"""
    try:
        run(rconf.exec_bin,
            'prune',
            *rconf.base_args)
    # pylint: disable=broad-exception-caught
    except Exception as error:
        log.error('resitc-prune %s', error)
        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)
        if not status:
            log.error('restic-prune %s', stderr)
            return False
    # pylint: disable=broad-exception-caught
    except Exception as error:
        log.error('retsic-prune %s', error)
        return False
    return True


def run_check(rconf, args):
    """restic check command"""
    try:
        status, _, stderr = run(rconf.exec_bin,
                                'check',
                                *rconf.base_args)
        if not status:
            log.error('restic-check %s', stderr)
            return False
    # pylint: disable=broad-exception-caught
    except Exception as error:
        log.error('restic-check %s', error)
        return False
    return True


# def follow(logfile, pattern):
#     # catch up
#     for line in logfile.readlines():
#         if pattern in line:
#             yield line
#     logfile.seek(0, 2)
#     while True:
#         line = logfile.readline()
#         if not line:
#             time.sleep(0.1)
#             continue
#         if pattern in line:
#             yield line
#
#
def run_watch(args):
    logfile = open("restic.log", "r")
    loglines = follow(logfile, 'ERROR')
    for line in loglines:
        print(line)


def main():
    """main"""
    argz = prepare_args()
    # ResticCmdConfig
    restic_config = ResticCmdConfig(
        resolve_restic(),
        [
            '--password-file', argz['password_file'],
            '--repo', f'{argz["repo"]}/{argz["namespace"]}',
        ],
        [
            '--exclude-file', argz['exclude_file'],
        ]
    )
    try:
        success = argz['func'](restic_config, argz)
    # pylint: disable=broad-exception-caught
    except Exception as error:
        log.error('error running %s', error)
        sys.exit(1)
    if not success:
        sys.exit(1)
    sys.exit(0)