This post covers how to make a free website backup. Once finished you will be able to download a complete website backup zip file. This will also include your database. This task is very easy but I am going to explain the process in detail.
Update: Backed by popular demand I was asked to do an updated script to this one. This script works with older versions of PHP. Go to page #2 to see my updated script on making a free website backup in PHP and MySQL.
Backup Website PHP & MySql Step #1
When you first started your website you should of been given a hosting control panel. Sometimes this is cPanel, Paraless Pleask, ISP Config or something similar. You want to create a FTP account.
Backup Website PHP & MySql Step #2
Upload the following script to your root directory. This will be something like public_html or var/www.
If you want to just do the database use this file. Enter your username, database name, password and whatever you want to name your .SQL file. You can name it whatever you want.
Once you enter the information and hit submit it will create an SQL file on your server. You can then download it or transfer it to another server. Make sure you delete the SQL file when you are done.
Optional Step #2
Once you set up your FTP connect to your server and create a file called backup.php in your root directory. Copy and paste the code below and save the file to your server.
<?php ini_set("max_execution_time", 0); $dir = "a1-backup"; if(!(file_exists($dir))) { mkdir($dir, 0777); } $host = "localhost"; //host name $username = "root"; //username $password = ""; // your password $dbname = "wp_test"; // database name $zip = new ZipArchive(); backup_tables($host, $username, $password, $dbname); function backup_tables($host,$user,$pass,$name,$tables = '*') { $con = mysql_connect($host,$user,$pass); mysql_select_db($name,$con); if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } $return = ""; foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "nn".$row2[1].";nn"; while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = preg_replace("#n#","n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");n"; } $return.="nnn"; } //save file $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+'); fwrite($handle,$return); fclose($handle); } if (glob("*.sql") != false) { $filecount = count(glob("*.sql")); $arr_file = glob("*.sql"); for($j=0;$j<$filecount;$j++) { $res = $zip->open($arr_file[$j].".zip", ZipArchive::CREATE); if ($res === TRUE) { $zip->addFile($arr_file[$j]); $zip->close(); unlink($arr_file[$j]); } } } $path = dirname($_SERVER['PHP_SELF']); $position = strrpos($path,'/') + 1; $folder_name = substr($path,$position); $zipname = date('Y/m/d'); $str = "backup-".$zipname.".zip"; $str = str_replace("/", "-", $str); if ($zip->open($str, ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../$folder_name/")); foreach ($iterator as $key=>$value) { if( strstr(realpath($key), "backup") == FALSE) { $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); } } $zip->close(); echo "Archive created successfully."; if(glob("*.sql.zip") != false) { $filecount = count(glob("*.sql.zip")); $arr_file = glob("*.sql.zip"); for($j=0;$j<$filecount;$j++) { unlink($arr_file[$j]); } } if(glob("*.zip") != false) { $arr_zip = glob("*.zip"); } foreach ($arr_zip as $key => $value) { if (strstr($value, "backup")) { $delete_zip[] = $value; copy("$value", "$dir/$value"); } } for ($i=0; $i < count($delete_zip); $i++) { unlink($delete_zip[$i]); } ?>
Backup Website PHP & MySql Step #3
Change the values of the file to match your database connection. Towards the top of the file you will see this. Make sure you change it accordingly. If you are using WordPress you can get this information from your wp-config.php file.
$host = "localhost"; //host name $username = "root"; //username $password = "password"; // your password $dbname = "wp_test"; // database name
Backup Website PHP & MySql Step #4 Run & Download Backup
Once you have everything ready to go visit your website at http://yourwebsite.com/backup.php. This may take a few minutes so let the process complete. Once completed you can download a copy of your website backup at http://yourwebsite.com/a1-backup/backup-YYYY-MM-DD.zip {you would replace YYYY-MM-DD with the actual number year, month and day example: http://yourwebsite.com/a1-backup/backup-2017-02-22}
Notice: there is a new an improved script on the next page.
17 Comments
Script not working – get error message. It creates folder then error. can you please fix as it seems and excellent script
I have it on my schedule this week to check out the issue.
Hi Maximus,
Have you had a opportunity to look at bug
HI Hany, I looked at it and I am trying to work out a solution. I will notify you when I get it done. Just had a lot on my plate lately but since I know your waiting on it Ill make it a priority.
Was the bug ever fixed? And btw good tutorial.
I have it on my schedule, just been very busy.
me2 eagarly waiting
Hi there! Any new on this topic?
Here you go! ๐ https://a1websitepro.com/make-free-website-backup-php-mysql/2/
I updated the script. You guys can check it out here. https://a1websitepro.com/make-free-website-backup-php-mysql/2/ Thanks!
Hello Where is the script ? Only video is there for now ?
Here is the script. https://a1websitepro.com/make-free-website-backup-php-mysql/2/
Bug is Fixed ๐
Does the updated script where you ask for a donation update both the database and file folder associated with the database? If so, how do I get the $5 donation to you.
Sorry for the late response. I have been working with other projects. I am going to look at your question more carfully.
Just downloaded the updated script and it is working fine on one of my domains running php 7.4.x but have the following error message on a domain running php 5.6.39…yes, I know I need to upgrade but at the moment I’m stuck with it. What needs to be changed to fix the errors.
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in /public_html/backup.php on line 162
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in /public_html/backup.php on line 167
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in /public_html/backup.php on line 169
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in /public_html/backup.php on line 171
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in /public_html/backup.php on line 173
Notice: Use of undefined constant PHP_OS_FAMILY – assumed ‘PHP_OS_FAMILY’ in l/public_html/backup.php on line 175
The error messages you’re seeing are related to the use of the constant PHP_OS_FAMILY, which was introduced in PHP 7.2. Therefore, you can’t use it in PHP 5.6.39.
To fix the error, you can replace all instances of PHP_OS_FAMILY with the following code:
This code defines the constant PHP_OS_FAMILY if it doesn’t exist and sets it to the appropriate value based on the current operating system.
You should replace all instances of PHP_OS_FAMILY with the above code, and the error should be resolved.
However, keep in mind that PHP 5.6.39 is no longer supported, and you should consider upgrading to a supported version of PHP as soon as possible to avoid security risks and to take advantage of the latest features and improvements.