Automatic Backup Website In PHP

  • Author: Sajjad Hossain
  • |
  • Added: May 20, 2016
  • |
  • In: PHP Projects

website backup is most important for every single site. Every ideal website owner backup their website regularly. If you do not make your website backup regularly then you may fall in problem. If your website's server will crash then you will lose your website and will never get it back, If hosting provider will backup your website then you need to pay them for backup data. If your website attacked by Hacker and if they delete your all data then will lose your website. It will be a big amount of harm for your business. If you keep regular backup your website then you could restore your website anytime if your website will get any type of attack by hacked or fall into server problem.

How to Backup a website?
You can backup your website from your website's cPanel. If you use WordPress then you can use Auto backup "BackWPup" plugin for backup your website automatically. If you don't use WordPress then you can make your backup manually from your cPanel. Follow these instructions,

1. Login to your website's cPanel
2. Select "Backups" option
attachmentwwcom3ypng
3. Go to Full Backup and Click "Download a full website backup" for make full backup.
4. If you do not want to full backup then go to Partial Backups and Download a Home Directory Backup and Click on "Download" for Backup your website's home directory.
attachmentkgayb50png
If you click on "Download" Your Backup file will start to download. for database backup you can see your database list under Download a MySQL Database Backup section. Click any database name for Backup then Download will start automatically.

attachmentjxr7gsypng

And you can restore you backup anytime if you want.

How to automatic Backup In PHP?
If you want to backup your website automatic then follow these instructions.
create a file for backup, as an example we created backup.php then write down this code into this file.

<?php
@ini_set('max_execution_time',0);
@ini_set('memory_limit', '-1');
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {return false;}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {return false;}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}

function autobackup_data($dir,$destination)
{
if (!is_dir($destination)){mkdir($destination, 0755);}
$filename = 'backup-'.date('dmy').'-'.date('his').'.zip';
$backup = $destination.$filename;
if (is_dir($dir))
{
$res = Zip($dir,$backup);
}
if ($res)
{
return $filename;
}
else
{
return false;
}
}
$dir = '/DirectoyBackup/';
$destination = '/BackupDestination/';
autobackup_data($dir,$destination);
?>
$dir is backup directory which directory you want to backup and $destination is where you want to save your backupd file. change $dir and $destination value. When you run this script the backup will save as a zip file into your desination directory.
For automatic backup set a new corn job and add this file into cron job. the backup will save automatically.


Top