-
Notifications
You must be signed in to change notification settings - Fork 0
/
illbeback.sh
214 lines (179 loc) · 7.48 KB
/
illbeback.sh
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
#!/usr/bin/env python3.6
# vim: set filetype=python :
# inspired by: http://blog.interlinked.org/tutorials/rsync_time_machine.html
from subprocess import Popen, PIPE, DEVNULL
from datetime import datetime
from fcntl import lockf, LOCK_EX, LOCK_NB
import re
import sys
import os
import yaml
import argparse
# from https://stackoverflow.com/questions/4417546/ ...
# constantly-print-subprocess-output-while-process-is-running/
def run_at_source(command, display=True, timeout=15):
if display:
# print("running: {}".format(command))
popen = Popen(command, stdout=PIPE, bufsize=1, shell=True)
lines_iterator = iter(popen.stdout.readline, b"")
while popen.poll() is None:
for line in lines_iterator:
nline = line.rstrip()
print(nline.decode("utf-8"), end="\r\n", flush=True)
else:
# print("running: {}".format(command))
popen = Popen(command, stdout=PIPE, stderr=DEVNULL, shell=True)
popen.wait(timeout=timeout)
return popen
def run_at_destination(backup_destination, command, timeout=15):
if ":" in backup_destination:
command = "ssh {} {}".format(connection(backup_destination), command)
return run_at_source(command, display=False, timeout=timeout)
def path(resource):
if ":" in resource:
match = re.compile("^.*:(.*)$").match(resource)
return match.group(1)
else:
return resource
def connection(resource):
if ":" in resource:
match = re.compile("^(.*):.*$").match(resource)
return match.group(1)
else:
return None
def all_backups(backup_destination):
global all_backups_cached
if all_backups_cached is None:
all_backups_cached = run_at_destination(
backup_destination,
"ls {}".format(path(backup_destination)))
all_backups_cached = all_backups_cached.stdout.read().decode("utf-8")
all_backups_cached = all_backups_cached.splitlines()
return all_backups_cached
def completed_backups(backup_destination):
completed = []
for backup in all_backups(backup_destination):
if backup == "completed":
next
inc = re.compile("^incomplete-.*$")
if inc.match(backup):
next
completed.append(backup)
return completed
def incomplete_backup(backup_destination):
for backup in all_backups(backup_destination):
inc = re.compile("^incomplete-.*$")
if inc.match(backup):
return backup
def old_backups(backup_destination):
months_encountered = set()
backups_to_remove = set()
for backup in sorted(sorted(completed_backups(backup_destination),
reverse=True)[30:-1]):
made_on = datetime.strptime(backup, "%Y-%m-%dT%H_%M_%S").date()
made_on_month = (made_on.year, made_on.month)
if made_on_month in months_encountered:
backups_to_remove.add(backup)
else:
months_encountered.add(made_on_month)
return backups_to_remove
def delete_old_backups(backup_destination):
for backup in sorted(old_backups(backup_destination)):
backup_path = "{}/{}".format(path(backup_destination), backup)
print("deleting {}".format(backup_path))
run_at_destination(backup_destination,
"rm -rf {}".format(backup_path), timeout=300)
def main():
os.nice(20)
command = 'route -n show -inet | grep default | head -1 | awk \'{print $8}\' | grep ppp'
proc = run_at_source(command)
if proc.returncode == 0:
print('warning: on a wwan connection, skipping backup')
sys.exit()
ap = argparse.ArgumentParser(
description='backup a directory with versioning, kinda like Apple\'s \
Time Machine, but based on rsync instead')
ap.add_argument('-c', '--config', default='~/.illbeback',
help='config file to use, defaults to ~/.illbeback')
args = ap.parse_args()
config = yaml.safe_load(open(os.path.expanduser(args.config)))
backup_sources = config['source']
if backup_sources.__class__ == str:
backup_sources = {'default': backup_sources}
backup_destination = os.path.join(config['destination'], config['name'])
dont_backup = config.get('exclude', [])
name = config['name']
lockfile = open("/tmp/illbeback-{}.lockfile".format(name),
mode="w")
try:
lockf(lockfile, LOCK_EX | LOCK_NB)
except BlockingIOError:
print("error: another instance is running")
sys.exit()
global all_backups_cached
all_backups_cached = None
is_first_backup = "current" not in all_backups(backup_destination)
if incomplete_backup(backup_destination) is not None:
backup_date = re.compile("^incomplete-(.*?)$").match(
incomplete_backup(backup_destination)).group(1)
action = "resuming the previous"
else:
backup_date = datetime.now().strftime('%Y-%m-%dT%H_%M_%S')
action = "starting a new"
if is_first_backup:
action += " (and first)"
print("{} backup: {}".format(action, backup_date))
sum_of_returncodes = 0
for key in backup_sources:
source = os.path.expanduser(backup_sources[key])
if len(backup_sources) == 1:
source = "{}/".format(source)
destination = "{}/incomplete-{}".format(backup_destination,
backup_date)
else:
source = "{}/".format(source)
destination = "{}/incomplete-{}/{}/".format(
backup_destination,
backup_date,
key)
rsync_params = ["-azvP", "--delete", "--delete-excluded"]
if not is_first_backup:
if len(backup_sources) == 1:
rsync_params.append("--link-dest=../current")
else:
rsync_params.append("--link-dest=../../current/{}".format(key))
for item in dont_backup:
rsync_params.append("--exclude \"{}\"".format(item))
rsync_params.append("{} {}".format(source, destination))
newdir = backup_destination.split(':')[1]
command = "mkdir -p {}".format(newdir)
proc = run_at_destination(backup_destination, command)
if proc.returncode != 0:
print("Error: rc={} stdout={} command={}".format(
proc.returncode, proc.stdout.read(), command))
raise
command = "rsync {} 2>&1".format(" ".join(rsync_params))
proc = run_at_source(command)
sum_of_returncodes += proc.returncode
if sum_of_returncodes == 0:
print("backup complete")
print("updating the 'current' backup link")
commands = []
commands.append("mv {}/incomplete-{} {}/{}".format(
path(backup_destination),
backup_date,
path(backup_destination),
backup_date))
commands.append("rm -f {}/current".format(path(backup_destination)))
commands.append("ln -s {}/{} {}/current".format(
path(backup_destination),
backup_date,
path(backup_destination)))
for command in commands:
proc = run_at_destination(backup_destination, command)
if proc.returncode != 0:
print("Error: rc={} stdout={} command={}".format(
proc.returncode, proc.stdout.read(), command))
raise
delete_old_backups(backup_destination)
main()