~therp-nl/therp-addons/6.1-web_mode_visibility_separator_use_override

« back to all changes in this revision

Viewing changes to trp_backup/trp_backup.py

  • Committer: Holger Brunn
  • Date: 2012-10-12 07:41:44 UTC
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: hbrunn@therp.nl-20121012074144-zjtl80aeu5hseyqm
[IMP] renamed modules to better reflect their purpose
[IMP] only restart rsync transfer if the error code indicates a connection
issue
[FIX] fixed copyright notices

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    This module copyright (C) 2011-2012 Therp BV (<http://therp.nl>) Tiny SPRL (<http://tiny.be>))
6
 
#    All Rights Reserved
7
 
#
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.
12
 
#
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.
17
 
#
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/>.
20
 
#
21
 
##############################################################################
22
 
from openerp.osv.orm import TransientModel,except_orm
23
 
from openerp.osv import fields
24
 
import subprocess
25
 
import os.path
26
 
from openerp.osv.orm import except_orm
27
 
from openerp.tools.translate import _
28
 
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
29
 
import logging
30
 
import datetime
31
 
 
32
 
class trp_backup_wizard(TransientModel):
33
 
    _name='trp_backup.wizard'
34
 
 
35
 
    _columns={
36
 
            'messages': fields.text('Messages', readonly=True),
37
 
            }
38
 
 
39
 
    _defaults={
40
 
            'messages': 'Press "Do backup" to start the backup process',
41
 
            }
42
 
    
43
 
    _logger=logging.getLogger('trp_backup')
44
 
 
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})
48
 
 
49
 
    def do_backup_cron(self, cr, uid, context=None):
50
 
        self.do_backup(cr, uid)
51
 
 
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')
56
 
        messages=''
57
 
        success=False
58
 
        
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'))
63
 
 
64
 
        outfile=os.path.join(backup_dir, cr.dbname+'_'+
65
 
                datetime.datetime.now().strftime(
66
 
                    DEFAULT_SERVER_DATETIME_FORMAT).replace(' ', '_'))
67
 
 
68
 
        self._logger.info(_('starting backup to %s')%outfile)
69
 
 
70
 
        backup_process=subprocess.Popen(['pg_dump', 
71
 
            '--format', 'custom',
72
 
            '--no-owner',
73
 
            cr.dbname], 
74
 
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75
 
        crypt_process=subprocess.Popen(['openssl', 
76
 
            'smime', '-encrypt', '-aes256', '-binary', '-outform', 'DEM',
77
 
                '-out', outfile,
78
 
                publc_key_file],
79
 
                stdin=backup_process.stdout,stdout=subprocess.PIPE, 
80
 
                stderr=subprocess.PIPE)
81
 
        output=crypt_process.communicate()
82
 
 
83
 
        if crypt_process.returncode==0:
84
 
            messages+=_('Sucessfully backed up to %s')%outfile
85
 
            success=True
86
 
        else:
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 ''
90
 
 
91
 
        self._logger.info(messages)
92
 
 
93
 
        return success, messages, outfile