通用远程数据备份和下载 - 包括 InnoDb 支持

发布于 2024-10-12 11:54:54 字数 2111 浏览 5 评论 0原文

我想要一个基于 PHP 的解决方案来备份远程服务器的数据库(仅数据而不是代码)并下载文件。我知道基于 Shell 的解决方案更适合执行此类操作(在本地系统上运行 shell 脚本并通过 SSH 连接到远程系统),但需要一个基于 PHP 的解决方案,其中知道 URL 并拥有数据库凭据就足够了非技术人员进行备份。 PHP脚本可以上传到远程服务器并执行。

以下是我想要的功能: -

  • 至少应该支持 InnoDb 引擎 - 应该导出外键约束。如果它支持所有其他引擎也没有什么坏处。
  • 应该在存在最大可能限制的情况下在所有服务器上工作(我知道一些限制,例如safe_mode启用,exec()system() 功能已禁用等)。我想要一个非常通用的解决方案,保证在任何地方都能工作。

  • 进程应该经过密码验证(要求提供数据库凭据)。

现在,我正在分解事情并从最基础的开始。以下是我到目前为止对事情的假设和一些问题:-

  1. 我不确定系统功能是否像 execsystem 等。可以在共享托管服务器中完全禁用,也可以不禁用。如果它们被禁用而无法被覆盖,则基于 mysqldump 的解决方案给出 这里并不通用。
    问题 - 但是,如果只有 safe_mode 处于开启状态,系统函数就可以在 safe_mode_exec_dir 中存在的文件上执行,该解决方案能否安全运行?

  2. 我问了一个关于的问题使用 PHP 执行此操作存在安全风险,并且理解永远不应该创建备份文件(我假设,在基于 mysqldump 的解决方案中,需要在下载之前首先创建备份文件)在网络空间中。因此,该解决方案不需要在那里创建备份文件(如果在其他位置创建也没有问题)。
    问题 - 但是,共享托管提供商会允许这样做吗?

  3. 我检查了各种通用用户贡献的 PHP 类,例如 phpmysqldump 等,并没有找到基于 mysqldump 的解决方案使用系统命令进行备份的用法。他们执行诸如 SHOW CREATE TABLE 等操作来获取所有表创建、数据插入查询,然后下载这些内容,而无需实际将其保存为文件(因此没有安全风险)。
    问题-我是否正确地得出这样的结论:他们在没有执行第一点解决方案中给出的简单mysqldump的情况下完成了所有这些事情,因为这不能是通用且安全的解决方案?
    问题 - 另外,我读到没有任何效果良好的好方法。我个人只使用这个 phpmysqldump 当我尝试使用创建的备份恢复数据库时,它给我 mysql 错误。转储文件中的查询看起来也与 PhpMyAdmin 的导出模块创建的查询有些不同。我还检查了其他一些免费用户贡献的 PHP 类。看起来它们中的大多数不支持 InnoDb 支持,因此外键约束(如果存在于数据库中)不会存在于导出中。
    问题 - 我猜,PhpMyAdmin 本身的导出功能(如果单独存在)可能是我的解决方案。有人知道像这样的稳定​​库吗?

I want a PHP based solution to backup database (only data and not code) of a remote server and download the file. I know that Shell based solutions are better for doing such things (running a shell script on local system and connecting through SSH to remote system) but it is a requirement to have a PHP based solution where knowing a URL and having database credentials is enough for a non-techie to take backups. The PHP script can be uploaded to the remote server and executed.

Following are the features I want:-

  • Should have support for InnoDb engine at least - foreign key constraints should be exported. No harm if it supports all other engines.
  • Should work on all servers, in the presence of the maximum possible amount of restrictions (I know about a few restrictions like safe_mode enabled, exec(), system() functions disabled etc.). I want a very general purpose solution which is guaranteed to work anywhere.

  • Process should be password authenticated (asks for database credentials).

Now, I am breaking down things and starting from the very basics. Following are my assumptions of things so far and some questions:-

  1. I am not sure if system functions like exec, system etc. can be completely disabled in shared hosting servers or not. If they are disabled such that they cannot be overridden, then the mysqldump based solution given here will not work universally.
    Question - If however only safe_mode is on such that system functions can execute on files present inside safe_mode_exec_dir, will the solution work securely?

  2. I asked a question regarding security risks of doing this using PHP and understood that the backup file should never be created (I assume, in case of a mysqldump based solution, backup file needs to be created first before downloading) in the webspace. So, the solution should not need the backup files to be created there (no problem if creates in other locations).
    Question - But, will shared hosting providers allow this?

  3. I checked out various general-purpose user contributed PHP classes like phpmysqldump etc. and did not find the usage of mysqldump based solution using system commands there to take backup. They do things like SHOW CREATE TABLE etc. to get all the table creation, data insertion queries and then download those things without actually saving it as a file (so no security risk).
    Question - Am I correct to conclude that they do all these things without doing a simple mysqldump as given in the solution in the first point because this cannot be a general purpose and secure solution?
    Question - Also, I read that there aren't any good ones which work well. I personally used only this phpmysqldump and it gives me mysql errors when I try to restore a database with the backup created. The queries in the dump file also look somewhat different from those created by PhpMyAdmin's export module. I also checked a few other free user contributed PHP classes. It looks like most of them do not support InnoDb support and so foriegn key constraints, if present in the database are not present in the export.
    Question - The export functionality of PhpMyAdmin itself, if present separately could be the solution for me, I guess. Does anybody know of any stable library like this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-10-19 11:54:54

我认为你应该做什么:

我认为你应该在你的服务器上安装 phpmyadmin,这将允许你从工作/学校/咖啡馆/等访问你的数据库,MySQL-workbench 更先进,并为你提供更多功能,以便你可以处理更改结构并编辑任何行/列、关系等等,请查看 phpmyadmin 的大部分功能(如果不是全部的话)。

phpmyadmin 它适用于任何网络浏览器:

我真的推荐 phpMyAdmin 它有许多 SQL 功能可以帮助您处理涉及 MySQL 数据库的所有事情,如果您使用 innoDB,那么您可以获得更多功能,例如表之间的关系。

phpMyAdmin 具有以下功能:

  • 直观的 Web 界面
  • 支持大多数 MySQL 功能:
  • 浏览和删除数据库、表、视图、字段和索引
  • 创建、复制、删除、重命名和更改数据库、表、字段和索引
  • 维护服务器、数据库和表,并提供服务器配置建议
  • 执行、编辑任何 SQL 语句并添加书签,甚至批量查询
  • 管理 MySQL 用户和权限
  • 管理存储过程和触发器
  • 从 CSV 和 SQL 导入数据
  • 将数据导出为各种格式:CSV、SQL、XML、PDF、ISO/IEC 26300 - OpenDocument 文本和电子表格、Word、Excel、LATEX 等
  • 管理多个服务器
  • 创建数据库的 PDF 图形布局
  • 使用示例查询 (QBE) 创建复杂查询
  • 在数据库或其子集中进行全局搜索
  • 使用一组预定义函数将存储的数据转换为任何格式,例如将 BLOB 数据显示为图像或下载链接
  • 等等...

您上面列出的所有内容都包含在 phpMyAdmin 中,如果您运行的是 debian 或基于 Debian 的系统,只需运行:

root@debian:~ # aptitude install phpmyadmin
root@arch:~ # pacman -S phpmyadmin

顺便说一句:如果您没有使用 Apache 或 lighttpd 作为 http 服务器,您将需要阅读conf 文件phpmyadmin 然后编写 phpmyadmin 所需的 conf 脚本以与您的 http 服务器一起使用。

MySQL 工作台。它是跨平台的并且运行良好。

MySQL 工作台直观地查看您正在对数据库执行的操作。 http://diariolinux.com/wp-content/uploads/2008/08/wb51linuxpreview2a.png

顺便说一句:使用 +来正向设计数据库,我花了一段时间才找到这样做的方法。

独立的 perl 文件,在配置后即可工作:(未经测试)

use DBI;
my $user = "username";   # MySQL Username
my $pass = "xxxx";       # MySQL Password
my $host = "localhost";  # MySQL Host
my $mydb = "zzzz";       # MySQL Database
my $file = "test.sql";   # Import file
my $sqlServer = "mysql"; # What sql-server are we using, oracle/mysql/etc
    # I would use the following method to configure it, though the above works fine too.
($user,$pass,$host,$mydb,$file,sqlServer) = (
    "username",  # MySQL Username
    "password",  # MySQL Password
    "localhost", # MySQL Host
    "myDB",      # MySQL Database
    "test.sql",  # Imported file
    "mysql"      # What sql-server are we using, oracle/mysql/etc
);
    # Now lets connect to the MySQL server.
my $dbh  = DBI->connect("DBI:$sqlServer:$mydb:$host",$user,$pass)or die DBI->errstr();
    # Lets now open the .sql file.
open(INPUT,$file);
    # Now lets run each sql-statement.
while ($line = <INPUT>){
    print $line;
    $dbh->do($line);
    print "Query failed (run manually):\n$line\n\n ". $dbh->errstr()."\n" if $dbh->errstr();
}
    # Now close the file.
close(INPUT);

What I think you should do:

I Think you should install phpmyadmin on your server, this will allow you to access your database from work/school/cafe/etc, MySQL-workbench is more advanced and gives you more features so you can deal with changing the structure and editing any rows/columns, relations, and much more, look at phpmyadmin's features it has most if not all.

phpmyadmin it works in any web-browser:

I really recommended phpMyAdmin it has many SQL features to help you deal with everything when it comes to the MySQL database if you are using innoDB then you get even more features such as relation-ships between tables.

phpMyAdmin has the following features:

  • Intuitive web interface
  • Support for most MySQL features:
  • browse and drop databases, tables, views, fields and indexes
  • create, copy, drop, rename and alter databases, tables, fields and indexes
  • maintenance server, databases and tables, with proposals on server configuration
  • execute, edit and bookmark any SQL-statement, even batch-queries
  • manage MySQL users and privileges
  • manage stored procedures and triggers
  • Import data from CSV and SQL
  • Export data to various formats: CSV, SQL, XML, PDF, ISO/IEC 26300 - OpenDocument Text and Spreadsheet, Word, Excel, LATEX and others
  • Administering multiple servers
  • Creating PDF graphics of your database layout
  • Creating complex queries using Query-by-example (QBE)
  • Searching globally in a database or a subset of it
  • Transforming stored data into any format using a set of predefined functions, like displaying BLOB-data as image or download-link
  • And much more...

Everything you listed above is included in phpMyAdmin, if you are running debian or Debian-based system simply run:

root@debian:~ # aptitude install phpmyadmin
root@arch:~ # pacman -S phpmyadmin

BTW: if you are not using Apache or lighttpd for the http-server you will need to read through the conf files for phpmyadmin and then write up the required conf script for phpmyadmin to work with your http-server.

MySQL workbench. Its cross-platform and works great.

MySQL workbench visurally see what you are doing with your database. http://diariolinux.com/wp-content/uploads/2008/08/wb51linuxpreview2a.png

BTW: Use <ctrl>+<G> to forward engineer a database, It took me a while to find out for to do this.

Stand alone perl file that works just after you configure it: (untested)

use DBI;
my $user = "username";   # MySQL Username
my $pass = "xxxx";       # MySQL Password
my $host = "localhost";  # MySQL Host
my $mydb = "zzzz";       # MySQL Database
my $file = "test.sql";   # Import file
my $sqlServer = "mysql"; # What sql-server are we using, oracle/mysql/etc
    # I would use the following method to configure it, though the above works fine too.
($user,$pass,$host,$mydb,$file,sqlServer) = (
    "username",  # MySQL Username
    "password",  # MySQL Password
    "localhost", # MySQL Host
    "myDB",      # MySQL Database
    "test.sql",  # Imported file
    "mysql"      # What sql-server are we using, oracle/mysql/etc
);
    # Now lets connect to the MySQL server.
my $dbh  = DBI->connect("DBI:$sqlServer:$mydb:$host",$user,$pass)or die DBI->errstr();
    # Lets now open the .sql file.
open(INPUT,$file);
    # Now lets run each sql-statement.
while ($line = <INPUT>){
    print $line;
    $dbh->do($line);
    print "Query failed (run manually):\n$line\n\n ". $dbh->errstr()."\n" if $dbh->errstr();
}
    # Now close the file.
close(INPUT);
憧憬巴黎街头的黎明 2024-10-19 11:54:54

一般来说,PHPMyAdmin 是一个很好的解决方案,但如果您只想进行备份,则可能有点过分了。如果您想要一个通用的解决方案,那么绝对不要依赖可用的 exec。 PHP 与正确的 SQL 查询相结合(正如您在第 3 点中所指出的)可以为您提供所需的所有信息,并且始终有效。

PHPMyAdmin is a great solution in general but may be overkill if you only ever want to do backups. If you want a universal solution then absolutely don't rely on exec being available. PHP combined with the right SQL queries (as you have noted in your point 3.) can provide you with all the information you need and will always work.

找回味觉 2024-10-19 11:54:54

根据我自己的经验,依赖基于 PHP 的备份解决方案是非常糟糕的主意。如果您正在处理大型备份,PHP 很容易失败(由于超时、内存消耗或未定义的原因)。

多年来,我们已经在我们的 un*x 服务器上成功使用 Bacula (http://www.bacula.org/en/)。我们自己的备份脚本可以做我们想做的任何事情。当然,您需要具有对服务器的 shell 访问权限。

如果您需要简单的基于 php 的备份解决方案,您最好编写自己的解决方案。 :)

In my own experience, relying on PHP based backup solution is VERY bad idea. If you are dealing with large backups, PHP can easily fail (due to timeouts, memory consumption or something undefined).

We have been successfully using Bacula (http://www.bacula.org/en/) for years now on our un*x servers. Did our own backup scripts which do whatever we want to do. You need to have shell access to server off course.

If you need simple backup php based solution, you are best with writing your own. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文