{"id":3536,"date":"2016-02-27T21:32:58","date_gmt":"2016-02-28T02:32:58","guid":{"rendered":"http:\/\/underhost.com\/blog\/?p=3536"},"modified":"2016-02-27T21:41:44","modified_gmt":"2016-02-28T02:41:44","slug":"automatic-cpanel-backup-domain-mysql-with-cron-php","status":"publish","type":"post","link":"https:\/\/underhost.com\/blog\/automatic-cpanel-backup-domain-mysql-with-cron-php\/","title":{"rendered":"Automatic cPanel backup (Domain &#038; MySQL) with cron &#038; PHP"},"content":{"rendered":"<div class=\"smallfont\"><strong>Automatic cPanel backup (domain &amp; MySQL) with cron &amp; PHP<\/strong><\/div>\n<hr size=\"1\" \/>\n<div id=\"post_message_1023\">\n<p>The situation is this: You use cPanel on our web hosting server. You use the cPanel backup tool to regularly backup \/home directory (includes my web files, mail, etc), and my MySQL databases. You love the fact that you can use it to backup to a remote FTP server, and you do that on a fairly regular basis.<\/p>\n<p>The major drawback is that you have to remember\/schedule to perform this backup manually. This becomes especially difficult if you have multiple cPanel accounts. It would be amazing if you could just schedule the cPanel backup to run at regular intervals, perhaps with cron. Well, although there&#8217;s no option for that in cPanel, the script below will allow you to do exactly that!<\/p>\n<p>This backup script includes SSL support. This isn&#8217;t necessary if you run the script on the server for which you&#8217;re generating the backup; but the SSL support is very important if you&#8217;re running the script somewhere else to connect to your cPanel hosting account.<\/p>\n<div>\n<div class=\"smallfont\">PHP Code:<\/div>\n<div class=\"alt2\" dir=\"ltr\"><code><code><code>&lt;?php<br \/>\n\/\/\u00a0PHP\u00a0script\u00a0to\u00a0allow\u00a0periodic\u00a0cPanel\u00a0backups\u00a0automatically,\u00a0optionally\u00a0to\u00a0a\u00a0remote\u00a0FTP\u00a0server.<br \/>\n\/\/\u00a0This\u00a0script\u00a0contains\u00a0passwords.\u00a0KEEP\u00a0ACCESS\u00a0TO\u00a0THIS\u00a0FILE\u00a0SECURE!\u00a0(place\u00a0it\u00a0in\u00a0your\u00a0home\u00a0dir,\u00a0not\u00a0\/www\/)<\/code><\/code><\/code><\/div>\n<div class=\"alt2\" dir=\"ltr\">\/\/\u00a0*********\u00a0THE\u00a0FOLLOWING\u00a0ITEMS\u00a0NEED\u00a0TO\u00a0BE\u00a0CONFIGURED\u00a0*********\/\/\u00a0Info\u00a0required\u00a0for\u00a0cPanel\u00a0access<br \/>\n$cpuser\u00a0=\u00a0&#8220;username&#8221;;\u00a0\/\/\u00a0Username\u00a0used\u00a0to\u00a0login\u00a0to\u00a0CPanel<br \/>\n$cppass\u00a0=\u00a0&#8220;password&#8221;;\u00a0\/\/\u00a0Password\u00a0used\u00a0to\u00a0login\u00a0to\u00a0CPanel<br \/>\n$domain\u00a0=\u00a0&#8220;example.com&#8221;;\u00a0\/\/\u00a0Domain\u00a0name\u00a0where\u00a0CPanel\u00a0is\u00a0run<br \/>\n$skin\u00a0=\u00a0&#8220;paper_lantern&#8221;;\u00a0\/\/\u00a0Set\u00a0to\u00a0cPanel\u00a0skin\u00a0you\u00a0use\u00a0(script\u00a0won&#8217;t\u00a0work\u00a0if\u00a0it\u00a0doesn&#8217;t\u00a0match).\u00a0Most\u00a0people\u00a0run\u00a0the\u00a0default\u00a0x\u00a0theme\/\/\u00a0Info\u00a0required\u00a0for\u00a0FTP\u00a0host<br \/>\n$ftpuser\u00a0=\u00a0&#8220;ftpusername&#8221;;\u00a0\/\/\u00a0Username\u00a0for\u00a0FTP\u00a0account<br \/>\n$ftppass\u00a0=\u00a0&#8220;ftppassword&#8221;;\u00a0\/\/\u00a0Password\u00a0for\u00a0FTP\u00a0account<br \/>\n$ftphost\u00a0=\u00a0&#8220;ftp.example.com&#8221;;\u00a0\/\/\u00a0Full\u00a0hostname\u00a0or\u00a0IP\u00a0address\u00a0for\u00a0FTP\u00a0host<br \/>\n$ftpmode\u00a0=\u00a0&#8220;ftp&#8221;;\u00a0\/\/\u00a0FTP\u00a0mode\u00a0(&#8220;ftp&#8221;\u00a0for\u00a0active,\u00a0&#8220;passiveftp&#8221;\u00a0for\u00a0passive)\/\/\u00a0Notification\u00a0information<br \/>\n$notifyemail\u00a0=\u00a0&#8220;you@example.com&#8221;;\u00a0\/\/\u00a0Email\u00a0address\u00a0to\u00a0send\u00a0results\/\/\u00a0Secure\u00a0or\u00a0non-secure\u00a0mode<br \/>\n$secure\u00a0=\u00a00;\u00a0\/\/\u00a0Set\u00a0to\u00a01\u00a0for\u00a0SSL\u00a0(requires\u00a0SSL\u00a0support),\u00a0otherwise\u00a0will\u00a0use\u00a0standard\u00a0HTTP\/\/\u00a0Set\u00a0to\u00a01\u00a0to\u00a0have\u00a0web\u00a0page\u00a0result\u00a0appear\u00a0in\u00a0your\u00a0cron\u00a0log<br \/>\n$debug\u00a0=\u00a00;<\/p>\n<p>\/\/\u00a0***********\u00a0NO\u00a0CONFIGURATION\u00a0ITEMS\u00a0BELOW\u00a0THIS\u00a0LINE\u00a0*********<\/p>\n<p>if\u00a0($secure)\u00a0{<br \/>\n$url\u00a0=\u00a0&#8220;ssl:\/\/&#8221;.$domain;<br \/>\n$port\u00a0=\u00a02083;<br \/>\n}\u00a0else\u00a0{<br \/>\n$url\u00a0=\u00a0$domain;<br \/>\n$port\u00a0=\u00a02082;<br \/>\n}<\/p>\n<p>$socket\u00a0=\u00a0fsockopen($url,$port);<br \/>\nif\u00a0(!$socket)\u00a0{\u00a0echo\u00a0&#8220;Failed\u00a0to\u00a0open\u00a0socket\u00a0connection&#8230;\u00a0Bailing\u00a0out!\\n&#8221;;\u00a0exit;\u00a0}<\/p>\n<p>\/\/\u00a0Encode\u00a0authentication\u00a0string<br \/>\n$authstr\u00a0=\u00a0$cpuser.&#8221;:&#8221;.$cppass;<br \/>\n$pass\u00a0=\u00a0base64_encode($authstr);<\/p>\n<p>$params\u00a0=\u00a0&#8220;dest=$ftpmode&amp;email=$notifyemail&amp;server=$ftphost&amp;user=$ftpuser&amp;pass=$ftppass&amp;submit=Generate\u00a0Backup&#8221;;<\/p>\n<p>\/\/\u00a0Make\u00a0POST\u00a0to\u00a0cPanel<br \/>\nfputs($socket,&#8221;POST\u00a0\/frontend\/&#8221;.$skin.&#8221;\/backup\/dofullbackup.html?&#8221;.$params.&#8221;\u00a0HTTP\/1.0\\r\\n&#8221;);<br \/>\nfputs($socket,&#8221;Host:\u00a0$domain\\r\\n&#8221;);<br \/>\nfputs($socket,&#8221;Authorization:\u00a0Basic\u00a0$pass\\r\\n&#8221;);<br \/>\nfputs($socket,&#8221;Connection:\u00a0Close\\r\\n&#8221;);<br \/>\nfputs($socket,&#8221;\\r\\n&#8221;);<\/p>\n<p>\/\/\u00a0Grab\u00a0response\u00a0even\u00a0if\u00a0we\u00a0don&#8217;t\u00a0do\u00a0anything\u00a0with\u00a0it.<br \/>\nwhile\u00a0(!feof($socket))\u00a0{<br \/>\n$response\u00a0=\u00a0fgets($socket,4096);<br \/>\nif\u00a0($debug)\u00a0echo\u00a0$response;<br \/>\n}<\/p>\n<p>fclose($socket);<\/p>\n<p>?&gt;<\/p>\n<\/div>\n<\/div>\n<p><b>To schedule the script to run regularly, save it as fullbackup.php in your top directory (not \/public_html, which would be less secure), and enter a new cron job like the following:<\/b><\/p>\n<div>\n<div class=\"smallfont\">Code:<\/div>\n<pre class=\"alt2\" dir=\"ltr\">15 2 * * * \/usr\/local\/bin\/php \/home\/youraccount\/fullbackup.php<\/pre>\n<\/div>\n<p>(Runs every night at 2:15 a.m.)<\/p>\n<p>or<\/p>\n<div>\n<div class=\"smallfont\">Code:<\/div>\n<pre class=\"alt2\" dir=\"ltr\">15 2 * * 1 \/usr\/local\/bin\/php \/home\/youraccount\/fullbackup.php<\/pre>\n<\/div>\n<p>(Runs every Sunday night at 2:15 a.m.)<\/p>\n<p><b><a href=\"http:\/\/underhost.com\/backup.php\">Just contact us if you want to orders a secured backup space into a backup servers.<\/a><\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Automatic cPanel backup (domain &amp; MySQL) with cron &amp; PHP The situation is this: You use cPanel on our web hosting server. You use the cPanel backup tool to regularly backup \/home directory (includes my web files, mail, etc), and my MySQL databases. You love the fact that you can use it to backup to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":302,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-3536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-controlpanel","category-how-to"],"_links":{"self":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/comments?post=3536"}],"version-history":[{"count":8,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3536\/revisions"}],"predecessor-version":[{"id":3544,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3536\/revisions\/3544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/media\/302"}],"wp:attachment":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/media?parent=3536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/categories?post=3536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/tags?post=3536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}