1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# This module copyright (C) 2011-2012 Therp BV (<http://therp.nl>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
22
from openerp.osv.orm import TransientModel,except_orm
23
from openerp.osv import fields
26
from openerp.osv.orm import except_orm
27
from openerp.tools.translate import _
28
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
32
class trp_backup_wizard(TransientModel):
33
_name='trp_backup.wizard'
36
'messages': fields.text('Messages', readonly=True),
40
'messages': 'Press "Do backup" to start the backup process',
43
_logger=logging.getLogger('trp_backup')
45
def do_backup_wizard(self, cr, uid, ids, context=None):
46
_, messages, _=self.do_backup(cr, uid)
47
self.write(cr, uid, ids, {'messages': messages})
49
def do_backup_cron(self, cr, uid, context=None):
50
self.do_backup(cr, uid)
52
def do_backup(self, cr, uid):
53
params=self.pool.get('ir.config_parameter')
54
publc_key_file=params.get_param(cr, uid, 'trp_backup.publickeyfile')
55
backup_dir=params.get_param(cr, uid, 'trp_backup.backupdirectory')
59
if not os.path.isfile(str(publc_key_file)):
60
raise except_orm(_('Error'),_('You need to give a *public* key to encrypt your backup - %s is not suitable')%(publc_key_file))
61
if not os.path.isdir(str(backup_dir)):
62
raise except_orm(_('Error'),_('You need to give a directory to backup to'))
64
outfile=os.path.join(backup_dir, cr.dbname+'_'+
65
datetime.datetime.now().strftime(
66
DEFAULT_SERVER_DATETIME_FORMAT).replace(' ', '_'))
68
self._logger.info(_('starting backup to %s')%outfile)
70
backup_process=subprocess.Popen(['pg_dump',
74
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75
crypt_process=subprocess.Popen(['openssl',
76
'smime', '-encrypt', '-aes256', '-binary', '-outform', 'DEM',
79
stdin=backup_process.stdout,stdout=subprocess.PIPE,
80
stderr=subprocess.PIPE)
81
output=crypt_process.communicate()
83
if crypt_process.returncode==0:
84
messages+=_('Sucessfully backed up to %s')%outfile
87
messages+=_('There was an error during backup:')+'\n'
88
messages+=(output[0]+'\n') if output[0] else ''
89
messages+=(output[1]+'\n') if output[1] else ''
91
self._logger.info(messages)
93
return success, messages, outfile