编写perl脚本以使用mysqldump进行mysql增量备份

发布于 2024-10-12 13:49:22 字数 198 浏览 6 评论 0原文

我正在处理centos中mysql数据库的增量备份解决方案。我需要编写一个 perl 脚本来进行增量备份。然后我将使用 crontabs 运行这个脚本。我有点困惑。有解决方案,但并没有真正的帮助。我做了很多研究。对文件进行全量备份和增量备份的方法有很多种。我可以很容易地理解它们,但我需要对 mysql 数据库进行增量备份。我不知道该怎么做。任何人都可以帮助我建议源代码或一段代码。

I am dealing with an incremental backup solution for a mysql database in centos. I need to write a perl script to take incremental backup. then i will run this script by using crontabs. I am a bit confused. There are solutions but not really helping. I did lots of research. there are so many ways to take full backup and incremental backup for files. I can easily understand them but I need to take an incremental backup of a mysql database. I do not know how to do it. Can anyone help me either advising a source or a piece of code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-10-19 13:49:22

MySQL 在此记录了您一直在查看的增量备份方法:

http://dev.mysql.com/doc/refman/5.0/en/binary-log.html

您本质上要做的是设置 mysql 实例以将任何更改写入数据库到这个二进制日志。这意味着任何更新、删除、插入等都会进入二进制日志,但不会选择语句(它们不会更改数据库,因此不会进入二进制日志)。

一旦您的 mysql 实例在打开二进制日志记录的情况下运行,您就可以进行完整备份并记下主位置。然后,为了进行增量备份,您需要从主位置运行 mysqlbinlog,其输出将是自您进行完整备份以来对数据库所做的所有更改。此时您需要再次记下主位置,以便您知道要从中进行下一次增量备份的位置。

显然,如果您反复进行多个增量备份,则需要保留所有这些增量备份。我建议经常进行完整备份。

事实上,如果可以的话,我建议始终进行完整备份。在我看来,进行增量备份只会给你带来痛苦,但如果你需要这样做,那肯定是一种方法。

The incremental backup method you've been looking at is documented by MySQL here:

http://dev.mysql.com/doc/refman/5.0/en/binary-log.html

What you are essentially going to want to do is set up your mysql instance to write any changes to your database to this binary log. What this means is any updates, deletes, inserts etc go in the binary log, but not select statements (which don't change the db, therefore don't go in the binary log).

Once you have your mysql instance running with binary logging turned on, you take a full backup and take note of the master position. Then later on, to take an incremental backup, you want to run mysqlbinlog from the master position and the output of that will be all the changes made to your database since you took the full backup. You'll want to take note of the master position again at this point, so you know the point that you want to take the next incremental backup from.

Clearly, if you then take multiple incremental backups over and over, you need to retain all those incremental backups. I'd recommend taking a full backup quite often.

Indeed, I'd recommend always doing a full backup, if you can. Taking incremental backups is just going to cause you pain, IMO, but if you need to do it, that's certainly one way to do it.

只等公子 2024-10-19 13:49:22

mysqldump 就是门票。

示例:

mysqldump -u [user_name] -p[password] --database [database_name] >/tmp/databasename.sql

-u = mysql 数据库用户名

-p = mysql 数据库密码

注意:-p 选项后面没有空格。如果您必须在 perl 中执行此操作,那么您可以使用系统函数来调用它,如下所示:

system("mysqldump -u [user_name] -p[password] --database [database_name] >/tmp/databasename.sql") or die "system call failed: $?";

请注意执行此操作涉及的安全风险。如果有人碰巧在系统运行时列出了​​系统上运行的当前进程的列表,他们就能够看到用于数据库访问的凭据。

mysqldump is the ticket.

Example:

mysqldump -u [user_name] -p[password] --database [database_name] >/tmp/databasename.sql

-u = mysql database user name

-p = mysql database password

Note: there is no space after the -p option. And if you have to do this in perl, then you can use the system function to call it like so:

system("mysqldump -u [user_name] -p[password] --database [database_name] >/tmp/databasename.sql") or die "system call failed: $?";

Be aware though of the security risks involved in doing this. If someone happened to do a listing of the current processes running on a system as this was running, they'd be able to see the credentials that were being used for database access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文