How To Make A Free Website Backup PHP & MySql

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.

a1backup.zip

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.

How To Make A Free Website Backup PHP & MySql was last modified: January 31st, 2022 by Maximus Mccullough

How To Make A Free Website Backup PHP & MySql

Pages: 1 2 Next

13 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *