Web site backup in PHP?

2019-01-21 17:46发布

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.

标签: php ftp backup
5条回答
贼婆χ
2楼-- · 2019-01-21 18:09

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.

查看更多
你好瞎i
3楼-- · 2019-01-21 18:09

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

查看更多
来,给爷笑一个
4楼-- · 2019-01-21 18:15

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.

查看更多
家丑人穷心不美
5楼-- · 2019-01-21 18:22

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.

查看更多
我命由我不由天
6楼-- · 2019-01-21 18:26

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?

查看更多
登录 后发表回答