Home > Article > Backend Development > CPanel panel uses PHP to automatically back up website files_PHP tutorial
The CPanel panel is a very powerful virtual host management panel. If you are a space business owner, you may be familiar with this CP panel. Now I will introduce how to use PHP to automatically back up website files.
代码如下 | 复制代码 |
// PHP script to allow periodic cPanel backups automatically. // Based on script posted by max.hedroom in cpanel.net forums // This script contains passwords. KEEP ACCESS TO THIS FILE SECURE! // ********* 下面项目需要设定 ********* // cPanel登录信息 $cpuser = "**"; // CPanel帐号 $cppass = "***"; // CPanel密码 $domain = "***"; // cPanel所在域名或IP $skin = "x3"; // 正在使用的cPanel风格,默认"x3"。 /************** 备份目的地 *************** "homedir" - 本地主目录 "ftp" - 远程FTP备份(主动模式) "passiveftp" - 远程FTP备份(被动模式) "scp" - 加密传输(需服务器支持) ****************************************/ $savedir = "ftp"; // 备份目的地,默认"homedir",即只做本地备份。 // FTP上传的信息(备份目的地为本地的,可不填) $ftpuser = "***"; // FTP帐号 $ftppass = "***!"; // FTP密码 $ftphost = "***"; // FTP服务器IP $ftpport = "21"; // FTP端口,默认21 $ftprdir = "/"; // 远程FTP目录,默认"/" // 通知信息 $notifyemail = "**@gmail.com"; // 信息反馈邮件地址 // 安全模式 $secure = 1; // 0为标准http, 1为ssl(需要主机支持ssl) // 设定值为1时会在排程记录中产生网页报告 $debug = 0; // *********** 以下内容不要更改! ********* if ($secure) { $url = "ssl://".$domain; $port = 2083; } else { $url = $domain; $port = 2082; } $socket = fsockopen($url,$port); if (!$socket) { echo "Failed to open socket connection... Bailing out!n"; exit; } // Encode authentication string $authstr = $cpuser.":".$cppass; $pass = base64_encode($authstr); $params = "dest=$savedir&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftprdir&submit=Generate Backup"; // Make POST to cPanel fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0rn"); fputs($socket,"Host: $domainrn"); fputs($socket,"Authorization: Basic $passrn"); fputs($socket,"Connection: Closern"); fputs($socket,"rn"); // Grab response even if we don't do anything with it. while (!feof($socket)) { |
Copy the above code and save it as: fullbackup.php file, upload it to a directory other than public_html
This global script contains the cPanel login account and password. For security reasons, you need to put this script in a folder other than the public_html folder, such as: "/home/yourname/backup", and set the folder's The permissions are 600. Remember!
2. Fill in the relevant information and change the file name to fullbackup.php
3. Upload the above script to the "/home/yourdomain/beifen" directory. (If there is no beifen directory, you can create a new one)
4. Change the permissions of the beifen directory to 600
5. Open the Cron Jobs in the cpanel panel.
Select the job frequency and enter
in the command field The code is as follows
|
Copy code | ||||