如何在php中创建备份数据库脚本并将其压缩 [英] how to create a backup database script in php and zip it

查看:89
本文介绍了如何在php中创建备份数据库脚本并将其压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用php备份数据库并从mysql备份数据的最佳方法,我试图在线寻找教程,但是我似乎不太了解我写了这篇文章,尽管它不能很好地工作,因为出现这些错误并警告

what is the best way to backup a database using php and backing data from mysql,i have tried to look for tutorials online but i seem not to understand nicely well i wrote this though its not working nicely,im getting these errors and warnings

Warning: gzopen(backups/adminpanel/adminpanel_aggrement_1367616442.sql.gz): failed to open stream: No such file or directory in C:\xampp\htdocs\how are things\admin panel\backup.php on line 17

Notice: Undefined variable: time in C:\xampp\htdocs\how are things\admin panel\backup.php on line 30
The file--backups/adminpanel/aggrement_.sql.gz--could not be opened for writing.

第17行和第30行的代码是这个

the code on line 17 and 30 is this

if ($fp = gzopen ("$dir/{$db_name}_{$table}_{$bu_time}.sql.gz", 'w9')) {



  echo "<p>The file--$dir/{$table}_{$time}.sql.gz--could not be opened for writing.</p>\n";

这是我的代码

<?php 
$db_name = 'adminpanel';
$dir = "backups/$db_name";
if (!file_exists('path/to/directory')) {
    @mkdir('path/to/directory');
}
$bu_time = time();
$dbc = @mysqli_connect (localhost, root, nokiae71, adminpanel);
$q = 'SHOW TABLES';
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
    echo "<p>Backing up database '$db_name'.</p>\n";
    while (list($table) = mysqli_fetch_array($r, MYSQLI_NUM)) {
        $q2 = "SELECT * FROM $table";
        $r2 = mysqli_query($dbc, $q2);
        if (mysqli_num_rows($r2) > 0) {
            if ($fp = gzopen ("$dir/{$db_name}_{$table}_{$bu_time}.sql.gz", 'w9')) {
                                while ($row = mysqli_fetch_array($r2, MYSQLI_NUM)) {
                    foreach ($row as $value) { 

                        gzwrite ($fp, "'$value', ");
                    }
                    gzwrite ($fp, "\n"); 

                }               
                // Close the file:
                gzclose ($fp); 

            } else { // Could not create the file!
                echo "<p>The file--$dir/{$table}_{$time}.sql.gz--could not be opened for writing.</p>\n";
                break; 
            }   
        }
    } 
} else {
    echo "<p>The submitted database--$db_name--contains no tables.</p>\n";
}

?>

我该如何进行这项工作,或者还有什么更好的脚本可以使用...

how can i make this work or is there any better script i can use out there...

推荐答案

现在,此解决方案使用 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.

这篇关于如何在php中创建备份数据库脚本并将其压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆