107 |
107 |
108 # server specific migration methods ######################################## |
108 # server specific migration methods ######################################## |
109 |
109 |
110 def backup_database(self, backupfile=None, askconfirm=True): |
110 def backup_database(self, backupfile=None, askconfirm=True): |
111 config = self.config |
111 config = self.config |
112 source = config.sources()['system'] |
112 repo = self.repo_connect() |
113 helper = get_adv_func_helper(source['db-driver']) |
113 timestamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S') |
114 date = datetime.now().strftime('%Y-%m-%d_%H:%M:%S') |
114 for source in repo.sources: |
115 app = config.appid |
115 source.backup(self.confirm, backupfile, timestamp, |
116 backupfile = backupfile or join(config.appdatahome, 'backup', |
116 askconfirm=askconfirm) |
117 '%s-%s.dump' % (app, date)) |
117 repo.hm.call_hooks('server_backup', repo=repo, timestamp=timestamp) |
118 if exists(backupfile): |
118 |
119 if not self.confirm('a backup already exists for %s, overwrite it?' % app): |
119 def restore_database(self, backupfile, drop=True, systemonly=True, |
120 return |
120 askconfirm=True): |
121 elif askconfirm and not self.confirm('backup %s database?' % app): |
|
122 return |
|
123 cmd = helper.backup_command(source['db-name'], source.get('db-host'), |
|
124 source.get('db-user'), backupfile, |
|
125 keepownership=False) |
|
126 while True: |
|
127 print cmd |
|
128 if os.system(cmd): |
|
129 print 'error while backuping the base' |
|
130 answer = self.confirm('continue anyway?', |
|
131 shell=False, abort=False, retry=True) |
|
132 if not answer: |
|
133 raise SystemExit(1) |
|
134 if answer == 1: # 1: continue, 2: retry |
|
135 break |
|
136 else: |
|
137 from cubicweb.toolsutils import restrict_perms_to_user |
|
138 print 'database backup:', backupfile |
|
139 restrict_perms_to_user(backupfile, self.info) |
|
140 break |
|
141 |
|
142 def restore_database(self, backupfile, drop=True): |
|
143 config = self.config |
121 config = self.config |
144 source = config.sources()['system'] |
122 repo = self.repo_connect() |
145 helper = get_adv_func_helper(source['db-driver']) |
123 if systemonly: |
146 app = config.appid |
124 repo.system_source.restore(self.confirm, backupfile=backupfile, |
147 if not exists(backupfile): |
125 drop=drop, askconfirm=askconfirm) |
148 raise Exception("backup file %s doesn't exist" % backupfile) |
126 else: |
149 if self.confirm('restore %s database from %s ?' % (app, backupfile)): |
127 # in that case, backup file is expected to be a time stamp |
150 for cmd in helper.restore_commands(source['db-name'], source.get('db-host'), |
128 for source in repo.sources: |
151 source.get('db-user'), backupfile, |
129 source.backup(self.confirm, timestamp=backupfile, drop=drop, |
152 source['db-encoding'], |
130 askconfirm=askconfirm) |
153 keepownership=False, drop=drop): |
131 repo.hm.call_hooks('server_restore', repo=repo, timestamp=backupfile) |
154 while True: |
|
155 print cmd |
|
156 if os.system(cmd): |
|
157 print 'error while restoring the base' |
|
158 answer = self.confirm('continue anyway?', |
|
159 shell=False, abort=False, retry=True) |
|
160 if not answer: |
|
161 raise SystemExit(1) |
|
162 if answer == 1: # 1: continue, 2: retry |
|
163 break |
|
164 else: |
|
165 break |
|
166 print 'database restored' |
|
167 |
132 |
168 @property |
133 @property |
169 def cnx(self): |
134 def cnx(self): |
170 """lazy connection""" |
135 """lazy connection""" |
171 try: |
136 try: |