cron job for backup the database in linux/php - 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 ,

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

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

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

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

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

Related

PHP - MySQL - Backup database daily

I have developed an agenda which works (server-side) with PHP and MySQL. The last thing I need to do is to make an authomatic daily backup of the database.
This is what I have thought may be the easiest: Write a php script which once every 24hs saves a file(Whose name would be a timestamp) in certain folder in the server. Combining this with time machine or some other backup software to backup this folder into an external disk should be enough.
So now, the questions are: How do I make a php script to run automatically once per day? How do I save a file with sql backup from a php script?(Similar to phpMyAdmin export as sql)
Thanks!
If you are running apache server:
sudo nano /etc/crontab
Add below line:
0 0 * * * root mysqldump -u root -proot db_name > /home/username/db_backup/$( date +"\%Y_\%m_\%d" ).sql
0 0 * * * => every 24 hours
If your server is a linux, you can make a cron using this script : cronsql.sh .
#!/bin/bash
path=/home/backup/
projet=(databasename1 databasename2)
now=$(date +"%Y-%m-%d")
for arg in ${projet[*]}
do
if [ ! -d $path$arg ]; then
mkdir $path$arg
fi
cd $path$arg
mkdir dump$now
cd dump$now
mysqldump -dBR --triggers -p"dbpassword" $arg > structure.sql
mysqldump --no-create-info -p"dbpassword" $arg > data.sql
done
It ll generate a folder with database name.
Inside, a folder with date
Inside the folder, a structure.sql file and a data.sql file
>databasename1
---->20151009
---->data.sql
---->structure.sql
---->20151008
---->data.sql
---->structure.sql
---->20151007
---->data.sql
---->structure.sql
for the cron
use crontab -e
then add 59 23 * * * /home/backup/cronsql.sh if cronsql.sh is in /home/backup/
And last but not least,
you can add in crontab
01 01 * * * find /home/backup/ -mindepth 2 -ctime +30 -exec rm -fr {} +
It ll delete every file / folder older than 30 days.
So you have a backup for every last 30days.
And no need to clean it manually
I found out, that you can do it with a *bat, if you are running windows.
This will safe your databases as zip, you also can upload them into ftp by changing this script a little bit.
Follow this link:
http://www.redolive.com/utah-web-designers-blog/automated-mysql-backup-for-windows/
It's easy to do, if you have any questions just ask

how to run a php script daily with the cron job on Ubuntu os

command to runI am using ubuntu 12 and lamp server . I want to run a php script after every 1 hour . i have create a crontab to execute this and if i check my cron list with command crontab -l it is showing like this
# Edit this file to introduce tasks to be run by cron.
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
this is my php script
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
but it is not executing
how can i check why it is not working , please help
You can use crontab to add/remove/edit cronjobs.
Hit Alt+Ctrl+T to open terminal.
First make sure the script is executable by running:
chmod +x YOURSCRIPT
Then run the following command to add your cronjob:
crontab -e
Add your cronjob like this:
0 * * * * /usr/local/bin/php path/of/php/file
That's it!
Your can check the current user's crontab entries by running:
crontab -l
For more information about crontab run:
crontab --help
OR
man crontab
To find out what is wrong with your cron you can type the following command in your terminal:
grep -i "cron1.php" /var/log/syslog
The syslog contains all log of crons.
Try run the code /usr/bin/php5 -q /var/www/cronjobs/cron1.php on terminal to check if there are errors.
You can also redirect all errors to a file:
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 2> /tmp/errorCron1.txt
Make Script File /etc/scripts/cron.sh with contain
cd /var/www/
/usr/bin/php cron.php
Save it then
chmod +x cron.sh
then go on /etc/crontab
15 15 * * * root /etc/scripts/cron.sh
save it
and wait
This is the description of crontab arguments
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
To Run your script every hour use below crontab entry.
0 */1 * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
This way your script will start executing every hour.

Run PHP script with CRON

I try to run PHP scripts on my debian server, everyday at midnight & 1 minute.
So I created the crontab file in admin, with
crontab -e
who seems to be in
/tmp/crontab.ky3Q3F/crontab
And I put the line
01 00 * * * /usr/bin/php5 /var/www/MySite/cronTB.php
Bellow the comments.
The goal is to run cronTB.php (with PHP) everyday at 0h01.
Is my syntax correct ?
Is the correct file to edit, to the correct path ?
Is the CRON daemon still runing after I run it with:
/etc/init.d/cron start
even if I close the ssh connexion ?
Thanks for help

PHP cron job from MySQL query

Thanks for all the help in advance!
I'm just starting with PHP and currently have to work with existing code; which is over my head.
I have a file filled with functions, which includes this MySQL query:
function search_late_orders(){
$this->assert_connected();
$this->assert_table_exists(REGION_PREFIX."Order");
$date = date('Y-m-d H:i:s');
$sql = "UPDATE `".REGION_PREFIX."Order`
SET `".REGION_PREFIX."Order`.`orderfulfilled_ts` = `".REGION_PREFIX."Order`.`orderread_ts`
WHERE `".REGION_PREFIX."Order`.`orderfulfilled_ts` = '0000-00-00 00:00:00'";
}
What i'm attempting to accomplish is to reiterate this function every 90 minutes from a different file. The only caveat is that my DB is roughly 60K rows and growing rapidly. What's the best way to accomplish something like this with an eye on performance.
0 0,3,6,9,12,15,18,21 * * * /usr/bin/php -q /yourpath/search_late_orders.php
you can use crontab to run this task every 90 min
first install cronie.i dont knw which linux flavour u are using.i am using centos
yum install cronie
as a root.if u know the risk of being a root.if not do it as sudo user
first add apache user
useradd apache-Apache
type which php to see path
/usr/bin/php
give necessary permission and ownership
chown apache /path/to/file
chmod 755 /path/to/file
type crontab -l to list cronjobs for that user.
now crontab -e to edit or create new jobs as it will open vi editor.
* * * * * php /location/of/yourfile/scriptto call
add your time in place of star to match your requirement

Schedule Automatic Server Maintenance [Ubuntu]

I'm running an Ubuntu 12.10 server as a LAMP stack. I access this through a SSH terminal (as I don't have access to the physical box).
My question is, is there a way to setup ubuntu to automatically execute a script at chosen times. Like to schedule automatic maintenance scripts that have been built in PHP or really any language (possibly Bash Scripts?). I'm sure this has to be possible.
Since I am a bit newer to linux/ubuntu, any help would be appreciated.
EDIT:
This is one solution I found if anybody else stumbles across this...
sudo crontab -e
...
#daily /usr/bin/wget -q -O /var/log/maintenence.txt /var/www/admin/script.php
You can use the crontab in linux.. That wil suit your purpose.
Cron is a daemon that executes scheduled commands
eg:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /usr/bin/find
Okay for example.. if you need to run the script file /home/krizna/backup.sh every sunday 12 PM .. just issue the below command
sudo crontab -e
and add this line at the end of the file
# Minute Hour Day of Month Month Day of Week Command
00 12 * * 7 /bin/sh /home/krizna/backup.sh

Categories