예제 #1
0
    def exp_dump(self, db_name):
        logger = netsvc.Logger()

        self._set_pg_psw_env_var()

        cmd = ['pg_dump', '--format=c', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append(db_name)

        stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
        stdin.close()
        data = stdout.read()
        res = stdout.close()
        if res:
            logger.notifyChannel("web-services", netsvc.LOG_ERROR,
                    'DUMP DB: %s failed\n%s' % (db_name, data))
            raise Exception, "Couldn't dump database"
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
                'DUMP DB: %s' % (db_name))

        self._unset_pg_psw_env_var()

        return base64.encodestring(data)
예제 #2
0
    def exp_dump(self, db_name):
        logger = netsvc.Logger()

        self._set_pg_psw_env_var()

        cmd = ['pg_dump', '--format=c', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append(db_name)

        stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
        stdin.close()
        data = stdout.read()
        res = stdout.close()
        if res:
            logger.notifyChannel("web-services", netsvc.LOG_ERROR,
                    'DUMP DB: %s failed\n%s' % (db_name, data))
            raise Exception, "Couldn't dump database"
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
                'DUMP DB: %s' % (db_name))

        self._unset_pg_psw_env_var()

        return base64.encodestring(data)
예제 #3
0
    def exp_restore(self, db_name, data):
        logger = netsvc.Logger()

        self._set_pg_psw_env_var()

        if self.exp_db_exist(db_name):
            logger.notifyChannel("web-services", netsvc.LOG_WARNING,
                    'RESTORE DB: %s already exists' % (db_name,))
            raise Exception, "Database already exists"

        self._create_empty_database(db_name)

        cmd = ['pg_restore', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append('--dbname=' + db_name)
        args2 = tuple(cmd)

        buf=base64.decodestring(data)
        if os.name == "nt":
            tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam()
            file(tmpfile, 'wb').write(buf)
            args2=list(args2)
            args2.append(' ' + tmpfile)
            args2=tuple(args2)
        stdin, stdout = tools.exec_pg_command_pipe(*args2)
        if not os.name == "nt":
            stdin.write(base64.decodestring(data))
        stdin.close()
        res = stdout.close()
        if res:
            raise Exception, "Couldn't restore database"
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
                'RESTORE DB: %s' % (db_name))

        self._unset_pg_psw_env_var()

        return True
예제 #4
0
    def exp_restore(self, db_name, data):
        logger = netsvc.Logger()

        self._set_pg_psw_env_var()

        if self.exp_db_exist(db_name):
            logger.notifyChannel("web-services", netsvc.LOG_WARNING,
                    'RESTORE DB: %s already exists' % (db_name,))
            raise Exception, "Database already exists"

        self._create_empty_database(db_name)

        cmd = ['pg_restore', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append('--dbname=' + db_name)
        args2 = tuple(cmd)

        buf=base64.decodestring(data)
        if os.name == "nt":
            tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam()
            file(tmpfile, 'wb').write(buf)
            args2=list(args2)
            args2.append(' ' + tmpfile)
            args2=tuple(args2)
        stdin, stdout = tools.exec_pg_command_pipe(*args2)
        if not os.name == "nt":
            stdin.write(base64.decodestring(data))
        stdin.close()
        res = stdout.close()
        if res:
            raise Exception, "Couldn't restore database"
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
                'RESTORE DB: %s' % (db_name))

        self._unset_pg_psw_env_var()

        return True
예제 #5
0
    def ept_backup(self, cr, uid, ids, db_name, bkup_dir, automatic, ftp_enable, FTP_id, bak_conf, keep_backup_local, context=None):
        context = context or self.pool['res.users'].context_get(cr, uid)
        self._set_pg_psw_env_var()
        bkp_file = '%s_%s.sql' % (db_name, time.strftime('%Y%m%d_%H_%M_%S'))
        tar_file_name = '%s_%s.tar.gz' % (db_name, time.strftime('%Y%m%d_%H_%M_%S'))
        file_path = os.path.join(bkup_dir, bkp_file)
        tar_file_path = os.path.join(bkup_dir, tar_file_name)
        tar_obj = tarfile.open(tar_file_path, 'w:gz')
        fp = codecs.open(file_path, 'wb')
        cmd = ['pg_dump', '--format=p', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append(db_name)
        stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
        stdin.close()
        data = stdout.read()
        fp.write(data)
        fp.close()
        tar_obj.add(file_path, bkp_file)
        tar_obj.close()
        res = stdout.close()
        user_id = None
        user_name = ''
        backup_status = ''
        if not automatic:
            user_id = uid
            user_name = self.pool['res.users'].browse(cr, uid, uid, context=context).name
        if res:
            _logger.error(u"DUMP DB: %s failed\n%s" % (db_name, data))
            for obj in self.browse(cr, uid, ids, context):
                self.pool['db.backup.line'].create(cr, uid, {
                    'backup_id': obj.id,
                    'name': obj.name,
                    'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                    'done_by': user_id,
                    'message': 'Could not create back up of database. Backup Failed.',
                    'automatic': automatic,
                })
                backup_status = 'Could not create back up of database. Backup Failed.'
            self._unset_pg_psw_env_var()
            return False
        else:
            _logger.info(u"DUMP DB: %s" % (db_name))
            if ftp_enable:
                if FTP_id:
                    ftpbackup_obj = self.pool['ept.ftpbackup']
                    ept_ftp = ftpbackup_obj.browse(cr, uid, FTP_id.id, context)
                    if not ept_ftp.ept_ftp_host or not ept_ftp.ept_ftp_username or \
                            not ept_ftp.ept_ftp_password or not ept_ftp.to_ept_location:
                        for obj in self.browse(cr, uid, ids):
                            self.pool['db.backup.line'].create(cr, uid, {
                                'backup_id': obj.id,
                                'name': tar_file_name,
                                'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                'done_by': uid,
                                'message': 'Could not create back up of database. Backup Failed. Invalid FTP Credentials',
                                'automatic': automatic,
                            }, context)
                        backup_status = 'Could not create back up of database. Backup Failed. Invalid FTP Credentials'
                        os.remove(file_path)
                        return True
                    try:
                        if ept_ftp.is_ftp_active:
                            ftp = ftp_login(ept_ftp.ept_ftp_host, ept_ftp.ept_ftp_username, ept_ftp.ept_ftp_password)
                            # s = ftplib.FTP(ept_ftp.ept_ftp_host, ept_ftp.ept_ftp_username, ept_ftp.ept_ftp_password)  # Connect
                            f = open(tar_file_path, 'rb')                # file to send

                            remote_file_path = os.path.join(ept_ftp.to_ept_location, tar_file_name)
                            ftp.storbinary('STOR ' + remote_file_path, f)         # Send the file
                            f.close()                                # Close file and FTP
                            ftp.quit()
                            for obj in self.browse(cr, uid, ids, context):
                                backup_status = 'Backup completed successfully at Remote FTP path : %s/%s.' % (ept_ftp.to_ept_location, tar_file_name)
                                self.pool['db.backup.line'].create(cr, uid, {
                                    'backup_id': obj.id,
                                    'name': obj.name,
                                    'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                    'message': backup_status,
                                    'automatic': automatic,
                                    'done_by': user_id,
                                    'path': tar_file_path,
                                    'file_size': str(os.path.getsize(tar_file_path)),
                                }, context)
                    except Exception, e:
                        for obj in self.browse(cr, uid, ids, context):
                            backup_status = 'Could not create back up of database at Remote FTP path: %s/%s. Backup Failed. Exception: %s' % (e, ept_ftp.to_ept_location, tar_file_name)
                            self.pool['db.backup.line'].create(cr, uid, {
                                'backup_id': obj.id,
                                'name': tar_file_name,
                                'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                'done_by': uid,
                                'message': backup_status,
                                'automatic': automatic,
                            }, context)
                                      
                if not keep_backup_local:
                    os.remove(tar_file_path)
            else:
예제 #6
0
    def ept_backup(self, cr, uid, ids, db_name, bkup_dir, automatic,
                   ftp_enable, FTP_id, bak_conf, keep_backup_local):
        logger = netsvc.Logger()
        self._set_pg_psw_env_var()
        bkp_file = '%s_%s.sql' % (db_name, time.strftime('%Y%m%d_%H_%M_%S'))
        tar_file_name = '%s_%s.tar.gz' % (db_name,
                                          time.strftime('%Y%m%d_%H_%M_%S'))
        file_path = os.path.join(bkup_dir, bkp_file)
        tar_file_path = os.path.join(bkup_dir, tar_file_name)
        tar_obj = tarfile.open(tar_file_path, 'w:gz')
        fp = codecs.open(file_path, 'wb')
        cmd = ['pg_dump', '--format=p', '--no-owner']
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        cmd.append(db_name)
        stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
        stdin.close()
        data = stdout.read()
        fp.write(data)
        fp.close()
        tar_obj.add(file_path, bkp_file)
        tar_obj.close()
        res = stdout.close()
        user_id = None
        user_name = ''
        backup_status = ''
        if not automatic:
            user_id = uid
            user_name = self.pool.get('res.users').browse(cr,
                                                          uid,
                                                          uid,
                                                          context=None).name
        if res:
            logger.notifyChannel("web-services", netsvc.LOG_ERROR,
                                 'DUMP DB: %s failed\n%s' % (db_name, data))
            for obj in self.browse(cr, uid, ids):
                self.pool.get('db.backup.line').create(
                    cr, uid, {
                        'backup_id': obj.id,
                        'name': obj.name,
                        'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                        'done_by': user_id,
                        'message':
                        'Could not create back up of database. Backup Failed.',
                        'automatic': automatic,
                    })
                backup_status = 'Could not create back up of database. Backup Failed.'
            self._unset_pg_psw_env_var()
            return False
        else:
            logger.notifyChannel("web-services", netsvc.LOG_INFO,
                                 'DUMP DB: %s' % (db_name))
            context = {}
            if ftp_enable:
                if FTP_id:
                    ftpbackup_obj = self.pool.get('ept.ftpbackup')
                    ept_ftp = ftpbackup_obj.browse(cr, uid, FTP_id.id, context)
                    if not ept_ftp.ept_ftp_host or not ept_ftp.ept_ftp_username or \
                        not ept_ftp.ept_ftp_password or not ept_ftp.to_ept_location:
                        for obj in self.browse(cr, uid, ids):
                            self.pool.get('db.backup.line').create(
                                cr, uid, {
                                    'backup_id': obj.id,
                                    'name': tar_file_name,
                                    'date_time':
                                    time.strftime('%Y-%m-%d %H:%M:%S'),
                                    'done_by': uid,
                                    'message':
                                    'Could not create back up of database. Backup Failed. Invalid FTP Credentials',
                                    'automatic': automatic,
                                })
                        backup_status = 'Could not create back up of database. Backup Failed. Invalid FTP Credentials'
                        os.remove(file_path)
                        return True
                    try:
                        if ept_ftp.is_ftp_active:
                            s = ftplib.FTP(ept_ftp.ept_ftp_host,
                                           ept_ftp.ept_ftp_username,
                                           ept_ftp.ept_ftp_password)  # Connect
                            f = open(tar_file_path, 'rb')  # file to send
                            remote_file_path = os.path.join(
                                ept_ftp.to_ept_location, tar_file_name)
                            s.storbinary('STOR ' + remote_file_path,
                                         f)  # Send the file
                            f.close()  # Close file and FTP
                            s.quit()
                            for obj in self.browse(cr, uid, ids):
                                backup_status = 'Backup completed successfully at Remote FTP path : %s/%s.' % (
                                    ept_ftp.to_ept_location, tar_file_name)
                                self.pool.get('db.backup.line').create(
                                    cr, uid, {
                                        'backup_id':
                                        obj.id,
                                        'name':
                                        obj.name,
                                        'date_time':
                                        time.strftime('%Y-%m-%d %H:%M:%S'),
                                        'message':
                                        backup_status,
                                        'automatic':
                                        automatic,
                                        'done_by':
                                        user_id,
                                        'path':
                                        tar_file_path,
                                        'file_size':
                                        str(os.path.getsize(tar_file_path)),
                                    })
                    except Exception, e:
                        for obj in self.browse(cr, uid, ids):
                            backup_status = 'Could not create back up of database at Remote FTP path : %s/%s. Backup Failed. Exception : %s' % (
                                e, ept_ftp.to_ept_location, tar_file_name)
                            self.pool.get('db.backup.line').create(
                                cr, uid, {
                                    'backup_id': obj.id,
                                    'name': tar_file_name,
                                    'date_time':
                                    time.strftime('%Y-%m-%d %H:%M:%S'),
                                    'done_by': uid,
                                    'message': backup_status,
                                    'automatic': automatic,
                                })

                if not keep_backup_local:
                    os.remove(tar_file_path)
            else:
예제 #7
0
    def ept_backup(self, cr, uid, ids, db_name, bkup_dir, automatic, ftp_enable, FTP_id, bak_conf, keep_backup_local, context=None):
        context = context or self.pool['res.users'].context_get(cr, uid)
        self._set_pg_psw_env_var()

        bkp_file_name = '%s_%s.sql' % (db_name, time.strftime('%Y%m%d_%H_%M_%S'))
        tar_file_name = '%s_%s.tar.gz' % (db_name, time.strftime('%Y%m%d_%H_%M_%S'))

        bck_file_path = os.path.join(bkup_dir, bkp_file_name)

        # - - - - - - - - - - - - - - - - - - - -
        # Build the pg_dump command line
        # - - - - - - - - - - - - - - - - - - - -
        cmd = ['pg_dump', '--format=p', '--no-owner']

        # Get DB connection parameters from OpenERP configuration
        if tools.config['db_user']:
            cmd.append('--username='******'db_user'])
        # end if
        if tools.config['db_host']:
            cmd.append('--host=' + tools.config['db_host'])
        # end if
        if tools.config['db_port']:
            cmd.append('--port=' + str(tools.config['db_port']))
        # end if

        # Add the output file path
        cmd.append("--file=" + bck_file_path)

        # Add the DB name
        cmd.append(db_name)

        # Run the backup
        stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
        stdin.close()
        data = stdout.read()  # NB: the this is not the data from the DB dump, just the pg_dump output text
        res = stdout.close()

        tar_file_path = os.path.join(bkup_dir, tar_file_name)
        tar_obj = tarfile.open(tar_file_path, 'w:gz')
        tar_obj.add(bck_file_path, bkp_file_name)
        tar_obj.close()

        user_id = None
        user_name = ''
        backup_status = ''
        if not automatic:
            user_id = uid
            user_name = self.pool['res.users'].browse(cr, uid, uid, context=context).name
        if res:
            _logger.error(u"DUMP DB: %s failed\n%s" % (db_name, data))
            for obj in self.browse(cr, uid, ids, context):
                self.pool['db.backup.line'].create(cr, uid, {
                    'backup_id': obj.id,
                    'name': obj.name,
                    'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                    'done_by': user_id,
                    'message': 'Could not create back up of database. Backup Failed.',
                    'automatic': automatic,
                })
                backup_status = 'Could not create back up of database. Backup Failed.'
            self._unset_pg_psw_env_var()
            return False
        else:
            _logger.info(u"DUMP DB: %s" % (db_name))
            if ftp_enable:
                if FTP_id:
                    ftpbackup_obj = self.pool['ept.ftpbackup']
                    ept_ftp = ftpbackup_obj.browse(cr, uid, FTP_id.id, context)
                    if not ept_ftp.ept_ftp_host or not ept_ftp.ept_ftp_username or \
                            not ept_ftp.ept_ftp_password or not ept_ftp.to_ept_location:
                        for obj in self.browse(cr, uid, ids):
                            self.pool['db.backup.line'].create(cr, uid, {
                                'backup_id': obj.id,
                                'name': tar_file_name,
                                'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                'done_by': uid,
                                'message': 'Could not create back up of database. Backup Failed. Invalid FTP Credentials',
                                'automatic': automatic,
                            }, context)
                        backup_status = 'Could not create back up of database. Backup Failed. Invalid FTP Credentials'
                        os.remove(bck_file_path)
                        return True
                    try:
                        if ept_ftp.is_ftp_active:
                            ftp = ftp_login(ept_ftp.ept_ftp_host, ept_ftp.ept_ftp_username, ept_ftp.ept_ftp_password)
                            # s = ftplib.FTP(ept_ftp.ept_ftp_host, ept_ftp.ept_ftp_username, ept_ftp.ept_ftp_password)  # Connect
                            f = open(tar_file_path, 'rb')                # file to send

                            remote_file_path = os.path.join(ept_ftp.to_ept_location, tar_file_name)
                            ftp.storbinary('STOR ' + remote_file_path, f)         # Send the file
                            f.close()                                # Close file and FTP
                            ftp.quit()
                            for obj in self.browse(cr, uid, ids, context):
                                backup_status = 'Backup completed successfully at Remote FTP path : %s/%s.' % (ept_ftp.to_ept_location, tar_file_name)
                                self.pool['db.backup.line'].create(cr, uid, {
                                    'backup_id': obj.id,
                                    'name': obj.name,
                                    'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                    'message': backup_status,
                                    'automatic': automatic,
                                    'done_by': user_id,
                                    'path': tar_file_path,
                                    'file_size': str(os.path.getsize(tar_file_path)),
                                }, context)
                    except Exception, e:
                        for obj in self.browse(cr, uid, ids, context):
                            backup_status = 'Could not create back up of database at Remote FTP path: %s/%s. Backup Failed. Exception: %s' % (e, ept_ftp.to_ept_location, tar_file_name)
                            self.pool['db.backup.line'].create(cr, uid, {
                                'backup_id': obj.id,
                                'name': tar_file_name,
                                'date_time': time.strftime('%Y-%m-%d %H:%M:%S'),
                                'done_by': uid,
                                'message': backup_status,
                                'automatic': automatic,
                            }, context)
                                      
                if not keep_backup_local:
                    os.remove(tar_file_path)
            else: