-
Notifications
You must be signed in to change notification settings - Fork 16
/
rollback.py
194 lines (152 loc) · 5.74 KB
/
rollback.py
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
#
# Copyright (c) 2010-2013 Liraz Siri <[email protected]>
#
# This file is part of TKLBAM (TurnKey GNU/Linux BAckup and Migration).
#
# TKLBAM is open source software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
import os
import sys
from os.path import *
import stat
import shutil
from datetime import datetime
from paths import Paths as _Paths
import mysql
import pgsql
from changes import Changes
from dirindex import DirIndex
from pkgman import Packages
import utils
import traceback
class Error(Exception):
pass
class Rollback:
Error = Error
PATH = "/var/backups/tklbam-rollback"
class Paths(_Paths):
files = [ 'etc', 'etc/mysql',
'fsdelta', 'dirindex', 'originals',
'newpkgs', 'myfs', 'pgfs' ]
@classmethod
def create(cls, path=PATH):
if exists(path):
shutil.rmtree(path)
os.makedirs(path)
os.chmod(path, 0700)
self = cls(path)
os.mkdir(self.paths.etc)
os.mkdir(self.paths.originals)
return self
def __init__(self, path=PATH):
"""deletes path if it exists and creates it if it doesn't"""
if not exists(path):
raise Error("No such directory " + `path`)
self.paths = self.Paths(path)
self.timestamp = datetime.fromtimestamp(os.stat(path).st_ctime)
@staticmethod
def _move(source, dest):
if not lexists(source):
raise Error("no such file or directory " + `source`)
if not exists(dirname(dest)):
os.makedirs(dirname(dest))
utils.remove_any(dest)
utils.move(source, dest)
def _move_to_originals(self, source):
"""Move source into originals"""
dest = join(self.paths.originals, source.strip('/'))
self._move(source, dest)
def _move_from_originals(self, dest):
"""Move path from originals to dest"""
source = join(self.paths.originals, dest.strip('/'))
self._move(source, dest)
def rollback_files(self):
if not exists(self.paths.fsdelta):
return
changes = Changes.fromfile(self.paths.fsdelta)
dirindex = DirIndex(self.paths.dirindex)
exceptions = 0
for change in changes:
try:
if change.path not in dirindex:
utils.remove_any(change.path)
continue
if change.OP in ('o', 'd'):
try:
self._move_from_originals(change.path)
except self.Error:
continue
dirindex_rec = dirindex[change.path]
local_rec = DirIndex.Record.frompath(change.path)
if dirindex_rec.uid != local_rec.uid or \
dirindex_rec.gid != local_rec.gid:
os.lchown(change.path, dirindex_rec.uid, dirindex_rec.gid)
if dirindex_rec.mod != local_rec.mod:
mod = stat.S_IMODE(dirindex_rec.mod)
os.chmod(change.path, mod)
except:
exceptions += 1
# fault-tolerance: warn and continue, don't die
traceback.print_exc(file=sys.stderr)
for fname in ('passwd', 'group'):
shutil.copy(join(self.paths.etc, fname), "/etc")
if exceptions:
raise Error("caught %d exceptions during rollback_files" % exceptions)
def rollback_new_packages(self):
if not exists(self.paths.newpkgs):
return
rollback_packages = Packages.fromfile(self.paths.newpkgs)
current_packages = Packages()
purge_packages = current_packages & rollback_packages
if purge_packages:
os.system("DEBIAN_FRONTEND=noninteractive dpkg --purge " + " ".join(purge_packages))
def rollback_database(self):
if exists(self.paths.myfs):
mysql.restore(self.paths.myfs, self.paths.etc.mysql,
add_drop_database=True)
if exists(self.paths.pgfs):
pgsql.restore(self.paths.pgfs)
def rollback(self):
exceptions = 0
for method in (self.rollback_database, self.rollback_files, self.rollback_new_packages):
try:
method()
except:
exceptions += 1
print >> sys.stderr, "error: %s raised an exception:" % method.__name__
traceback.print_exc(file=sys.stderr)
shutil.rmtree(self.paths)
if exceptions:
raise Error("caught %d exceptions during rollback" % exceptions)
def save_files(self, changes, overlay_path):
for fname in ("passwd", "group"):
shutil.copy(join("/etc", fname), self.paths.etc)
changes.tofile(self.paths.fsdelta)
di = DirIndex()
for change in changes:
if lexists(change.path):
di.add_path(change.path)
if change.OP in ('o', 'd'):
if change.OP == 'o' and not lexists(overlay_path + change.path):
continue
self._move_to_originals(change.path)
di.save(self.paths.dirindex)
def save_new_packages(self, packages):
packages = list(packages)
packages.sort()
fh = file(self.paths.newpkgs, "w")
for package in packages:
print >> fh, package
fh.close()
def save_database(self):
try:
mysql.backup(self.paths.myfs, self.paths.etc.mysql)
except mysql.Error:
pass
try:
pgsql.backup(self.paths.pgfs)
except pgsql.Error:
pass