SitePoint Sponsor |
|
User Tag List
Results 26 to 50 of 81
Thread: MySQL periodic backup with PHP
-
Aug 12, 2003, 14:50 #26
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
what is the way to make this lovely script make a new folder every time it is run, this folder would be called like 2003-08-12
?
how could we do this ??Last edited by RedRose; Aug 12, 2003 at 15:40.
Linux, Apache V 1.3.28 (Unix)bind (9.2.1),
Perl V 5.006001, PHP V 4.3.2, Mysql V 4.0.13
-
Aug 13, 2003, 01:53 #27
- Join Date
- Nov 2002
- Location
- North West, UK
- Posts
- 545
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi RedRose
Well nobody's laughed at me yet and I've asked stupider questions than yours
1: Should you call this script from your browser?
You can if you wish. It will work fine. But as I'm lazy and prone to forget to do stuff like database backups I set up a cronjob (it's like a schedule manager in windows) on the server to run the script at predefined intervals.
Check with your host if you can do cron jobs on your site - it will make life a lot easier for you.
2: Is it safe to have this script in the public_html folder?
Put it in a password protected directory and you should be fine. But if you use the cron job method (recommended) you can place the file outside of the public folder - meaning its more secure again.
3: Is it for security reasons that you are not including the username and password of the database in the same file?
Yes. Ideally, you should put your database connection info above the public folder and use an include to get the info. It makes your scripts a little more secure. (highly recommended)
Hope that helps but post back if you need any more info and, if I can't help, I'm sure someone with a lot more experience will help you out.
-
Aug 13, 2003, 02:12 #28
- Join Date
- Aug 2003
- Location
- Hamburg
- Posts
- 36
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by RedRose
Originally Posted by RedRose
Originally Posted by RedRose
-
Aug 13, 2003, 02:40 #29
- Join Date
- Nov 2002
- Location
- North West, UK
- Posts
- 545
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by RedRose
$date = date("dmy" );
Then you could amend the path to save the database to something like
// Database Backup Filename & Location
$backupto = '/home/sitename/$date'; // absolute path to folder containing database - no trailing slash.
Alternatively, you could still put the date line in and amend the $backupas = $database . '.sql' . '.gz'; line to:
$backupas = $database . $date . '.sql' . '.gz';
this will save all the backups in the same directory but add the date to file name eg. DATABASE2003-13-08.sql.gzip
Good luck
-
Aug 13, 2003, 02:50 #30
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by elgumbo
Originally Posted by elgumbo
I have either to use a rm –rf command before the dump command, or I will have to make sure that the cron job does not run in less than 24 hours.
In the first case, you are taking high risks, for if your board is hacked just before the dump, your "security" backup would be your killer, as it would delete your most important backup and write over it.
I wish there were a way to run this every 24 hours and 1 minute…. In this way, the timing of the cron job would not move in a few days to the server's busy hours, and at the same time it would not have to delete your file before the dump.
The problem is that cron jobs as I know are programmed on a fixed daily time, not every "this much" of hours and minutes.
Any work around? I think we need a few lines of php code here to set this running every 24 hour and 1 minute issue.
Originally Posted by elgumbo
(I think, and I might be wrong, that calling scripts from browsers can ONLY happen if your script is in the public_html folder. Please correct me if I am wrong. )
It goes without saying that I am using a .htaccess and .htpasswd.
Originally Posted by elgumbo
Originally Posted by elgumbo
Originally Posted by elgumbo
-
Aug 13, 2003, 02:57 #31
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lacerus
Originally Posted by lacerus
Originally Posted by lacerus
Originally Posted by lacerus
what could be more secure than using a .htacess and a .htpasswd (this last in the .htpasswds behind the public_html folder) ???
In addition, if I have a index.html file in every folder, how could anyone see what other folders are there, and what other files are there in this or that folder??
Originally Posted by lacerus
ty for this info…. ;-)
-
Aug 13, 2003, 03:00 #32
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by elgumbo
it is a small world !!!
I did try this yesterday and no, it did not work....
I even tried to make the folder with a separate command, and double check the chmod 777 of this command.... BEFORE the dumping process, and still the dumping process would not understand the $date and it dumped in the parent folder of the $date folder.
:-) it is a small world.... two guys on two sides of the earth thinking of the same solution to one problem.... ;-)
let us try something else.
-
Aug 13, 2003, 03:06 #33
- Join Date
- Nov 2002
- Location
- North West, UK
- Posts
- 545
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Global Village
RedRose,
The second method (with the date apended to the filename) is the one I use and it works fine.
Cron jobs can be set up for any time period you wish, eg you could specify it to run your script every minute of every third sunday in september if you wish. I have mine runnning at 03.50 every 14 days.
Do a search on crojobs and you'll find a lot of info.
-
Aug 13, 2003, 03:37 #34
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
you are right elgumbo ....
thank you very much ...
long time I have not used cron jobs, and thus i forgot how to
i was scared of cron jobs, because my host kept moving servers all the time and every time he moved, i lost all of my cron jobs....
and as I was a newbie, I had many separate commands for every little job.... and thus re-entering those lines was soooo time-needy.. (talking about many sites here)
I thus kind of gave up on cron jobs....
now we are back..
is this the proper way to request a cron job for my script every 24 hours and 1 minute??? or would it do the job at midnight and 1 minute everyday??
see the attached gif...
-
Aug 13, 2003, 03:38 #35
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
vb3 .... where is the link to my attachment ??
-
Aug 13, 2003, 18:34 #36
- Join Date
- Sep 2001
- Location
- Panama
- Posts
- 2,181
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by RedRose
Community Guidelines | Community FAQ
"He that is kind is free, though he is a slave;
he that is evil is a slave, though he be a king." - St. Augustine
-
Aug 13, 2003, 20:07 #37
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Toly
thank you Toly , but that is NOT what I want to do
I would like this script to run every 24 hours and 1 minute.
how can you do that ?Linux, Apache V 1.3.28 (Unix)bind (9.2.1),
Perl V 5.006001, PHP V 4.3.2, Mysql V 4.0.13
-
Aug 14, 2003, 04:19 #38
- Join Date
- Aug 2003
- Location
- Hamburg
- Posts
- 36
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I would like this script to run every 24 hours and 1 minute.
what could be more secure than using a .htacess and a .htpasswd (this last in the .htpasswds behind the public_html folder) ???
-
Aug 14, 2003, 05:35 #39
- Join Date
- Sep 2001
- Location
- Panama
- Posts
- 2,181
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by RedRose
Community Guidelines | Community FAQ
"He that is kind is free, though he is a slave;
he that is evil is a slave, though he be a king." - St. Augustine
-
Aug 14, 2003, 17:31 #40
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lacerus
Well, how can you then define a random parameter in php?
I will give a random string to the end of my files, and this way, I am sure, that backing up my bases would not mean kill the previous one if it was made the same day.
Originally Posted by lacerus
Originally Posted by lacerus
databasename_date-of-today.gzip
however, if I run this backup with a cron, it would simply make a new file every day at a fixed hour.
Manually running the script would kill my first backup and replace it with the new one…. Which is not my purpose…
If I can make a random short string at the end of the file name, I would be fine ;-)
Originally Posted by lacerus
I sincerely appreciate this.
That DOES answer my question. Thank you lacerus.
Herzlichen Dank…. Na ja, ich wohne im Schweiz und ich kan ein bisschen Deutsch.
-
Aug 14, 2003, 17:32 #41
- Join Date
- Jul 2003
- Location
- A Nice place on earth..
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Toly
you are right Toly.
Thank you so much for your very appreciated help
-
Oct 27, 2003, 08:15 #42
Hey I know this is an old thread but I was trying this script out and am having a problem setting it up.
This is the script as I have it on my server now:
Code:<?php // Include your user and password $user $pass // outside the public folder include("/home/USER/domains/mysql_backup/user.php" ); // Mysql Database Information: $database = 'My_database'; // name of the database. $username = $user; // username with access to database. $password = $pass; // password for username. $date = date("dmy" ); // Database Backup Filename & Location $backupto = '/home/USER/domains/mysql_backup/backups'; // absolute path to folder containing database - no trailing slash. $backupas = $database . $date . '.sql' . '.gz'; // Perform backup $backupcommand = "mysqldump --opt -u$username -p$password $database | gzip >$backupto/$backupas"; passthru ("$backupcommand", $error); if($error) { echo ("Problem: $error\n" ); exit; } ?>
Code:<?php $user='My_username'; $pass='My_pass'; ?>
My problem is the resulting back_up file is virtually empty?
This is what I get:
Code:-- MySQL dump 9.07 -- -- Host: localhost Database: My_database --------------------------------------------------------- -- Server version 4.0.12-log
What am I doing wrong?
Also is there anyway to make this script e-mail and/or FTP the file to me? So that my back_up will not be on the same server just in case the server goes completly down.
After I get the script working correctly I will move it to a directory above my web tree and set it up as a cron job.
Any help will be appreciated!
Thanx
TWLast edited by TW.; Oct 27, 2003 at 21:31.
-
Oct 27, 2003, 14:03 #43
I edited the above post to take out my passwords and usernames!
-
Oct 27, 2003, 14:23 #44
- Join Date
- Sep 2001
- Location
- Panama
- Posts
- 2,181
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Usually when you get a blank backup file like that, it means that the username or password wasn't accepted. Verify that your username and password are correct and use double quotes.
PHP Code:$user = "username";
$pass = "password";
Edit:
Also is there anyway to make this script e-mail and/or FTP the file to me? So that my back_up will not be on the same server just in case the server goes completly down.
Community Guidelines | Community FAQ
"He that is kind is free, though he is a slave;
he that is evil is a slave, though he be a king." - St. Augustine
-
Oct 27, 2003, 14:58 #45
- Join Date
- Oct 2003
- Location
- Germany
- Posts
- 2,195
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
An email wouldn't be that great since those db backups can get big. If you have a FTP server available then you can use PHP's ftp functions.
-
Oct 28, 2003, 07:14 #46
Originally Posted by Toly
Like I stated above I have tried my individual usename and pass and also my root usename and pass and it always comes out the same.
I am going to try it with the usename and pass in the 1st file instead of calling it from user.php just to check, will let you know what I get.
TW
-
Oct 28, 2003, 07:40 #47
I know your already working on some code. But here is a script which can email or FTP the backup.
http://www.hotscripts.com/Detailed/27576.html
-
Oct 28, 2003, 07:55 #48
OK I have tried double quotes "user" and single quotes 'user' with the same results and with spaces or without with no difference!
I have put my "user" and "pass" in original file and comment out the include line and the "$username = $user;" and "$password = $pass;" lines and all does the same.
The only change I can get is if I intentionally put wrong user or pass I get a totally empty file instead of this:Code:-- MySQL dump 9.07 -- -- Host: localhost Database: My_database --------------------------------------------------------- -- Server version 4.0.12-log
marcel thanx for the link I will try that one out too only thing I can lose is time and if I try enough I am sure I will come out on top with a working solution.
TW
-
Oct 28, 2003, 08:10 #49
- Join Date
- Sep 2001
- Location
- Panama
- Posts
- 2,181
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Although the --opt method is used to optimize the backup process, try removing it from this line:
PHP Code:$backupcommand = "mysqldump --opt -u$username -p$password $database | gzip >$backupto/$backupas";
Community Guidelines | Community FAQ
"He that is kind is free, though he is a slave;
he that is evil is a slave, though he be a king." - St. Augustine
-
Oct 28, 2003, 08:43 #50
Originally Posted by Toly
That gives me a nice 3.91mb gziped file with all my create and insert commands when done on my forum database.
Ok now I am off to see about adding ftp funtions to it and then I am done!
I will be moving this script to above my web folder after I get ftp working and was wondering if it would be safe to just add my usename and pass to file instead of calling user.php for these variables since it will no longer be accessable from a browser?
I will also want to .htaccess it and would like to know if crontrab will be able to bypass the .htaccess?
I want to thank you again for holding my hand though this process!
TW
Bookmarks