2011-04-12 117 views
2

我想让客户端能够手动下载他的数据库的备份。我正在使用PHP和MySQL编码网站。因此,在admin用户登录后,菜单中会出现一个链接,用于将.sql文件下载到本地计算机。我怎样才能用PHP完成这项工作?MySQL数据库的PHP下载备份

+0

http://stackoverflow.com/questions/3595976/backup-mysql-database-with-php类似 – mikel 2011-04-12 21:08:26

+0

[备份mysql数据库并将其作为文件下载]的完全重复(http://stackoverflow.com/问题/ 3751069/backup-a-mysql-database-and-download-as-a-file),以及右边相关列表中的一半问题。 – Charles 2011-04-12 21:08:32

回答

3

这会导致混乱尝试备份从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告诉操作系统执行该脚本,并等待它完成,这通常是用做execsystem如:

exec('/path/to/backup/script.sh'); 

然后,你可以简单地发送由创建的文件脚本到达浏览器t用户。

+0

+1让MySQL做到这一点是路要走。从上面的脚本中删除重要的是使用'mysqldump'。请参阅http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html以获取更多参考。 – mfonda 2011-04-12 21:13:49

+0

shell脚本似乎是最好的解决方案,因为你可以更好地控制错误等,如果你只是用'mysqldump'和'exec',那么你很可能会遇到一些错误。 – RobertPitt 2011-04-12 21:16:07

+0

感谢这使我在正确的轨道上,但我做了我自己的shell脚本来执行mysqldump命令 – 2011-04-12 22:21:25