简体   繁体   English

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

[英]writing perl script to take mysql incremental backup with mysqldump

I am dealing with an incremental backup solution for a mysql database in centos. 我正在用centos处理mysql数据库的增量备份解决方案。 I need to write a perl script to take incremental backup. 我需要编写一个perl脚本来进行增量备份。 then i will run this script by using crontabs. 然后我将使用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. 我可以很容易地理解它们,但是我需要对mysql数据库进行增量备份。 I do not know how to do it. 我不知道怎么做。 Can anyone help me either advising a source or a piece of code. 任何人都可以帮助我建议源代码或一段代码。

The incremental backup method you've been looking at is documented by MySQL here: MySQL记录了您一直在研究的增量备份方法:

http://dev.mysql.com/doc/refman/5.0/en/binary-log.html 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. 本质上,您要做的是设置mysql实例,以将对数据库的所有更改写入此二进制日志。 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). 这意味着在二进制日志中进行任何更新,删除,插入等操作,但不执行select语句(不会更改数据库,因此不会在二进制日志中进行处理)。

Once you have your mysql instance running with binary logging turned on, you take a full backup and take note of the master position. 一旦您的mysql实例在二进制日志打开的情况下运行,就进行完整备份并记下主位置。 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 . 然后,要进行增量备份,您想从主位置运行mysqlbinlog,其输出将是自进行完整备份以来对数据库所做的所有更改。 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. IMO,进行增量备份只会让您感到痛苦,但是,如果您需要这样做,那肯定是一种方法。

mysqldump is the ticket. mysqldump是票证。

Example: 例:

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

-u = mysql database user name -u = mysql数据库用户名

-p = mysql database password -p = mysql数据库密码

Note: there is no space after the -p option. 注意:-p选项后没有空格。 And if you have to do this in perl, then you can use the system function to call it like so: 而且,如果必须在perl中执行此操作,则可以使用系统函数来调用它,如下所示:

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. 如果有人偶然列出了正在运行的系统上当前正在运行的进程的清单,那么他们将能够查看用于数据库访问的凭据。

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

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