Cron task to backup database in linux / php

I am new to linux cron, I use mysql DB, my database name is finaldb, I want to use this database every hour,

I have a folder called dailbackup, in it I have a folder by date wise, in this folder I have a backup mysql db file

name, for example final_db_9.sql (this backup was made in the morning of 9 a.m.), final_db_13.sql (this backup was made at noon 1pm, for example,

this process is currently being performed manually, is it possible to automate it, any ideas, suggestions,

+4
source share
5 answers

Create a PHP Script containing the following:

$dbFile = 'final_db'.date('H').'.sql.gz'; $dbHost = 'localhost'; // Database Host $dbUser = 'username'; // Database Username $dbPass = 'password'; // Database Password exec( 'mysqldump --host="'.$dbHost.'" --user="'.$dbUser.'" --password="'.$dbPass.'" --add-drop-table "finaldb" | gzip > "'.$dbFile.'"' ); 
+6
source

Create a script somewhere to make your rolling backups like this (untested, but should work):

 #!/bin/bash BKPDIR=dailbackup # You must use absolute path here DB=finaldb USERNAME=myusername PASSWORD=mypassword BKPFILE=${BKPDIR}/`date +%Y-%m-%d`/final_db_`date +%H`.sql # Create backup mysqldump --user=${USERNAME} --password=${PASSWORD} ${DB} | gzip -c > ${BKPFILE} # Remove older backups (> 7 days), # unless you want to run out of drive space find ${BKPDIR} -mtime +7 -print0 | xargs -0 rm -rf 

Then configure this script to execute as an hourly cronjob:

 crontab -e 0 * * * * /absolute-path-to-where-you-saved-the-script 
+4
source
 crontab -e 

putting this:

 the_date='date +%Y%m%d' the_hour='date +%H' 0 * * * * mysqldump OPTIONS > /dailbackup/`$the_date`/final_db_`$the_hour`.sql 

the above cron allows you to backup the database every hour and use %H as the sql file name

+2
source

Unacknowledged one liner:

 mysqldump -u*user* -p*password* -P*dbport* -h localhost finaldb > /.../dailbackup/final_db_$(date +%Y-%m-%d_%H_%M).sql 

just add it to your cron job or wrap it in a script and you're done

+1
source

Yes, of course, you can do this while your mysql server gets up and listens :). You will need to make a shell or perl script and use crond to edit using the following command (in Fedora):

  crontab -e

Components of your cron job:

1) The path to your script (executable file)

2) Protocol (00-59)

3) Hours (00 - 23)

4) Month (01-12)

5) Day (01-31)

6) Day of the week (00 -06 from 00 on Sundays)

Example: you waited to run test_pl script every day at 1200

an entry in crontab will be ::

00 12 * * * / home / jok / test_pl

+1
source

All Articles