~therp-nl/therp-addons/7.0_lp1215631

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
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2011-2012 Therp BV (<http://therp.nl>)
#    All Rights Reserved
#
#    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 3 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, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv.orm import TransientModel,except_orm
import subprocess
from openerp.tools.translate import _

class trp_backup_wizard(TransientModel):
    _inherit='trp_backup.wizard'

    def do_backup(self, cr, uid):
        success, messages, backup_file=super(trp_backup_wizard, self).do_backup(cr, 
                uid)

        if success:
            messages+='\n'
            params=self.pool.get('ir.config_parameter')
            rsync_host=self.pool.get('ir.config_parameter').get_param(cr, uid, 
                    'trp_backup_rsync.host')
            
            self._logger.info(_('copying %s to %s')%(backup_file,rsync_host))
            
            run_transfer=True
            while run_transfer:
                rsync_process=subprocess.Popen(['rsync', 
                    '--partial',
                    backup_file,
                    rsync_host], 
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                
                output=rsync_process.communicate()
                
                if rsync_process.returncode==0:
                    messages+=_('Sucessfully copied %s to %s')%(backup_file,
                            rsync_host)
                    messages+=(output[0]+'\n') if output[0] else ''
                    messages+=(output[1]+'\n') if output[1] else ''
                    run_transfer=False
                else:
                    messages+=_('There was an error during transfer:')+'\n'
                    messages+=(output[0]+'\n') if output[0] else ''
                    messages+=(output[1]+'\n') if output[1] else ''
                    if rsync_process.returncode not in [23,30,35]:
                        run_transfer=False
                    
                self._logger.info(messages)
        else:
            self._logger(_('Backup failed, not running transfer'))

        return success, messages, backup_file