Backup script cron job

I wan to set a backup script as a cron job in order to frequently backup my database.
I use this script that i found here:

backup_script.sh

#!/bin/bash
DIR=`date +%d-%m-%y`
DEST=~/db_backups/$DIR
mkdir $DEST

mysqldump -h "mysql_hostname" -u "mysql_user"  -p "mysql_password" "mysql_database_name" > dbbackup.sql

changed:

mysql_hostname
mysql_user
mysql_password
mysql_database_name

changed the rights of the backup_script.sh to 750 (execution rights)
and created a cron job (pick 2 minutes, left hours empty).

Till now, even though the database is small i dont see any backup.

Hello

Our cron jobs tool allows you to call a URL on your website. We don’t support bash files here, so your cron job call will fail.

With cron jobs, you need to use PHP.

5 Likes

Thanks for the info.
I tried this but doesn’t look to work. Is there any limitation?

<?php //i take these info from https://app.infinityfree.net/accounts/myaccount page $result=exec('mysqldump myDBname --host=myDBhost --password=myDBpassword --user=myDBuser --single-transaction > myBackupFile.sql',$output); if(empty($output)){ echo ('everything fine: ' . $output);} else { echo('something is wrong'); }

It prints "everything fine: " without any output, or sql file created. Is the platform capable to create such files?

Yes:

  • We don’t have mysqldump installed.
  • All functions that try to run system commands are disabled on our hosting - you cannot run system commands on our servers. So even if we did have mysqldump installed, you would not be able to use it.

Technically, yes. phpMyAdmin and Adminer can generate SQL dumps without using MySQL command line tools. But as far as I know, they basically reconstruct the SQL dumps themselves, and I don’t think it’s easy to do that yourself.

4 Likes

Thanks for the info.

I created this php file:

<?php
include "db.php";
$tableNames = ['table1', 'table2'];
$backupsFolder = 'backups/';
//https://www.php.net/manual/en/timezones.php
date_default_timezone_set('mytimezone');

foreach($tableNames as $tableName){
    $filenameSQL= $backupsFolder . 'database_backup_' . $tableName . '_'. date("Y-m-d_H:i:s").'.sql';
    $query = "SELECT * FROM " . $tableName;

    $stmt = $conn->prepare($query);
    $backupSuccess = $stmt->execute();
    $tableRows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $backupString = "";
    $numOfLines = 0;
    print_r($tableRows);
    foreach($tableRows as $row){
        $backupString .= implode(',', $row) . "\r\n";
    }
    echo $backupString;
    file_put_contents($filenameSQL, $backupString);
}

If i run

mydomain.rf.gd/my_backup_script.php

from the browser, the backup files are successfully created. But, cron job cannot do that…

Why not? A cron job just calls the URL on your site. If you can do it through the browser, you can do it through a cron job.

If you want, I can check it for you. But to do that, I need actual, real information about where this is running, not just example values and placeholders.

3 Likes

It looks strange to me why this doesn’t work.

Here is my script link where you can check:

https://hfit.rf.gd/backup_script.php

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.