Acronis命令后查询备份状态

我想在备份成功完成时执行操作(但不是在失败时)。

  • 有没有办法将备份状态传递给后备命令?
  • 或者我可以从后备份查询环境variables?
  • 或者查询作业状态的简单方法(可以运行多个作业)。
  • 考虑login到事件日志并使用LogParsersearch成功消息。

我正在使用电子邮件通知,发送电子邮件到机器人,从主题parsing出备份状态,然后运行一个命令,如果备份成功。

产品:Acronis Backup&Recovery 10

从我所看到的,不是一个简单的方法。

像你一样,我目前得到一个任务后的状态电子邮件; 不过,我想将这些信息的一部分放到仪表板系统中。 我刚刚写了一个python(3.4)脚本来从Acronis日志中提取信息并写入日志文件。 我已经添加了有条件地执行命令的function。 (我在一个batch file中完成了这部分,但是可以修改python脚本来执行命令…从而不需要batch file。)

稍微修改/定制,这应该适合你。

概念

  • Acronis命令后操作将启动batch file
  • …启动一个python脚本
  • …parsingAcronis日志,检测成功或失败(以及其他),并返回错误代码
  • …由batch file捕获
  • 然后根据成功或失败有条件地执行一项行动

注意事项

  • python脚本将读取上次修改的日志文件,因此并发的Acronis操作可能会很危险。
  • python脚本只读取具有以星期几开头的文件名的日志文件。 (即它会忽略任何“console_xxxxx.log”[和类似的]日志,但这是我们想要的。)
  • 我是一个贸易开发者,但这是我第一次尝试python,所以它可能不是很漂亮。

要求

  • Python 3.4安装在环境path中,并与.py文件关联
  • untangle python包安装( pip install untangle

脚步

  1. 创build以下batch file并将其保存为acronis-last-log.cmd 。 更改适用的命令以有条件地执行操作。

     @echo off REM Filename: acronis-last-log.cmd acronis-last-log.py if ERRORLEVEL 1 ( echo This statement is executed whenever the Acronis task FAILS echo Insert any action here within the if-clause ) else ( echo This statement is executed whenever the Acronis task SUCCEEDS echo Insert any action here within the else-clause ) 
  2. 创build下面的python脚本,并将其保存为acronis-last-log.py并将其放在与batch file相同的文件夹中。 确保访问CONFIGURATION VARIABLES部分以更改任何path或选项。 注意:这会创build一个基于任务名称的日志文件,每次执行Acronis任务时都会覆盖自身。 要禁用日志文件,请with open(...) as outFile注释with open(...) as outFileprint(..., file=outFile)行,确保根据需要调整任何代码缩进。 要更改日志path,请编辑outputPathvariables。

     # Filename: # acronis-last-log.py # # Description: # Process an Acronis log file and print out relevant output in a formatted string. # # Rules: # 1. if any log entry is greater than ACRONIS_LOG_INFO, report that the task has failed. # - This is how the Acronis emails work. Warnings will cause the "failed" summary import glob import os import sys import textwrap import time import untangle # to install: pip install untangle ########## CONSTANTS DECONSTRUCTED FROM ACRONIS LOGS ########################### # log module that provides the overall task status ACRONIS_STATUS_MODULE = 316 # Acronis log error levels ("levels") # (0 and 1 are dummy entries that don't seem to exist within the logs) ACRONIS_LOG_DUMMY0 = 0 ACRONIS_LOG_DUMMY1 = 1 ACRONIS_LOG_INFO = 2 ACRONIS_LOG_WARNING = 3 ACRONIS_LOG_ERROR = 4 # Error level descriptions # descriptions for printing, indexed by the above constants # padded to 7 characters long for alignment ERROR_LEVEL_DESCRIPTIONS = ["DUMMY0 ", "DUMMY1 ", "INFO ", "WARNING", "ERROR "] ########## CONFIGURATION VARIABLES ############################################# # showSubMessages # show auxiliary messages that meet the error level threshold (set below) # True: show subMessages # False: only show the overall exit error level (module 316) showSubMessages = True # logPath # path to Acronis log files (default: C:\ProgramData\Acronis\TrueImageHome\Logs) logPath = r'C:\ProgramData\Acronis\TrueImageHome\Logs' # outputPath # path to where this script will output outputPath = r'.' # debug # turn debugging on? (default: False) debug = False # minLevelToDisplay # minimum error level to display (default: ACRONIS_LOG_WARNING) minLevelToDisplay = ACRONIS_LOG_WARNING # maxLevelToDisplay # maximum error level to display (default: ACRONIS_LOG_ERROR) maxLevelToDisplay = ACRONIS_LOG_ERROR ########## HELPER FUNCTIONS #################################################### def logDescription(errorLevel): """Return a log description based on Acronis error level.""" return ERROR_LEVEL_DESCRIPTIONS[errorLevel] ########## FUNCTIONS ########################################################### def process(logFile): """Process an Acronis log file and print out relevant output in a formatted string. with !showSubMessages, just a single line is printed: yyyy-mm-dd hh:mm:ss ERRORLEVEL [AcronisTask] Summary eg 2014-12-25 14:16:40 WARNING [MyBackupTask] execution failed with showSubMessages, multiple will be printed: yyyy-mm-dd hh:mm:ss ERRORLEVEL [AcronisTask] Summary ERRORLEVEL SubMessage 1 ERRORLEVEL SubMessage 2 ERRORLEVEL SubMessage n eg 2014-12-25 14:16:40 ERROR [MyBackupTask] execution failed ERROR The quotas are violated. ERROR Cannot perform this operation in quiet mode. (0x103F1) Tag = 0x1D8EAB676A3F6BAA Target drive is running out of space. (0x4001D) Tag = 0x1D8EAB676A3F6BAB WARNING Terminated by user. WARNING Batch operation has been terminated by user. Note: the first ERRORLEVEL printed (the one between the timestamp and [AcronisTask]) will be the highest error level in the log """ # store the highest error level highestLevel = ACRONIS_LOG_DUMMY0 subMessages = [] success = False try: xmlDocument = untangle.parse(logFile) # read task_name taskName = xmlDocument.log['task_name'] # open output file with open(outputPath + r"\acronis-" + taskName + ".log", 'w') as outFile: if debug: print("Debug mode enabled. Processing", logFile) print("Debug mode enabled. Processing", logFile, file=outFile) # for each log event for event in xmlDocument.log.event: # read attributes eventId = int(event['id']) eventLevel = int(event['level']) eventModule = int(event['module']) eventCode = int(event['code']) eventTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(event['time']))) eventMessage = event['message'] # strip unicode characters out (yes, it happens for some INFO messages annotating user responses) eventMessage = eventMessage.encode('ascii', 'ignore').decode('ascii', 'ignore') # set highestLevel if eventLevel > highestLevel: highestLevel = eventLevel # add subMessages, if they fit into the level threshold if (eventLevel >= minLevelToDisplay) and \ (eventLevel <= maxLevelToDisplay): subMessages.append([logDescription(eventLevel), eventMessage]) # create summary message for top line summary = "execution failed" # determine success if highestLevel <= ACRONIS_LOG_INFO: summary = "completed successfully" success = True # print the summary message if (highestLevel >= minLevelToDisplay) and \ (highestLevel <= maxLevelToDisplay): print(eventTime, logDescription(highestLevel), "[" + taskName + "]", summary) print(eventTime, logDescription(highestLevel), "[" + taskName + "]", summary, file=outFile) # print subMessages, maybe if showSubMessages: for message in subMessages: # do some fancy textwrapping here, because sometimes there are really long messages that wrap in the wrong places # and a hanging indent is prettier print(' '*(len(eventTime)+1) + message[0], textwrap.fill(message[1], 160, subsequent_indent=' '*30)) #30 = len("YYYY-MM-DD HH:MM:SS ERRCODE ") + 2 for indenting print(' '*(len(eventTime)+1) + message[0], textwrap.fill(message[1], 160, subsequent_indent=' '*30), file=outFile) #30 = len("YYYY-MM-DD HH:MM:SS ERRCODE ") + 2 for indenting except: if debug: # probably want to catch the error in debug... raise else: print("Generic Error with file <" + logFile + ">.") # return success flag return success ########## ENTRY POINT ######################################################### if __name__ == "__main__": # only grab files named with a day of week # so, ignore non-compliant (won't parse) logs # - console*, # - monitor.log, # - afcdpsrv.log # - NSB*.log (non-stop backup) logFiles = [f for f in glob.iglob(logPath + "\*.log") \ if "Sunday" in f or \ "Monday" in f or \ "Tuesday" in f or \ "Wednesday" in f or \ "Thursday" in f or \ "Friday" in f or \ "Saturday" in f] # sort by modified date (descending) logFiles.sort(key=os.path.getmtime, reverse=True) # get the most recent newestFile = logFiles[0] # process newest file success = process(newestFile) # for testing purposes... # process all log files #for logFile in logFiles: # process(logFile) # return with exit code 0 if success (no warnings or errors), otherwise 1 sys.exit(0 if success else 1) 
  3. 将Acronisconfiguration为使用以下对话框设置作为后命令操作运行batch file( acronis.cmd ):

    • 命令: C:/path/to/acronis.cmd
    • 工作目录: C:/path/to (batch file的位置)
    • 参数:(留空)
    • []在命令的执行完成之前,不要执行操作
    • [x]如果用户命令失败,则中止操作

编辑:选中“不执行操作…”checkbox可能会产生XMLparsing错误,因为日志文件可能没有被刷新。

  1. 看到networking的全面的答案 – 这是我会build议的路线。

  2. 在Cyber​​的发布之前,我已经在LogParser方面取得了一些成功,因为我想跟踪2个备份工作。

注意事项 LogParser解决scheme容易出现误报(例如,如果一项工作运行了两次,另一项工作没有运行,那么在两项工作成功的情况下,您将获得相同的结果)。

Acronis 10似乎没有提供足够的详细信息到Windows日志,以便能够唯一标识作业成功或甚至开始。

入住的Acronis-BACKUP.BAT

 "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" -i:evt file:check-acronis-backup.sql > check-acronis-backup.out type check-acronis-backup.out grep "Elements output: 2" check-acronis-backup.out if %ERRORLEVEL%==0 goto ReportSuccess GOTO End :ReportSuccess call report-success acronis :End 

入住的Acronis-backup.sql

 SELECT TOP 10 SourceName, TimeGenerated, EventCategory, Message FROM Application WHERE TimeGenerated > TO_TIMESTAMP(SUB(TO_INT(SYSTEM_TIMESTAMP()), 90000)) --90000 = 60*60*25 AND SourceName = 'Acronis Backup Recovery 10' AND EventCategory = 144 AND Message like '%Task _Full backup_ completed successfully.%'