Linux的备份不写字母

大家好!

今天,我想告诉您有关如何使用命令行来管理Veeam Agent for Linux的知识,以及它在程序员熟练的技能中所带来的可能性。

上一篇文章的评论提示我写一篇文章 。 为了说明用户的惊喜:“嗯,怎么办? 服务器不会写信说明已保存!” 而且,据分析人士说,他不是唯一的人,否则该话题将不会出现在论坛上。 既然人们写作,那就意味着有人需要它!

在本文中,我将解释为什么产品中没有此功能。 但是我们不会止步于此,我们将添加此功能! 我们是程序员,因此我们将写一封信并以html页面的形式生成报告。


另外,我将展示最有用的命令,这些命令可以简化管理员的工作。

准备:很多代码,没有图片。

首先,让我们回答一个问题:“为什么Linux的Veeam代理不写字母?”

您可能不喜欢答案,不要怪我。 但是事实是,或多或少的大型企业用户不需要它,这就是原因:

  • 首先,要使用邮件,您必须将smpt服务器放置在本地计算机上,或者在网络上使用其中的一个。 使用最简单的实现( mail命令),您将需要安装mailutils软件包。 而且,许多系统管理员不希望以可以在任何地方发送信件的服务的形式在生产服务器上创建潜在的漏洞。 是的,可能不是由于端口关闭,子网的独立性等引起的。
  • 其次,由于mailutils软件包很可能不在系统上(第一个原因),因此尝试使用它是没有意义的。 否则,我们可以获得一个似乎存在的功能,但“开箱即用”不起作用,这意味着论坛上将有一个主题为“如何配置服务器以便发送电子邮件”的主题。
  • 第三,一般来说,不需要额外的通知,因为或多或少的大型企业客户使用Veeam Backup&Replication 。 它的控制台收集有关对知名存储库进行的所有备份的信息。 自己看看。

Veeam Backup&Replication 9.5 Update 4版本中,可以免费使用此产品,但对服务的虚拟机/物理机有限制。
如果您有多达3个(包括3个)物理服务器,那么免费的VBR功能将绰绰有余。

如果您有3台以上的计算机,则无法购买该软件,但是您仍然想集中监视服务器,建议您自己添加一些脚本。 在C / C ++工作之后,我喜欢在python中娱乐自己。

veeamconfig我们将包装veeamconfig命令veeamconfigveeamconfig团队提供对产品所有功能的访问。 毫无疑问,使用ncurses库创建的伪图形界面更令人赏心悦目,但是,如果您需要将程序链接到新的东西中,那么CLI就是我们的一切。

Veeam Agent for Linux命令的说明对版本3.0有效。 我没有检查以前的版本,所以可能会有差异。

适用于Linux的Veeam代理中的CLI界面非常方便且有据可查。 只需输入veeamconfig --help ,您将获得可用命令的列表:

 $sudo veeamconfig --help Veeam Agent for Linux (c) Veeam Software AG Usage: veeamconfig [command] Commands: repository - Backup repositories management vbrserver - Veeam Backup and Replication servers management job - Backup jobs management backup - Backups management point - Restore points management license - License management agreement - End User License Agreement management config - Import/export configuration schedule - Jobs schedule configuration cloud - Cloud provider management mode - Operation mode session - Sessions management ui - User interface aap - Application-aware processing version, --version, -v - Product version help, --help, -h - Short help 

要查看每个命令允许您执行的操作,只需调用veeamconfig config --help 。 我们得到:

 Veeam Agent for Linux (c) Veeam Software AG Usage: veeamconfig config [command] Commands: import - Import repositories and jobs into database export - Export repositories and jobs from database grabLogs - Collect support logs bundle patchiso - Create custom Veeam Recovery Media adding all hardware drivers from this system help, --help, -h - Short help 

顺便说一下,在这里,我们可以看到grabLogs日志收集grabLogs 。 它可以让您快速收集所有必要的日志以获得支持。 以防万一出问题了。

3.0版中还出现了一个有趣的团队:

 $ sudo veeamconfig agreement --help Veeam Agent for Linux (c) Veeam Software AG Usage: veeamconfig agreement [command] Commands: acceptEula - Accept Veeam End User License Agreements acceptThirdPartyLicenses - Accept Veeam 3rd party License Agreement show - Show End User License Agreements acceptance status help, --help, -h - Short help 

事实是,从3.0版开始,要求用户明确同意许可协议。 看起来像这样:

 $ sudo veeamconfig job list I accept Veeam Software End User License Agreement: /usr/share/doc/veeam/EULA (yes/no | y/n): yes I accept the terms of the following 3rd party software components license agreements: /usr/share/doc/veeam/3rd_party (yes/no | y/n): 

因此,脚本的操作可能会中断。 为了不进入每台计算机并且不手动执行此过程,提供了以下命令:

 veeamconfig agreement acceptEula veeamconfig agreement acceptThirdPartyLicenses 

它们使您可以接受许可协议,而无需其他问题。

但是我们偏离了写信的主题。

对于服务器状态监视任务,我们需要veeamconfig session list命令。 它输出类似:

 Job name Type ID State Started at Finished at bj-home Backup {dbe48e88-3df7-4712-a472-09af8fed4e80} Success 2018-12-05 15:43 2018-12-05 15:44 bj-home Backup {c178a799-2935-4bd6-883b-b11278000076} Success 2018-12-05 16:26 2018-12-05 16:26 bj-home Backup {3405dad3-0016-4a00-933e-60ef66b30324} Success 2018-12-06 06:00 2018-12-06 06:00 

好了,这里有信息备份服务器的时间以及成功的方法。 原则上,已经可以将“排气”收集到文件中并通过信件发送。 但是,一年之内可以增加大约365行。 寻找有错误的状态似乎很乏味。 因此,我们将解析此“精疲力竭”,并获得一个普通列表,您可以使用该列表进行某些操作。

这里查看整个代码

 class CSession: @staticmethod def List(): return subproccall( ["veeamconfig", "session", "list"] ) class CSessionInfoList(object): def __init__(self, list): self.list = list def List(self): return self.list @staticmethod def Get(): text = CSession.List() lines = text.split("\n") list = [] # session info list for line in lines: if len(line) == 0: continue words = line.split() if len(words) == 0: continue if words[0] == "Job": continue if words[0] == "Total": continue try: jobName = words[0] type = words[1] id = words[2] state = words[3] startTime = words[4] + " " + words[5] finishTime = words[6] + " " + words[7] list.append(CSessionInfo(id, type, jobName, state, startTime, finishTime)) except: print "Failed to parse [", line, "]" return CSessionInfoList(list) 

好吧,现在让我们来写一封信,然后寄给自己。

 def SendMailsessions(): print "---" print "Sending statistic to administrator:" sessions = veeamlpb.session.CSessionInfoList.Get() recipient = "dear.admin@company.com" subject = "VAL status notification" text = "Statistic:\n" inx = 0; successCount = 0 warningCount = 0 errorCount = 0 for sessionInfo in sessions.List(): if (sessionInfo.State() == "Success"): successCount += 1 elif (sessionInfo.State() == "Warning"): warningCount += 1 else: errorCount += 1 text += str(successCount)+"/"+str(warningCount)+"/"+str(errorCount)+" Success/Warning/Error\n" text += "Last 10 session:\n" for sessionInfo in reversed(sessions.List()): if inx == 10: text += "...\n" break; text += str(inx)+" | "+sessionInfo.State()+" | "+sessionInfo.JobName()+" | "+sessionInfo.StartTime()+" / "+sessionInfo.FinishTime() + "\n" #text += inx += 1 text += "\n" text += "--------------------------------------------------------------------------------\n" text += " Yours sincerely, Veeam Agent for Linux Monitor\n" print text os.system("echo '"+text+"' | mail -s '"+subject+"' "+recipient) 

结果,在安装mailutils之后,我们可以获得以下形式的字母:

 Statistic: 3/0/0 Success/Warning/Error Last 10 session: 0 | Success | bj-home | 2018-12-06 06:00 / 2018-12-06 06:00 1 | Success | bj-home | 2018-12-05 16:26 / 2018-12-05 16:26 2 | Success | bj-home | 2018-12-05 15:43 / 2018-12-05 15:44 -------------------------------------------------------------------------------- Yours sincerely, Veeam Agent for Linux Monitor 

该字母仅显示最近的10个会话。 同时,有关成功和不成功会话数的信息将显示在该信的开头。 在工作日开始时,只需查看字母中的数字,检查邮件并ing饮咖啡壶即可了解夜间备份是否成功。

如果您需要更明显的内容,则可以以xml格式请求有关会话的信息,并将其传输到服务器。 在那里,将获得的数据合并到一个汇总表中,该汇总表以对您方便或可行的格式显示所有必要的信息。
我们得到两行XML:

 sessionList = veeamlpb.session.CSessionList() text = sessionList.ToXml() 

保存结果文件

 sessionListFileName = "session_list.xml" print "Store XML to file: ",sessionListFileName sessionListXmlFile = open(sessionListFileName, "w") sessionListXmlFile.write(text) sessionListXmlFile.close() 

然后,我们将接收到的XML发送到服务器。 另一种选择是可行的-服务器从备份的计算机中收集XML。 谁是发起者,对我们来说还不重要。 重要的是,XML文件应与所有计算机上的会话列表一起在服务器上收集。 我选择了第一个选项:

 hostname = os.uname()[1] target = "user@admin-desktop:/home/user" os.system("scp ./"+sessionListFileName+" "+target+"/backups/"+hostname+"/session_list.xml") 

现在,在服务器端,它仍然可以处理接收到的数据并创建漂亮的html页面。

 import veeamlpb import os import datetime import xml.etree.ElementTree as xml def main(): hosts = [] backupsDirectory = "/home/user/backups" for item in os.listdir(backupsDirectory): if item in [".", ".."]: continue if os.path.isdir(os.path.join(backupsDirectory,item)): hosts.append(item) print "item: ",item if len(hosts) == 0: return 0 backupSessionMap = {} for host in hosts: print "found host: ", host sessionInfoFile = os.path.join(os.path.join(backupsDirectory,host), "session_list.xml") sessionList = veeamlpb.session.CSessionInfoList.FromXmlFile(sessionInfoFile) backupSessionMap[host] = sessionList for sessionInfo in sessionList.List(): print "Session:",sessionInfo.ToString() html = xml.Element("html") body = xml.SubElement(html, "body", {"style":"background-color: #00b336;"}) xml.SubElement(body,"h1").text = "Report at "+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") xml.SubElement(body,"h2").text = "Statistic:" for host in hosts: sessionList = backupSessionMap[host] success=0 warning=0 error=0 if len(sessionList.List()) == 0: continue for sessionInfo in sessionList.List(): if sessionInfo.State() == "Success": success +=1 elif sessionInfo.State() == "Warning": warning +=1 else: error +=1 latestSessionInfo = sessionList.List()[-1] attr = {} if latestSessionInfo.State() == "Success": #attr["style"] = "background-color: #00b336;" attr["style"] = "background-color: #005f4b; color: white;" elif latestSessionInfo.State() == "Warning": attr["style"] = "background-color: #93ea20;" else: attr["style"] = "background-color: #ba0200; color: white;" xml.SubElement(xml.SubElement(body,"p"),"span", attr).text = \ host + " - "+str(success)+"/"+str(warning)+"/"+str(error)+" Success/Warning/Error" for host in hosts: sessionList = backupSessionMap[host] xml.SubElement(body,"h2").text = host+":" tableStyle =xml.SubElement(body,"style") tableStyle.attrib["type"] = "text/css" tableStyle.text = "TABLE {border: 1px solid green;} TD{ border: 1px solid green; padding: 4px;}" table = xml.SubElement(body,"table") thead = xml.SubElement(table, "thead") xml.SubElement(thead, "th").text = "Number" xml.SubElement(thead, "th").text = "State" xml.SubElement(thead, "th").text = "Job name" xml.SubElement(thead, "th").text = "Start at" xml.SubElement(thead, "th").text = "Complete at" tbody = xml.SubElement(table, "tbody") inx = 0 for sessionInfo in reversed(sessionList.List()): if inx == 10: break; tr = xml.SubElement(tbody,"tr") xml.SubElement(tr, "td").text = str(inx) attr ={} if sessionInfo.State() == "Success": pass elif sessionInfo.State() == "Warning": attr["style"] ="background-color: #93ea20;" else: attr["style"] ="background-color: #ba0200; color: white;" xml.SubElement(tr, "td", attr).text = sessionInfo.State() xml.SubElement(tr, "td").text = sessionInfo.JobName() xml.SubElement(tr, "td").text = sessionInfo.StartTime() xml.SubElement(tr, "td").text = sessionInfo.FinishTime() inx += 1 xml.ElementTree(html).write("summary.html", encoding='utf-8', method='html') return 0 exit(main()) 

结果,报告准备就绪:



我没有任务要做漂亮的产品。 任务是证明收集统计信息的问题可以在一到两天内用脚本解决。

原则上,如果您提出此处提出的想法,则可以制作“用于Veeam Agent for Linux的Open Backup Monitor”。 我认为,这是Python课程工作的好话题,甚至是文凭的好话题,还是在开源项目上练习编程的借口。 同意,练习编程比成为80级精灵更好。

所有代码都可以在http://www.github.com/CodeImp/veeampy/中找到。 下载,使用,补充健康。

请注意,该代码是根据GPL-2许可分发的,可能包含错误以及更多信息。 一切都像开源世界一样。 因此,在生产环境中使用前-不要忘记在测试实验室中开车。

Source: https://habr.com/ru/post/zh-CN432404/


All Articles