Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, February 9, 2016

cron job for backup the database in linux/php

cron job for backup the database in linux/php


Am new to linux cron job, i am using mysql DB, my database name finaldb, i want to take this database every one hour,

I have folder called dailbackup, in this i have folder by date wise,in this each folder i have backup mysql db file

name like final_db_9.sql (this backup taken at morning 9 am), final_db_13.sql(this backup taken at noon 1pm, like that ,

this process at present am doing manually , is it possible to make it automation , any ideas, suggestions ,

Answer by MatthieuP for cron job for backup the database in linux/php


Untested 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 are done

Answer by ajreal for cron job for backup the database in linux/php


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 is allow you to backup database every hour and using the %H as sql file name

Answer by Lucanos for cron job for backup the database in linux/php


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.'"' );  

Answer by Arunmu for cron job for backup the database in linux/php


Yes ofcourse, you can do it as long as your mysql server is up and listening :). You will need to make a shell or perl script and use edit the crond using the below command (in Fedora):

  crontab -e  

Components of your cron job is ::

1) Path to your script(executable)

2) Minutes(00-59)

3) Hours (00 - 23)

4) Month (01-12)

5) Day (01-31)

6) Day of the week (00 -06 with 00 as Sunday)

Example :: You wat to run test_pl script every day at 1200

entry in crontab will be ::

00 12 * * * /home/jok/test_pl

Answer by Zecc for cron job for backup the database in linux/php


Create somewhere a script 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 setup this script to run as an hourly cronjob:

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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.