我想让客户端能够手动下载他的数据库的备份。我正在使用PHP和MySQL编码网站。因此,在admin用户登录后,菜单中会出现一个链接,用于将.sql文件下载到本地计算机。我怎样才能用PHP完成这项工作?MySQL数据库的PHP下载备份
回答
这会导致混乱尝试备份从PHP数据库,你将让MySQL的处理这个问题的更好,并告诉mysql做到这一点,最好的方法是使用shell脚本:
#!/bin/bash
# Shell script to backup MySql database
# To backup Nysql databases file to /backup dir and later pick up by your
# script. You can skip few databases from backup too.
# For more info please see (Installation info):
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/01/mysql-backup-script.html
# Last updated: Aug - 2005
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2004, 2005 nixCraft project
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
MyUSER="SET-MYSQL-USER-NAME" # USERNAME
MyPASS="SET-PASSWORD" # PASSWORD
MyHOST="localhost" # Hostname
# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup Dest directory, change this if you have someother location
DEST="/backup"
# Main directory where backup will be stored
MBD="$DEST/mysql"
# Get hostname
HOST="$(hostname)"
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
# DO NOT BACKUP these databases
IGGY="test"
[ ! -d $MBD ] && mkdir -p $MBD || :
# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi
done
然后你告诉PHP告诉操作系统执行该脚本,并等待它完成,这通常是用做exec
或system
如:
exec('/path/to/backup/script.sh');
然后,你可以简单地发送由创建的文件脚本到达浏览器t用户。
+1让MySQL做到这一点是路要走。从上面的脚本中删除重要的是使用'mysqldump'。请参阅http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html以获取更多参考。 – mfonda 2011-04-12 21:13:49
shell脚本似乎是最好的解决方案,因为你可以更好地控制错误等,如果你只是用'mysqldump'和'exec',那么你很可能会遇到一些错误。 – RobertPitt 2011-04-12 21:16:07
感谢这使我在正确的轨道上,但我做了我自己的shell脚本来执行mysqldump命令 – 2011-04-12 22:21:25
- 1. PHP MySQL数据库备份
- 2. PHP下载完整的mysql数据库为.sql备份文件
- 3. Mysql数据库备份使用PHP并在下载时间在自定义指定位置下载备份
- 4. 使用mysqldump Mysql PHP备份数据库
- 5. MySQL PHP Hack预防/数据库备份?
- 6. 备份用PHP整个数据库/ MYSQL
- 7. PHP脚本备份mysql数据库
- 8. 备份MySQL数据库和下载的文件
- 9. MySQL数据库备份
- 10. 备份MySQL数据库
- 11. 如何使用PHP在Windows xampp下备份MySQL数据库?
- 12. 备份数据库从PHP
- 13. 备份mysql数据库的IProblem
- 14. mysqldump的mysql数据库备份
- 15. MySQL数据库的远程备份
- 16. 备份MySQL数据库的Bash脚本
- 17. 备份非常大的MySQL数据库
- 18. 备份mysql数据
- 19. MySQL数据备份?
- 20. 备份和恢复mysql数据库c#
- 21. 如何从NSIS备份MySQL数据库
- 22. 一次备份多个数据库[MySQL]?
- 23. 如何备份MySQL数据库?
- 24. 用SVN备份MySQL数据库?
- 25. 如何备份MySQL数据库
- 26. 用QT备份MySQL数据库
- 27. 如何从Java备份mysql数据库?
- 28. 定期备份MySQL数据库
- 29. MySQL数据库备份 - 安全方式
- 30. 备份数据库使用Mysql查询
http://stackoverflow.com/questions/3595976/backup-mysql-database-with-php类似 – mikel 2011-04-12 21:08:26
[备份mysql数据库并将其作为文件下载]的完全重复(http://stackoverflow.com/问题/ 3751069/backup-a-mysql-database-and-download-as-a-file),以及右边相关列表中的一半问题。 – Charles 2011-04-12 21:08:32