简体   繁体   English

使用git处理MySQL备份是否可行?

[英]Is it viable to handle MySQL backups with git?

Today I had this really neat idea for backing up my database: put the dump file in a git repository, then commit on each dump so that I have both the most recent copy, but can easily roll back to any previous backup. 今天我有一个非常好的想法来备份我的数据库:将转储文件放在一个git存储库中,然后在每个转储上提交,以便我有最新的副本,但可以轻松回滚到任何以前的备份。 I can also easily pull a copy of the repository on a regular basis to keep a copy on my own computer as a backup of the backups. 我还可以定期轻松地提取存储库的副本,以便将副本作为备份的备份保存在我自己的计算机上。 It definitely sounds clever. 这听起来很聪明。

However, I'm aware that clever solutions sometimes have fundamental flaws. 但是,我知道聪明的解决方案有时会有根本的缺陷。 What sort of issues might I hit storing mysqldump diffs in git? 我可以在git中存储mysqldump差异的哪些问题? Is it worth it? 这值得么? What do most people do in order to have multiple database backups on the server and keep redundant copies elsewhere? 大多数人为了在服务器上进行多个数据库备份并在其他地方保留冗余副本而做了什么?

Normally you don't keep every backup (or snapshot) forever. 通常,您不会永久保留每个备份(或快照)。 A git repository does keep every checkin you ever make. git存储库保留您进行的每次检查。 If you ever decide to prune old revisions (say month-old revisions down to once a week, year old to once a month, etc) you will have to do it with git filter-branch which will rewrite the entire history. 如果您决定修改旧修订版(比如每月修订一次,每月修订一次,每月一次等),您将不得不使用git filter-branch进行修改,这将重写整个历史记录。 Then git gc to remove the unwanted revisions. 然后git gc删除不需要的修订。

Given that git's strengths are distributed version control and complex patch/branch workflows (neither of which apply to snapshots or backups) I'd consider using a different VCS with a more malleable history. 鉴于git的优势是分布式版本控制和复杂的补丁/分支工作流(两者都不适用于快照或备份),我会考虑使用具有更具延展性的不同VCS。

This approach sounds fine to me. 这种方法听起来不错。 I use Git for backing up my own important data. 我使用Git来备份我自己的重要数据。

Note that you are not storing diffs -- Git effectively stores snapshots of the directory state with each commit. 请注意,您不存储差异 - Git会在每次提交时有效地存储目录状态的快照。 You can generate the diff of two commits, but the actual storage mechanism has nothing to do with diff. 您可以生成两个提交的差异,但实际的存储机制与diff无关。

In theory this will work, but you will start to have problems when the database dumps get large. 理论上这可行,但是当数据库转储变大时,您将开始遇到问题。

Git doesn't have any hard file size limits, but it will diff the contents of your latest dump with the one previously stored in the repository, which will require at least as much memory as the sizes of both of those files added together - so I would imagine it will start to get very slow, very quickly with files over 100MB (or even 10MB). Git没有任何硬文件大小限制,但它会将最新转储的内容与先前存储在存储库中的内容区分开来,这将至少需要与这两个文件的大小一起添加的内存 - 所以我认为文件超过100MB(甚至10MB)的速度会非常慢。

Git wasn't made for dealing with files of this type (ie big data files instead of source code), so I think this is fundamentally a bad idea. Git不是用来处理这种类型的文件(即大数据文件而不是源代码),所以我认为这基本上是一个坏主意。 You could, however, use something like Dropbox to store the dumps - which will still save the version history for you, but is more tailored towards files which can't effectively be diffed. 但是,您可以使用类似Dropbox的内容来存储转储 - 这仍然会为您保存版本历史记录,但更适合于无法有效区分的文件。

If you're using MySQL (and possibly others) and have binary logging enabled, you might consider setting up a git repo for the directory of your bin log and developing a strategy to regularly commit updates to the binlog. 如果您正在使用MySQL(可能还有其他人)并启用了二进制日志记录,您可以考虑为bin日志的目录设置一个git repo,并制定策略以定期提交binlog的更新。

In MySQL, the binlog stores the queries that change data to any tables on the database. 在MySQL中,binlog存储将数据更改为数据库上任何表的查询。 If you sync up your commits with regular dumps of the database, you should have a versioned way to restore data. 如果将提交与数据库的常规转储同步,则应具有恢复数据的版本化方法。

Honestly, I think just using MySQL's native tools would probably be a better solution, but what I've outlined here gets you versioning your MySQL data which is what I think you were after in the first place. 老实说,我认为使用MySQL的原生工具可能是一个更好的解决方案,但我在这里概述的内容可以让你对MySQL数据进行版本控制,这是我认为你首先要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM