def pgsql_backup(self, user, passwd, db, file_name, host='localhost', port=5432): """ Generates a backup for PostgreSQL. :param user: Database username. :param passwd: :param db: Database name. :param file_name: Output file. :param host: Database hostname. :param port: Database port, defaults to 5432. :return: """ cfg = self.config cmd = [ cfg.pg_dump_exe, '-h', host, '-p', str(port), '-U', user, '-Fp', '-b', '-O', '-E', 'UTF8', '-x', '--file={}'.format(file_name), db ] os.environ[ENV_PGPASSWORD] = passwd p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() del(os.environ[ENV_PGPASSWORD]) if stderr and len(stderr) > 0: raise SQLBackupError(stderr)
def mysql_backup(self, user, passwd, db, file_name, host='localhost', port=3306): """ Generates a backup for MySQL. :param user: Database server username. :param passwd: :param db: Database name. :param file_name: Output file. :param host: Database host. :param port: Database port, defaults to 3306. :return: """ cfg = self.config cmd = [ cfg.mysql_dump_exe, '--opt', '--single-transaction', '-u', user, '-h', host, '--port={}'.format(port), '--result-file', file_name, db ] os.environ[ENV_MYSQL_PWD] = passwd p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() del (os.environ[ENV_MYSQL_PWD]) if stderr and len(stderr) > 0: raise SQLBackupError(stderr)
def verify_gz(self, file_name): cfg = self.config cmd = [cfg.gzip_exe, '-t', file_name] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) logger.info('Verifying {}'.format(file_name)) (stdout, stderr) = p.communicate() if stderr and len(stderr) > 0: raise SQLBackupError('Integrity error in {}: {}'.format(file_name, stderr))
def compress_gz(self, file_name): """ See compress_bz2. :param file_name: :return: """ cfg = self.config cmd = [cfg.gzip_exe, '-9', file_name] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) logger.info('Compressing {}'.format(file_name)) (stdout, stderr) = p.communicate() if stderr and len(stderr) > 0: raise SQLBackupError('Error compressing {}: {}'.format(file_name, stderr))
def compress_bz2(self, file_name): """ Compresses the resultant file. We avoid using the python bzip2 modules because we need to compress the data inplace without reading or writing to memory. """ cfg = self.config cmd = [cfg.bzip2_exe, '-9', file_name] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) logger.info('Compressing {}'.format(file_name)) (stdout, stderr) = p.communicate() if stderr and len(stderr) > 0: raise SQLBackupError('Error compressing {}: {}'.format(file_name, stderr))
def compress_7z(self, file_name): cfg = self.config p7z_file = '{}.7z'.format(file_name) p7z_opts = self.config.p7zip_options cmd = [cfg.p7zip_exe, 'a', '-bd', '-t7z', '-m0=lzma', '-mx=9', '-mfb=64', '-md=64m', '-ms=on'] if p7z_opts.delete_after_compress and p7z_opts.supports_sdel: cmd.append('-sdel') cmd.append(p7z_file) cmd.append(file_name) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) logger.info('Compressing {}'.format(file_name)) (stdout, stderr) = p.communicate() if stderr and len(stderr) > 0: raise SQLBackupError('Error compressing {}: {}'.format(file_name, stderr)) if p7z_opts.delete_after_compress and not p7z_opts.supports_sdel: os.remove(file_name)