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
Related
after a week of trying i dont know what else to do.
I have a simple php script that is on my webserver called getpoem.php
The script opens up a website pulls its content and saves it to a poem.txt on the server
<?php
//File to extract the Poem of the day
$homepage = file_get_contents('http://SomeWebsite.com/today.php');
$poemALL = substr($homepage,strpos($homepage,"<p>"),strlen($homepage));
.
. // extracting the poem and saving it to $poemFinish
.
file_put_contents("poem.txt", $poemFinish); ?>
so this is a fairly simple script (it works fine if i manually execute it). This script should be executed with the www-data user with its cron so i opend up cron with this command and entered the command it should run
sudo crontab -u www-data -e
0 3 * * * php /var/www/html/getpoem.php
to avoid any permission problems i gave the getpoem.php and poem.txt rwx rights like this (i know i should change it when its live but this is just to test)
-rwxrwxrwx 1 www-data www-data 1189 Aug 17 15:07 getpoem.php
-rwxrwxrwx 1 www-data www-data 1335 Aug 17 15:07 poem.txt
So this is the setup, but it will not execute.
What i did so far changing the "php" to /usr/bin/php to secure that cron know what php is.
Next thing i did was making shure that cron was running so i changed the cronjob to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /var/www/html/test.txt
wich again did nothing .... so i changed it to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /tmp/test.txt
that did not run the php file but created a empty (-.-") file called test.txt in my tmp dir.
So i think the problem must be someware in the acces rights of my www-data user. It's just very wierd because all my webcontent (php files, webapp usw.) are also owned by the www-data user and they run smoothly.
Do i need to grand extra privileges for the cron of www-data ?
When running php from cron, your working directory usually isn't the directory the file is located in. When writing a file, it might try to write the file poem.txt relative to / which usually isn't writable.
So you either set the working directory or you should use "absolute" paths. So for example:
file_put_contents(__DIR__.'/poem.txt');
where __DIR__ is a magic constant that contains the directory where the current file is in.
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
This is my very first time running a cron job on Elastic Beanstalk (EB). After deploying my code, it seems the cron job is created and running but the PHP script is not executing correctly. Here's my set-up.
In my .ebextensions folder I have a file called 01run.config.
container_commands:
01_remove_old_cron_jobs:
command: "crontab -r || exit 0"
02_cronjobs:
command: "cat .ebextensions/cron_jobs.txt > /etc/cron.d/cron_job && chmod 644 /etc/cron.d/cron_job"
leader_only: true
In my .ebextensions folder I also have a cron_jobs.txt file. Please note that I have an line break at the end of this file as instructed by another stackoverflow post. In my example below I am running the command as ec2-user but I also tried root.
* * * * * ec2-user /usr/bin/php -q /var/app/current/tests/cron.php
After deploying my code, I can see that the file /etc/cron.d/cron_job has been created. I can also see the cron job running every minute when I run sudo tail /var/log/cron.
[ec2-user#ip-xxx-xxx-xxx-xxx ~]$ sudo tail /var/log/cron
Apr 13 12:54:53 ip-xxx-xxx-xxx-xxx crontab[26093]: (root) DELETE (root)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx crond[1230]: (*system*) RELOAD (/etc/cron.d/cron_job)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx CROND[26128]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Apr 13 12:56:01 ip-xxx-xxx-xxx-xxx CROND[26139]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Within /var/app/current/tests/cron.php I have some code that adds a row to a MySQL database (hosted on RDS). But nothing is being added to the database.
I then tried running the cron command directly through my terminal window:
$ /usr/bin/php -q /var/app/current/tests/cron.php
And it runs without error and adds the record to the database. I am logged in as ec2-user in terminal.
Have I missed something? Or is my cron job code set-up incorrectly?
I had a similar problem with a php script that was trying to access an AWS RDS database. Is your php script getting the database details with $_SERVER['RDS_xxxx']? If so, those RDS_xxxx variables don't exist in the environment when the php script is run by cron.
In order to fix this, I added the variables to the beginning of the cron file:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * php /path/to/my/script.php
Login via SSH and check if generated cron job file/etc/cron.d/cron_job have unix line ending i.e. ASCII text not win i.e. ASCII text, with CRLF line terminators.
To check the line ending refer the answer here.
Note: If you have windows line ending then you will have to convert the line ending of file .ebextensions/cron_jobs.txt, for that you can use dos2unix or similar program.
I had a similar problem with my RDS_ variables on AWS, I followed this discussion and it works.
This was my cronjob before:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And changed to this:
* * * * * . /opt/elasticbeanstalk/support/envvars cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And now I can access them like: $_SERVER['RDS_HOSTNAME']
I want to edit the crontab of root from the web interface. I have Apache and PHP installed on my Ubuntu. If there is any permissions required please also mention that.
I want to edit the crontab of root. I have some jobs running in that crontab. One of my projects wants me to change the running jobs Time from web interface.
So please tell me how can I access the crontab of root and edit it.
permission problems: solve those by allowing www-data to run the appropriate commands via sudo.
run crontab -u root -l to get the current crontab, edit it with php string manipulation code of your liking and intall it with crontab -u root $FILE. i recommend to place markers in the file to make it easier to find the correct places to edit like so:
# Edit this file to introduce tasks to be run by cron.
# ...
# m h dom mon dow command
0 14 27 * * backupmails-monthly.sh
0 14 * * 5 backupmails-weekly.sh
# MARKER_EDIT_HERE_START
0 14 27 * * job_to_edit.sh
# MARKER_EDIT_HERE_END
a better solution if jobs run regularly: don't put them in the crontab but in /etc/cron/cron.{hourly,daily}.
if you have access to the console, you should install webmin for web management interface of the server
http://www.webmin.com/
These days there are tools like
chronos (depends on apache, has a dockerfile)
luigi from spotify
minicron
More options found at the github topic: cron
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