PHP 网站备份?

发布于 2024-10-10 09:51:11 字数 347 浏览 6 评论 0原文

有谁知道一个干净的基于 PHP 的解决方案可以使用 FTP 备份远程网站?

必须具备:

  • 递归 FTP 备份
  • 可以通过 cron 作业运行
  • 易于配置(轻松添加多个站点)
  • 备份文件的本地存储就足够了

更好:

  • 备份的站点存储为 zip 文件
  • 管理事物的良好界面
  • 提供通知备份成功或失败时
  • 是否进行增量备份
  • 是否进行 MySQL 数据库备份

我需要它基于 PHP,因为它将用于不允许使用标准 GNU/Linux 工具的共享托管包。

Does anybody know a clean PHP-based solution that can backup remote web sites using FTP?

Must haves:

  • Recursive FTP backups
  • Possible to run through a cron job
  • Easy to configure (easy adding of multiple sites)
  • Local storage of backup files is sufficient

Would be nice:

  • Backed up sites are stored as zip files
  • A nice interface to manage things
  • Provides notification when backup has succeeded or failed
  • Does incremental backups
  • Does MySQL Database backups

I need this to be PHP based because it's going to be used on shared hosting packages that do not allow usage of the standard GNU/Linux tools.

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

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

发布评论

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

评论(5

傾旎 2024-10-17 09:51:11

我之前在 cron 作业 PHP 脚本中做过类似的事情。不确定这是否是最好的方法,但它确实有效。

$backup_file = '/home/example/sql_backup/mo_'.date('Y-m-d').'.sql.gz';
$command = '/usr/bin/mysqldump -c -h'.DB_HOST.' -u'.DB_USER.' -p'.DB_PASS.' --default-character-set=latin1 -N '.DB_NAME.' | gzip > '.$backup_file;
exec($command);

然后您可以执行 sftp 到远程服务器。

您可以使用 exec() 和 linux 压缩来类似地处理文件夹。

I have done something like this in a cron job PHP script before. Not sure if it is the best way, but it certainly works.

$backup_file = '/home/example/sql_backup/mo_'.date('Y-m-d').'.sql.gz';
$command = '/usr/bin/mysqldump -c -h'.DB_HOST.' -u'.DB_USER.' -p'.DB_PASS.' --default-character-set=latin1 -N '.DB_NAME.' | gzip > '.$backup_file;
exec($command);

You could then exec an sftp to the remote server.

You could do the file folders similarly using exec() and linux zipping.

×纯※雪 2024-10-17 09:51:11

我编写此代码是为了处理 FTP 备份,不确定它是否适合您的特定需求:

class Backup
{
    public $ftp = null;
    public $files = array();

    public function FTP($host, $user = null, $pass = null, $port = 21, $path = '/')
    {
        if ((extension_loaded('ftp') === true) && (extension_loaded('zip') === true))
        {
            $this->ftp = ftp_connect($host, $port, 5);

            if (is_resource($this->ftp) === true)
            {
                if (ftp_login($this->ftp, $user, $pass) === true)
                {
                    $zip = new ZipArchive();

                    if (is_object($zip) === true)
                    {
                        ftp_pasv($this->ftp, true);

                        if ($zip->open(sprintf('./%s_%s.zip', $host, date('YmdHis', time())), ZIPARCHIVE::CREATE) === true)
                        {
                            $this->FTP_Map($path);

                            while (($file = array_shift($this->files)) !== null)
                            {
                                if (preg_match('~/$~', $file) > 0)
                                {
                                    $zip->addEmptyDir(preg_replace('~^[\\/]+~', '', $file));
                                }

                                else
                                {
                                    $stream = tempnam(sys_get_temp_dir(), __CLASS__);

                                    if (is_file($stream) === true)
                                    {
                                        if (ftp_get($this->ftp, $stream, $file, FTP_BINARY, 0) === true)
                                        {
                                            $zip->addFromString(preg_replace('~^[\\/]+~', '', $file), file_get_contents($stream));
                                        }

                                        unlink($stream);
                                    }
                                }
                            }
                        }

                        $zip->close();
                    }
                }

                ftp_close($this->ftp);
            }
        }

        return false;
    }

    public function FTP_Map($path = '/')
    {
        if (is_resource($this->ftp) === true)
        {
            $files = ftp_nlist($this->ftp, ltrim($path, '/'));

            if (is_array($files) === true)
            {
                foreach ($files as $file)
                {
                    if (@ftp_chdir($this->ftp, $file) !== true)
                    {
                        $this->files[] = sprintf('/%s/%s', trim($path, '\\/'), trim($file, '\\/'));
                    }

                    else if (ftp_cdup($this->ftp) === true)
                    {
                        if (array_push($this->files, sprintf('/%s/%s/', trim($path, '\\/'), trim($file, '\\/'))) > 0)
                        {
                            $this->FTP_Map(sprintf('/%s/%s/', trim($path, '\\/'), trim($file, '\\/')));
                        }
                    }
                }

                return true;
            }
        }

        return false;
    }
}

用法:

$Backup = new Backup();

$Backup->FTP('demo.wftpserver.com', 'demo-user', 'demo-user', 21, '/text/');

对于 MySQL 备份,SELECT INTO OUTFILE 会这样做吗?

I coded this to handle the FTP backups, not sure if it fits your specific needs tho:

class Backup
{
    public $ftp = null;
    public $files = array();

    public function FTP($host, $user = null, $pass = null, $port = 21, $path = '/')
    {
        if ((extension_loaded('ftp') === true) && (extension_loaded('zip') === true))
        {
            $this->ftp = ftp_connect($host, $port, 5);

            if (is_resource($this->ftp) === true)
            {
                if (ftp_login($this->ftp, $user, $pass) === true)
                {
                    $zip = new ZipArchive();

                    if (is_object($zip) === true)
                    {
                        ftp_pasv($this->ftp, true);

                        if ($zip->open(sprintf('./%s_%s.zip', $host, date('YmdHis', time())), ZIPARCHIVE::CREATE) === true)
                        {
                            $this->FTP_Map($path);

                            while (($file = array_shift($this->files)) !== null)
                            {
                                if (preg_match('~/$~', $file) > 0)
                                {
                                    $zip->addEmptyDir(preg_replace('~^[\\/]+~', '', $file));
                                }

                                else
                                {
                                    $stream = tempnam(sys_get_temp_dir(), __CLASS__);

                                    if (is_file($stream) === true)
                                    {
                                        if (ftp_get($this->ftp, $stream, $file, FTP_BINARY, 0) === true)
                                        {
                                            $zip->addFromString(preg_replace('~^[\\/]+~', '', $file), file_get_contents($stream));
                                        }

                                        unlink($stream);
                                    }
                                }
                            }
                        }

                        $zip->close();
                    }
                }

                ftp_close($this->ftp);
            }
        }

        return false;
    }

    public function FTP_Map($path = '/')
    {
        if (is_resource($this->ftp) === true)
        {
            $files = ftp_nlist($this->ftp, ltrim($path, '/'));

            if (is_array($files) === true)
            {
                foreach ($files as $file)
                {
                    if (@ftp_chdir($this->ftp, $file) !== true)
                    {
                        $this->files[] = sprintf('/%s/%s', trim($path, '\\/'), trim($file, '\\/'));
                    }

                    else if (ftp_cdup($this->ftp) === true)
                    {
                        if (array_push($this->files, sprintf('/%s/%s/', trim($path, '\\/'), trim($file, '\\/'))) > 0)
                        {
                            $this->FTP_Map(sprintf('/%s/%s/', trim($path, '\\/'), trim($file, '\\/')));
                        }
                    }
                }

                return true;
            }
        }

        return false;
    }
}

Usage:

$Backup = new Backup();

$Backup->FTP('demo.wftpserver.com', 'demo-user', 'demo-user', 21, '/text/');

For MySQL backups, would SELECT INTO OUTFILE do it?

累赘 2024-10-17 09:51:11

实际上,我写了一篇文章,其中包含脚本,介绍如何使用 PHP、Bash 和其他一些开源软件发送有关备份的预格式化电子邮件通知来完成此操作。

http://codeuniversity.com/scripts/scr1

尽管不涉及 FTP,但我的要求非常相似。这一切都是在本地完成的。看看吧。也许您会发现它很有用。

I actually wrote an article w/ included scripts on how I accomplished this using PHP, Bash and some other pieces of open source software to send out the pre-formatted email notifications about the backups.

http://codeuniversity.com/scripts/scr1

My requirements were fairly similar although there is no FTP involved. It's all done locally. Give it a look. Perhaps you may find it useful.

楠木可依 2024-10-17 09:51:11

我正在使用 myRepono,除了你提到的之外,如果执行快速和自动备份,它非常好稳定、安全。

I’m using myRepono, in addition to what you have mentioned, if performs fast and automatic backups and it’s very stable and secure.

故笙诉离歌 2024-10-17 09:51:11

虽然您的共享主机可能不会提供很多工具,但它必须至少提供一些工具,可能值得询问您的主机他们提供或推荐的备份工具。您想要备份到几乎相同的主机吗? (至少在软件方面)

要成功运行rsync,它只需要在一台机器上运行,另一台机器甚至不需要知道rysnc存在,哪一台机器运行它也没关系(备份或主要主机)。

您是否可以通过 cli 访问服务器来安装软件包或 php 模块?如果您不这样做,那么所有的赌注都会被取消,因为您的 php 安装很可能不会包含任何必要的软件包,因此无法开始考虑从 php 尝试此操作。

我会推荐一个非 php 解决方案,例如 rysnc 或某种 bash 脚本。尽管您可以将进程包装在 php 包装器中来管理它。

While your shared hosting may not provide many tools, it must provide at least some, it might be worth asking your host what they provide or recommend for backups. Do you want to back up to a pretty much identical host. (at least in terms of software)

To run rsync successfully it only has to be running on one machine, the other doesn't need to even know that rysnc exists, it also doesn't matter which one of the machines is running it (backup or primary host).

Do you have cli access to the server to install packages or php modules? If you don't then all bets are off as the chances are that your php install won't include any of the necessary packagesn to begin to think about attempting this from php.

I would reccommend a non php solution like rysnc or a bash script of some kind. Although you could wrap the process in a php wrapper to manage it.

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