Web site backup in PHP?

人走茶凉 提交于 2019-11-28 17:38:19

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.

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?

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’m using myRepono, in addition to what you have mentioned, if performs fast and automatic backups and it’s very stable and secure.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!