Automatic Backup Website In PHP

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
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.
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.


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.

@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;
return false;
$dir = '/DirectoyBackup/';
$destination = '/BackupDestination/';
$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.

Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy