File: Backup.py

package info (click to toggle)
pyrite 0.9.3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,504 kB
  • ctags: 1,924
  • sloc: python: 6,064; ansic: 5,094; makefile: 275; sh: 172
file content (188 lines) | stat: -rw-r--r-- 6,068 bytes parent folder | download
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
#
#  $Id: Backup.py,v 1.24 1999/12/11 12:35:11 rob Exp $
#
#  Copyright 1998-1999 Rob Tillotson <robt@debian.org>
#  All Rights Reserved
#
#  Permission to use, copy, modify, and distribute this software and
#  its documentation for any purpose and without fee or royalty is
#  hereby granted, provided that the above copyright notice appear in
#  all copies and that both the copyright notice and this permission
#  notice appear in supporting documentation or portions thereof,
#  including modifications, that you you make.
#
#  THE AUTHOR ROB TILLOTSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
#  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
#  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
#  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE!
#
"""Backup.
"""

__version__ = '$Id: Backup.py,v 1.24 1999/12/11 12:35:11 rob Exp $'

__copyright__ = 'Copyright 1998-1999 Rob Tillotson <robt@debian.org>'

import Pyrite
import Pyrite.Conduit

import os, string, stat, sys, re

from Sulfur import Options
from Sulfur.Options import O_MULTIPLE, Boolean, String
from Pyrite import protect_name, _

backup_dir = None

# exclusions:
#  - exclude-regexps  - regular expressions to match names
#  - exclude          - names to exclude
#  - exclude-creators - exclude these creator ids
#  - exclude-types    - exclude these types
#

def match(rlist, s):
    for r in rlist:
	m = re.match(r, s)
	if m: return m
    return None

class Conduit(Pyrite.Conduit.Conduit):
    name = 'Backup'
    version = Pyrite.version
    author = Pyrite.author
    url = ''
    description = _("Backs up the PalmOS device.")
    options = [
	Boolean('ignore_backup_bit', 1, _("Ignore backup bit?")),
	Boolean('incremental', 1, _("Incremental backup?")),
	Boolean('delete_old',1, _("Delete old databases?")), 
	Boolean('archive',1, _("Archive deleted databases?")),
	Boolean('debug', 0, _("Debugging output?")),

	Boolean('skip-old-applications', 1,
	       _("Skip applications that have already been backed up")),
	String('exclude', [], _("Names of databases to exclude"), None, (O_MULTIPLE,)),
	String('exclude-regexps', [], _("Regexps of database names to exclude"), None,
	       (O_MULTIPLE,)),
#	String('exclude-creators', [], _("Creators of databases to exclude"), None,
#	       (O_MULTIPLE,)),
#	String('exclude-types', [], _("Types of databases to exclude"), None,
#	       (O_MULTIPLE,)),
	]
    
    def __init__(self):
	Pyrite.Conduit.Conduit.__init__(self)

	self.__backup_list = []
	
    def presync(self, app):
	store = app.user_directory('backup')
	
	self.__backup_list = store.list()
	return 1

    def __should_backup(self, i, store):
	# test whether a database should be backed up, based on its info
	# structure.
	if not self.ignore_backup_bit and not i['flagBackup']:
	    if self.debug: self.log(_("excluding '%s' because backup bit not set") % name)
	    return 0
	name = i['name']
	# exclusions
	if self.__ex_names and name in self.__ex_names:
	    if self.debug: self.log(_("excluding '%s' based on name match") % name)
	    return 0
	if self.__ex_regexps and match(self.__ex_regexps, name):
	    if self.debug: self.log(_("excluding '%s' based on regexp match") % name)
	    return 0

	# incremental backup
	if not self.incremental or name not in self.__backup_list:
	    return 1
	
	try:
	    fi = store.info(name)
	except:
	    self.log(_("wtf? %s") % name)
	    return 0
	
	if self.debug:
	    self.log('%s %s/%s : %s/%s %s' % (name, fi['modifyDate'],
					      fi['modnum'],
					      i['modifyDate'],
					      i['modnum'],
					      i['flagBackup']))

	# the "backupbuddy rule"
	if (fi['modnum'] == i['modnum'] or
	    fi['modifyDate'] == i['modifyDate']) and \
	    fi['createDate'] == i['createDate']:
	    if self.debug: self.log(_("skipping '%s' based on header dates") % name)
	    return 0

	# the "don't back up big apps even when Palm OS is acting stupid" rule
	if self.__skip_apps and i['flagResource'] and i['type'] == 'APPL' and \
	   name in self.__backup_list:
	    if self.debug: self.log(_("skipping already-backed-up application '%s'") % name)
	    return 0

	return 1
    
    def __call__(self, app):
	store = app.user_directory('backup')

	# ??? create directory if doesn't exist?

	#backup_list = store.list()
	palm_list = app.remote.listinfo()

	self.__ex_names = self.get_option('exclude')
	self.__ex_regexps = self.get_option('exclude-regexps')
	self.__skip_apps = self.get_option('skip-old-applications')
	
	for i in palm_list:
	    name = i['name']
	    if self.__should_backup(i, store):
		self.log(_("backing up '%s'") % name)
		try:
		    app.remote.copy(store, name)
		except:
		    self.log('%s %s' % (sys.exc_info()[0],
					sys.exc_info()[1]))
		    self.log(_("failed to back up '%s'") % name)
		    try: store.delete(name)
		    except: pass

	    if name in self.__backup_list: self.__backup_list.remove(name)

	if self.delete_old:
	    self.__delete_list = self.__backup_list
#	    for fn in backup_list:
#		self.log('removing "%s" from backup directory' % fn)
#		try: store.delete(fn)
#		except: pass

	self.log(_("done"))


    def postsync(self, app):
	# hmm, this seems dangerous, can we guarantee the user name doesn't
	# change?  I say yes, it can change between presync and sync, but not
	# between sync and postsync, we have to make that rule or conduits will
	# be living in a world of hurt.
	store = app.user_directory('backup')
	if self.archive: astore = app.user_directory('backup-archive')
	if self.delete_old:
	    for fn in self.__delete_list:
		if self.archive:
		    self.log(_("archiving '%s'") % fn)
		    try: store.copy(astore, fn)
		    except: self.log(_("error copying '%s' to archive") % fn)
		self.log(_("removing '%s' from backup directory") % fn)
		try: store.delete(fn)
		except: self.log(_("error removing '%s' from backup") % fn)