summaryrefslogtreecommitdiff
path: root/obnam/oper_restore.py
blob: 311297919074f4b78ab4124fa78035b8921eb6c4 (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
# Copyright (C) 2008  Lars Wirzenius <liw@iki.fi>
#
# This program is free 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Operation to restore from backup."""


import logging
import os
import stat

import obnam


class UnknownGeneration(obnam.ObnamException):

    def __init__(self, gen_id):
        self._msg = "Can't find generation %s" % gen_id


class Restore(obnam.Operation):

    """Restore specified files (or all) from a specified generation."""
    
    name = "restore"

    def hardlink_key(self, st):
        """Compute key into hardlink lookup table from stat result"""
        return "%d/%d" % (st.st_ino, st.st_dev)

    def create_filesystem_object(self, hardlinks, full_pathname, inode):
        context = self.get_application().get_context()
        logging.debug("Creating filesystem object %s" % full_pathname)
        stat_component = inode.first_by_kind(obnam.cmp.STAT)
        st = obnam.cmp.parse_stat_component(stat_component)
        mode = st.st_mode
    
        if st.st_nlink > 1 and not stat.S_ISDIR(mode):
            key = self.hardlink_key(st)
            if key in hardlinks:
                existing_link = hardlinks[key]
                os.link(existing_link, full_pathname)
                return
            else:
                hardlinks[key] = full_pathname
    
        if stat.S_ISDIR(mode):
            if not os.path.exists(full_pathname):
                os.makedirs(full_pathname, 0700)
        elif stat.S_ISREG(mode):
            basedir = os.path.dirname(full_pathname)
            if not os.path.exists(basedir):
                os.makedirs(basedir, 0700)
            fd = os.open(full_pathname, os.O_WRONLY | os.O_CREAT, 0)
            cont_id = inode.first_string_by_kind(obnam.cmp.CONTREF)
            if cont_id:
                obnam.io.copy_file_contents(context, fd, cont_id)
            else:
                delta_id = inode.first_string_by_kind(obnam.cmp.DELTAREF)
                obnam.io.reconstruct_file_contents(context, fd, delta_id)
            os.close(fd)

    def restore_requested(self, files, pathname):
        """Return True, if pathname should be restored"""
        
        # If there is no explicit file list, restore everything.
        if not files:
            return True
            
        # If the pathname is specified explicitly, restore it.
        if pathname in files:
            return True
            
        # Otherwise, if there's an explicitly specified filename that is a
        # prefix of directory parts in the pathname, restore it. That is,
        # if files is ["foo/bar"], then restore "foo/bar/baz", but not
        # "foo/barbell".
        for x in files:
            if pathname.startswith(x) and x.endswith(os.sep):
                return True
            if pathname.startswith(x + os.sep):
                return True
                
        # Nope, don't restore it.
        return False

    def restore_single_item(self, hardlinks, target, pathname, inode):
        logging.debug("Restoring %s" % pathname)

        if pathname.startswith(os.sep):
            pathname = "." + pathname
        full_pathname = os.path.join(target, pathname)

        self.create_filesystem_object(hardlinks, full_pathname, inode)
        return full_pathname

    def fix_permissions(self, list):
        logging.debug("Fixing permissions")
        list.sort()
        for full_pathname, inode in list:
            obnam.io.set_inode(full_pathname, inode)

    def restore_from_filelist(self, target, fl, files):
        logging.debug("Restoring files from FILELIST")
        list = []
        hardlinks = {}

        for c in fl.find_by_kind(obnam.cmp.FILE):
            pathname = c.first_string_by_kind(obnam.cmp.FILENAME)
    
            if not self.restore_requested(files, pathname):
                logging.debug("Restore of %s not requested" % pathname)
                continue
    
            full_pathname = self.restore_single_item(hardlinks, target, 
                                                     pathname, c)
            list.append((full_pathname, c))

        self.fix_permissions(list)

    def restore_from_filegroups(self, target, hardlinks, list, parent, 
                                filegrouprefs, files):
        for ref in filegrouprefs:
            fg = obnam.io.get_object(self.app.get_context(), ref)
            if not fg:
                logging.warning("Cannot find FILEGROUP object %s" % ref)
            else:
                for name in fg.get_names():
                    if parent:
                        name2 = os.path.join(parent, name)
                    if self.restore_requested(files, name2):
                        file = fg.get_file(name)
                        full_pathname = self.restore_single_item(hardlinks,
                                            target, name2, file)
                        list.append((full_pathname, file))
                    else:
                        logging.debug("Restore of %s not requested" % name2)

    def restore_from_dirs(self, target, hardlinks, list, parent, dirrefs, 
                          files):
        for ref in dirrefs:
            dir = obnam.io.get_object(self.app.get_context(), ref)
            if not dir:
                logging.warning("Cannot find DIR object %s" % ref)
            else:
                name = dir.get_name()
                if parent:
                    name = os.path.join(parent, name)
                if self.restore_requested(files, name):
                    st = dir.first_by_kind(obnam.cmp.STAT)
                    st = obnam.cmp.parse_stat_component(st)
                    file = \
                        obnam.filelist.create_file_component_from_stat(
                            dir.get_name(), st, None, None, None)
                    full_pathname = self.restore_single_item(hardlinks,
                                                             target, name,
                                                             file)
                    list.append((full_pathname, file))
                    self.restore_from_filegroups(target, hardlinks, list, 
                                                 name,
                                                 dir.get_filegrouprefs(),
                                                 files)
                    self.restore_from_dirs(target, hardlinks, list, name,
                                           dir.get_dirrefs(), files)
                else:
                    logging.debug("Restore of %s not requested" % name)

    def restore_from_dirs_and_filegroups(self, target, gen, files):
        hardlinks = {}
        list = []
        self.restore_from_filegroups(target, hardlinks, list, None, 
                                     gen.get_filegrouprefs(), files)
        self.restore_from_dirs(target, hardlinks, list, 
                               None, gen.get_dirrefs(), files)
        self.fix_permissions(list)

    def do_it(self, args):
        gen_id = args[0]
        files = args[1:]
        logging.debug("Restoring generation %s" % gen_id)
        logging.debug("Restoring files: %s" % ", ".join(files))
    
        self.app = app = self.get_application()
        context = app.get_context()
        host = app.load_host()
    
        app.load_maps()
        app.load_content_maps()
    
        logging.debug("Getting generation object")    
        gen = obnam.io.get_object(context, gen_id)
        if gen is None:
            raise UnknownGeneration(gen_id)
        
        target = context.config.get("backup", "target-dir")
        logging.debug("Restoring files under %s" % target)
    
        fl_id = gen.get_filelistref()
        if fl_id:
            logging.debug("Getting list of files in generation")
            fl = obnam.io.get_object(context, fl_id)
            if not fl:
                logging.warning("Cannot find file list object %s" % fl_id)
            else:
                self.restore_from_filelist(target, fl, files)
        else:
            self.restore_from_dirs_and_filegroups(target, gen, files)