create backup cronjob in cpanel - share-system.com

create backup cronjob in cpanel

first script :

    1. to make backup cronjob using mysqldump console command :

 

script to make backup

 mysqldump -h your_ip_address_or_domain -u database_username -p'yourpassword' database_name 
> /home/stiepanc/public_html/database_backup/db-backup-$(/bin/date +\%d\%m\%Y).sql

  1. cron job to run automatic query to remove 10 days old files :
 /home/stiepanc/public_html/database_backup/db_remove_older_file.php

script to make automatic remove 10 days old file, make this with cron job

 
<?php

$folderName = '/home/stiepanc/public_html/database_backup';
if (file_exists($folderName)) {
    foreach (new DirectoryIterator($folderName) as $fileInfo) {
        if ($fileInfo->isDot()) {
        echo 'nothing to delete ';
        continue;
        }
        if (time() - $fileInfo->getCTime() >= 60 * 60 * 24 * 10) {  // 10 days
            unlink($fileInfo->getRealPath());
            
            echo 'delete process';
        }
    }
    
    
}
?>