且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php中创建备份数据库脚本并将其压缩

更新时间:2023-02-12 23:31:29

现在,此解决方案使用 cron ,但是我觉得这很容易,所以我认为值得一提. Cron可能已经安装在您使用的服务器上.

Now, this solution uses cron, but I find it very easy so I thought it was worth mentioning. Cron may already be installed on the server you're using.

这是我计划将cron每晚运行一次的命令:

This is the command that I schedule cron to run once a night:

mysqldump -h URL_TO_YOUR_DATABASE.com -u YOUR_MYSQL_USERNAME -pYOUR_MYSQL_PASSWORD --single-transaction YOUR_DATABASE_NAME | gzip > ~/PATH_WHERE_YOU_WANT_TO_PLACE_BACKUPS/db_backup_$(date +%Y-%m-%d).sql.gz

就是这样.

这告诉mysqldump将整个数据库转储为sql.然后gzip压缩它.然后将其存储在服务器上的某个位置,并将日期附加在文件名的末尾.

This tells mysqldump to dump your whole database as sql. Then gzip compresses it. And then it's stored in a spot on your server with the date appended to the end of the filename.

如果您真的想用php而不是cron触发它,则可以尝试从php调用 mysqldump .

If you really want to trigger it with php instead of cron you could try calling mysqldump from php.