Mayx's Home Page https://mabbs.github.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
6.9 KiB

  1. ---
  2. layout: post
  3. title: 论备份的重要性
  4. tags: [备份]
  5. ---
  6. 只有事情发生到自己头上才想到要解决<!--more-->
  7. # 起因
  8. 今天早上发生了一件很糟糕的事情,一打开聊天软件就发现有人在说我维护的花火学园挂掉了,错误信息是无法访问源站。我觉得挺奇怪,服务器又没啥负载,我也有一段时间没登进去了,怎么服务器就挂掉了?
  9. 我试着用SSH连接,同样无法连接,我发现事情不太对劲,然后就登到了Vultr里看了看。结果发现我的服务器在00:00之后就像一个死人一样,CPU负载被拉成了一条直线,就那样保持0%的位置。我以为是因为莫名其妙的原因服务器关机了,然而我重启以后仍然没有解决问题。
  10. 登录到终端一看,`No bootable device`就这样显示在屏幕上,硬盘直接读不出来了,这下可不得了了,我赶紧去快照里看了一下,发现最后一次快照的时间在5月30日,也就是说如果没能恢复数据这十几天的所有信息都将消失!
  11. # 难以想象的垃圾服务商:Vultr
  12. 首先我要做的事情当然是想办法先恢复服务,虽然那个快照有点早,但是先顶上再说吧……
  13. 想一想我的防护应该做的也没啥问题,而且一般成功入侵服务器的人也应该是删库然后留一条信息的那种,直接干死硬盘的我还真没见过。于是我开始发Ticket给Vultr,看看到底是怎么回事。
  14. Vultr在我问完的4个小时后给出了最终的解决方案,把我的服务器直接重置,在上面安了新的操作系统然后给我赔了两个月的服务器费用……原文如下:
  15. > Hello,
  16. >
  17. > In the past 24 hours, we sent notification of a node failure impacting your cloud server listed above.
  18. >
  19. > Despite extensive efforts, our attempts to manually recover your cloud server were unsuccessful.
  20. >
  21. > Our engineering team is currently deploying new instances with the same operating system and IP and you will receive login details in a separate message. You may also deploy a backup or snapshot on a new instance with a new IP if you prefer.
  22. >
  23. > Our staff will be applying a two month account credit for the affected services shortly.
  24. >
  25. > Regards,
  26. > Bryan M.
  27. > Systems Administrator
  28. 哇,这真是太糟糕了,作为一家云服务器商就直接把客户的数据搞没了,然后就赔2个月的费用?要知道数据无价啊,就这么不负责任的吗?简直是不可思议啊!
  29. 不过我也没什么好办法了,也许他们不重装我还想着试试SystemRescueCD试试看能不能把整个磁盘复制出来,但是他们既然已经直接重装那就彻底没救了……QAQ
  30. # 亡羊补牢
  31. 既然数据已经救不回来了,那我们也只能向前看,得想办法避免以后再出现这样的问题。因为我最近在期末阶段,比较忙,所以也不经常去打快照。虽然以前也出现过服务出问题的情况,像[MySQL挂了](/2020/01/05/devops.html)、CDN挂了、还有一次好像是交换机出问题了,但是无论如何数据从来没有丢失过。这一次数据都能丢了也真的是太糟糕了,要不是有快照,那就真成删库跑路了……
  32. 既然没时间打快照,我得想个办法搞一个自动打快照的东西。在网上搜了搜,还真有这样的脚本,于是我拿来改了改就装上去用了。
  33. ## 自动快照的脚本
  34. ```python
  35. import requests
  36. from requests import get
  37. import re
  38. import json
  39. class __RPC:
  40. def __init__(self, api_key, name):
  41. self.api_key = api_key
  42. self.api_info = None
  43. self.name = name
  44. self.errors = {
  45. 200: "Function successfully executed.",
  46. 400: "Invalid API location. Check the URL that you are using.",
  47. 403: "Invalid or missing API key. Check that your API key is present and matches your assigned key.",
  48. 405: "Invalid HTTP method. Check that the method (POST|GET) matches what the documentation indicates.",
  49. 412: "Request failed. Check the response body for a more detailed description.",
  50. 500: "Internal server error. Try again at a later time.",
  51. 503: "Rate limit hit. API requests are limited to an average of 2/s. Try your request again later."
  52. }
  53. def api_info_initial(self):
  54. self.api_info = {"snapshot/create":"POST","snapshot/destroy":"POST","snapshot/list":"GET","server/list":"GET"}
  55. def __getattr__(self, name):
  56. return eval("__RPC")(self.api_key, self.name + "/" + name)
  57. def __call__(self, **kwargs):
  58. if not self.api_info:
  59. self.api_info_initial()
  60. if self.name not in self.api_info:
  61. raise ValueError("The API is not exists.")
  62. if self.api_info[self.name] == "GET":
  63. res = requests.get("https://api.vultr.com/v1/" + self.name, headers={"API-Key": self.api_key},
  64. params=kwargs)
  65. elif self.api_info[self.name] == "POST":
  66. res = requests.post("https://api.vultr.com/v1/" + self.name, headers={"API-Key": self.api_key}, data=kwargs)
  67. if res.status_code == 200:
  68. return res.status_code, res.text.strip()
  69. elif res.status_code in self.errors.keys():
  70. return res.status_code, self.errors.get(res.status_code)
  71. else:
  72. res.raise_for_status()
  73. class Vultr:
  74. def __init__(self, api_key):
  75. self.api_key = api_key
  76. def __getattr__(self, name):
  77. return eval("__RPC")(self.api_key, name)
  78. vultr = Vultr("API Key")
  79. data = {'SUBID': '实例ID'}
  80. status_code, resp = vultr.snapshot.create(**data)
  81. requests.post("https://sc.ftqq.com/SCKEY.send",data ={"text":"快照已创建","desp": str(status_code)+resp})
  82. # 删除旧快照
  83. status_code, resp = vultr.snapshot.list() # /v1/snapshot/list
  84. if status_code != 200:
  85. print('获取快照列表失败' + str(status_code) + resp)
  86. else:
  87. print('成功获取到快照列表')
  88. data_list = list(json.loads(resp).values())
  89. data_list.sort(key=lambda x: x['date_created']) # 默认时间排序,由近到远
  90. data_list_del = data_list[::-1][9:] # 取超过9个之后的快照
  91. for data_del in data_list_del:
  92. data = {'SNAPSHOTID': data_del.get('SNAPSHOTID')}
  93. status_code, resp = vultr.snapshot.destroy(**data) # /v1/snapshot/destroy
  94. if status_code != 200:
  95. print('删除旧快照失败')
  96. else:
  97. print('成功删除一个旧快照')
  98. ```
  99. 把这个脚本放到Crontab里,每天执行一次就行了。
  100. # 后记
  101. 相信服务器厂商是完全靠不住的事情,自己还得想办法做好备份。我甚至在想,阿三把Intel和微软都占领了,会不会有一个阿三也跑到Vultr里,然后对着我的硬盘大喊“把你变成咖喱”之类的23333。
  102. 现在不过是权宜之计,以后还是得想办法把整个论坛下载到本地,至少能搞个数据库的差异备份啥的也行啊……

Powered by TurnKey Linux.