Skip to content

Commit

Permalink
postgres-plugin: stop with error, if accurate is enabled
Browse files Browse the repository at this point in the history
- typos and pep8 / black formatted
  • Loading branch information
aussendorf authored and franku committed May 4, 2020
1 parent b25dc5a commit 310b1be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
69 changes: 35 additions & 34 deletions core/src/plugins/filed/BareosFdPluginPostgres.py
Expand Up @@ -21,8 +21,8 @@
#
# Author: Maik Aussendorf
#
# Bareos python plugins class that adds files from a local list to
# the backup fileset
# Bareos python plugins class that performs online backups (full and incremental)
# from Postgres databases. Supports point-in-time (PIT) restores.

from bareosfd import *
from bareos_fd_consts import bJobMessageType, bFileType, bRCs
Expand Down Expand Up @@ -96,6 +96,15 @@ def check_options(self, context, mandatory_options=None):
result = super(BareosFdPluginPostgres, self).check_options(
context, mandatory_options
)
# Accurate may cause problems with plugins
accurate_enabled = GetValue(context, bVariable["bVarAccurate"])
if accurate_enabled is not None and accurate_enabled != 0:
JobMessage(
context,
bJobMessageType["M_FATAL"],
"start_backup_job: Accurate backup not allowed please disable in Job\n",
)
return bRCs["bRC_Error"]
if not result == bRCs["bRC_OK"]:
return result
if not self.options["postgresDataDir"].endswith("/"):
Expand All @@ -117,8 +126,8 @@ def check_options(self, context, mandatory_options=None):
self.switchWal = True
else:
self.switchWal = self.options["switchWal"].lower() == "true"
if 'dbHost' in self.options:
self.dbOpts += " host='%s'" %self.options["dbHost"]
if "dbHost" in self.options:
self.dbOpts += " host='%s'" % self.options["dbHost"]
return bRCs["bRC_OK"]

def execute_SQL(self, context, sqlStatement):
Expand All @@ -131,7 +140,7 @@ def execute_SQL(self, context, sqlStatement):
bareosfd.JobMessage(
context,
bJobMessageType["M_ERROR"],
"Query \"%s\" failed: \"%s\"" % (sqlStatement, e.message),
'Query "%s" failed: "%s"' % (sqlStatement, e.message),
)
return False
return True
Expand All @@ -141,25 +150,22 @@ def start_backup_job(self, context):
Make filelist in super class and tell Postgres
that we start a backup now
"""
bareosfd.DebugMessage(
context, 100, "start_backup_job in PostgresPlugin called",
)
bareosfd.DebugMessage(context, 100, "start_backup_job in PostgresPlugin called")
try:
self.dbCon = psycopg2.connect(
"dbname=%s user=%s %s" % (self.dbname, self.dbuser, self.dbOpts)
)
self.dbCursor = self.dbCon.cursor()
self.dbCursor.execute("SELECT current_setting('server_version_num')")
self.pgVersion = int(self.dbCursor.fetchone()[0])
#bareosfd.DebugMessage(
# bareosfd.DebugMessage(
# context, 1, "Connected to Postgres version %d\n" % self.pgVersion,
#)
# )
## WARNING: JobMessages cause fatal errors at this stage
JobMessage(
context,
bJobMessageType["M_INFO"],
"Connected to Postgres version %d\n"
% (self.pgVersion),
context,
bJobMessageType["M_INFO"],
"Connected to Postgres version %d\n" % (self.pgVersion),
)
except:
bareosfd.JobMessage(
Expand All @@ -176,7 +182,7 @@ def start_backup_job(self, context):
startDir = self.options["postgresDataDir"]
self.files_to_backup.append(startDir)
bareosfd.DebugMessage(
context, 100, "dataDir: %s\n" % self.options["postgresDataDir"],
context, 100, "dataDir: %s\n" % self.options["postgresDataDir"]
)
else:
# If level is not Full, we only backup WAL files
Expand Down Expand Up @@ -249,9 +255,7 @@ def start_backup_job(self, context):
# We need a trailing '/' for directories
if os.path.isdir(fullName) and not fullName.endswith("/"):
fullName += "/"
bareosfd.DebugMessage(
context, 100, "fullName: %s\n" % fullName,
)
bareosfd.DebugMessage(context, 100, "fullName: %s\n" % fullName)
# Usually Bareos takes care about timestamps when doing incremental backups
# but here we have to compare against last BackupPostgres timestamp
try:
Expand All @@ -260,7 +264,8 @@ def start_backup_job(self, context):
bareosfd.JobMessage(
context,
bJobMessageType["M_ERROR"],
"Could net get stat-info for file %s: \"%s\"" % (file_to_backup, e.message),
'Could net get stat-info for file %s: "%s"'
% (file_to_backup, e.message),
)
bareosfd.DebugMessage(
context,
Expand Down Expand Up @@ -311,11 +316,11 @@ def start_backup_job(self, context):
self.backupStartTime = datetime.datetime.now(
tz=dateutil.tz.tzoffset(None, self.tzOffset)
)
if not self.execute_SQL(context, "SELECT pg_start_backup('%s');" % self.backupLabelString):
if not self.execute_SQL(
context, "SELECT pg_start_backup('%s');" % self.backupLabelString
):
bareosfd.JobMessage(
context,
bJobMessageType["M_FATAL"],
'pg_start_backup statement failed.',
context, bJobMessageType["M_FATAL"], "pg_start_backup statement failed."
)
return bRCs["bRC_Error"]
results = self.dbCursor.fetchall()
Expand All @@ -324,9 +329,7 @@ def start_backup_job(self, context):
context, 150, "Adding label file %s to fileset\n" % self.labelFileName
)
self.files_to_backup.append(self.labelFileName)
bareosfd.DebugMessage(
context, 150, "Filelist: %s\n" % (self.files_to_backup),
)
bareosfd.DebugMessage(context, 150, "Filelist: %s\n" % (self.files_to_backup))
self.PostgressFullBackupRunning = True
return bRCs["bRC_OK"]

Expand All @@ -341,7 +344,7 @@ def parseBackupLabelFile(self, context):
)

for labelItem in labelFile.read().splitlines():
print labelItem
print(labelItem)
k, v = labelItem.split(":", 1)
self.labelItems.update({k.strip(): v.strip()})
labelFile.close()
Expand Down Expand Up @@ -398,7 +401,8 @@ def restore_object_data(self, context, ROP):
bareosfd.JobMessage(
context,
bJobMessageType["M_ERROR"],
"Could net parse restore object json-data \"%s\ / \"%s\"" % (self.row_rop_raw, e.message),
'Could net parse restore object json-data "%s\ / "%s"'
% (self.row_rop_raw, e.message),
)

if "lastBackupStopTime" in self.rop_data[ROP.jobid]:
Expand Down Expand Up @@ -440,14 +444,11 @@ def closeDbConnection(self, context):
+ "START WAL LOCATION: %s\n" % self.labelItems["START WAL LOCATION"],
)
self.PostgressFullBackupRunning = False
else:
else:
bareosfd.JobMessage(
context,
bJobMessageType["M_ERROR"],
'pg_stop_backup statement failed.',
context, bJobMessageType["M_ERROR"], "pg_stop_backup statement failed."
)


def checkForWalFiles(self, context):
"""
Look for new WAL files and backup
Expand All @@ -466,7 +467,7 @@ def checkForWalFiles(self, context):
bareosfd.JobMessage(
context,
bJobMessageType["M_ERROR"],
"Could net get stat-info for file %s: \"%s\"" % (fullPath, e.message),
'Could net get stat-info for file %s: "%s"' % (fullPath, e.message),
)
continue
fileMtime = datetime.datetime.fromtimestamp(st.st_mtime)
Expand Down
2 changes: 0 additions & 2 deletions core/src/plugins/filed/bareos-fd-postgres.py
Expand Up @@ -21,8 +21,6 @@
#
# Bareos-fd-postgres is a plugin to backup Postgres SQL databases using
# BareosFdPluginPostgres.
# TODO: The plugin argument 'filename' is used to read
# all files listed in that file and add it to the fileset
#
# Author: Maik Aussendorf
#
Expand Down

0 comments on commit 310b1be

Please sign in to comment.