使用PHP备份MySql数据库
我在 MySql 中有一个相当大的数据库,我需要每天左右对其进行备份。
我需要能够从任何计算机上进行备份,因此我考虑制作一个 php 脚本来执行此操作并将此 php 脚本放在网上(当然具有密码保护和授权等,以便只有我可以访问它)。
但我想知道,这是如何正确完成的?
我应该使用哪些命令,是否可以更改备份设置(例如添加 AUTO_INCRMENT value = true
)?
我希望有一些例子...
另外,如果这是一个不好的方法(不安全,或者可能会用错误的sql文件提供错误的备份),那么还有什么其他方法是首选? 我有 shell 访问权限,并且有一个 VPS(ubuntu 服务器)。
我的Mysql版本是5.1
谢谢
I have a pretty large db in MySql, and I need to take backups of it every day or so.
I need to be able to take backups from any computer, so therefore I thought about making a php script to do this and put this php script online (offcourse with password protection and authorization etc so that only I can access it).
I wonder however, how is this done properly?
What commands should I use, and is it possible to change settings of the backup (for instance Add AUTO_INCREMENT value = true
)?
I would appreciate examples...
Also, if this is a bad method (unsafe, or maybe gives bad backups with bad sql files), what other method would be preferred?
I have shell-access and I have a VPS (ubuntu server).
My Mysql version is 5.1
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据库备份中无需涉及 PHP。您只需要一个使用 mysqldump 来备份数据库的脚本,并设置 CRON 作业 定期执行脚本:
...将数据库备份到文件,通过将 mysqldump 的输出重定向到指定的文件名。
彼得提出了一个很好的观点,该命令只会给你一天的归档时间——任何超过两天的归档文件都会被覆盖。这将允许您拥有可回溯 7 天的滚动日志:
另请注意,文件权限将适用 - 如果执行脚本的用户没有文件夹的权限,则无法写入文件。
There's no need to involve PHP in the database backup. You just need a script that uses mysqldump to backup the database, and setup a CRON job to periodically execute the script:
...will backup your database to a file, by redirecting the output from the mysqldump to the specified file name.
Peter brought up a good point, that the command would only give you one day of archiving--any archive over two days old would be overwritten. This would allow you have a rolling log going back seven days:
Also be aware that file permissions will apply - can't write a file if the user executing the script doesn't have permissions to the folder.
我同意 OMG Ponies mysqldump + 脚本是可行的方法。
我使用的唯一其他选项是设置从服务器。这提供了针对硬件故障的几乎即时备份,并且可以位于与主服务器不同的建筑物中。除非您对数据库进行大量写入,否则不一定需要非常强大的服务器,因为它不处理查询,只处理数据库更新。
I agree with OMG Ponies mysqldump + script is the way to go.
The only other option that I use is to set up a slave server. This provides an almost instant backup against hardware failure and can be located in a different building to your main server. Unless you have a large number of writes to the database, you don't necessarily need a very powerful server as it is not processing queries, only database updates.