Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Farmacija on February 16, 2007, 12:24:07 PM

Title: Script for automatic back up of database for cron jobs?
Post by: Farmacija on February 16, 2007, 12:24:07 PM
does anybody have such a script which is propriate for backing up smf database in exactly period of time?
smf 1.1.2
linux server  
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on February 16, 2007, 10:39:08 PM
After the backup is done, what should it do with the backup? Do you want it emailed to you, stored in your home directory, uploaded to an external server, or something else?
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on February 16, 2007, 10:46:09 PM
i didnt see similar topic in croatin support board.
http://www.simplemachines.org/community/index.php?topic=137171.0
this would help
Quote from: Bernard T. on December 29, 2006, 12:29:16 PM
http://www.2serveu.net/cpanel-tutorials/standardcron.html
daniel, i want to store it in my home directory
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on February 17, 2007, 12:26:11 AM
Quotedaniel, i want to store it in my home directory
OK, first, create a new database username and password (in the MySQL section of cPanel, under "Current Users:"). Then, under "Add Users To Your Databases:", add the user to your database (for security reasons, only give it the "SELECT" permission).

Then, in the Cron section of cPanel, create a cron job with the following command:
mysqldump -ucpuser_username -ppassword cpuser_database > /home/cpuser/backup.sql

cpuser_username: Database username (the one you created earlier)
cpuser_password: Database password
cpuser: cPanel username
backup.sql: File to store backup in
As an example:
mysqldump -udaniel15_backup -pOIHsa3dfa23SDGa daniel15_test > /home/daniel15/backup.sql

If you want to compress the backup using gzip, try a command line like this:
mysqldump -ucpuser_username -ppassword cpuser_database | gzip > /home/cpuser/backup.sql.gz

I haven't tried this, but it should work :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Mayhem30 on February 21, 2007, 05:21:51 PM
... or you can just download AutoMySQLBackup script and add that to cron.

http://sourceforge.net/project/showfiles.php?group_id=101066

A script to take daily, weekly and monthly backups of your MySQL databases using mysqldump. Features - Backup mutiple databases - Single backup file or to a seperate file for each DB - Compress backup files - Backup remote servers - E-mail logs - More..

I use it and it works great!  :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on February 22, 2007, 03:30:10 AM
how i can add it to cron?
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 12, 2007, 11:25:46 AM
i've got this error

mysqldump: Got error: 1045: Access denied for user 'xxxxxx'@'localhost' (using password: YES) when trying to connect
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 13, 2007, 02:18:05 AM
Quote from: Farmacija on April 12, 2007, 11:25:46 AM
i've got this error

mysqldump: Got error: 1045: Access denied for user 'xxxxxx'@'localhost' (using password: YES) when trying to connect
That means the the username and password you used was incorrect. Double-check the settings you specified.
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 16, 2007, 05:50:41 AM
Quotemysqldump: Got error: 1044: Access denied for user 'farmaceu_blabla'@'localhost' to database 'farmaceu_wrdp2' when using LOCK TABLES
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 16, 2007, 06:32:00 AM
Make sure that user has full permissions on that database. You'll need to grant "ALL" permissions when adding the user to the database.
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 16, 2007, 06:51:52 AM
you said above  that i should give it just select permission!!!!
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 16, 2007, 07:25:23 AM
Sorry about that... I thought SELECT would cover everything, but it looks like it tries to LOCK the tables. SELECT worked for me using MySQL 4.1, but I guess it doesn't work like this for all versions.
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 17, 2007, 08:30:41 AM
No one is perfect :)
and where is saved that back up? i can't find it in home directory.........
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 18, 2007, 03:24:44 AM
The > /home/daniel15/backup.sql at the end tells it where to write the file to. I assume you changed this to suit your setup? You'll need to look in your home directory; it won't be in the "public_html" directory...
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 22, 2007, 01:41:53 PM
it works daniel, thanx. :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 24, 2007, 01:00:36 PM
how we can do this for several databases on 1 server?
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 25, 2007, 01:31:31 AM
I haven't tried this, but just came up with a script that should do this properly. First, make a file called "database-backup.php" in your home directory, and put something like this in it:

#!/usr/bin/php -q
<?php
// Where do we want to store the backups?
$output '/home/username/backups';

// Some MySQL stuff
$host 'localhost';
$username 'mysql_username';
$password 'mysql_password';

$databases = array(
'database1',
'database2',
// ... add more if you need to, like this:
// 'database name',
);

foreach (
$databases as $database)
{
exec('mysqldump -u' $username ' -p' $password ' ' $database ' | gzip > ' $output '/' $database '.sql.gz');
}
?>



Create a "backups" directory to store the backups (make sure $output in the script above is correct!), and then change the cron job to something like:
/home/username/database-backup.php

Hope this works for you :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: SleePy on April 25, 2007, 07:14:21 PM
I use one similar to this


<?php
// Ok.. Get our vars in here.
$db['host'] = 'localhost';
$db['user'] = 'root';
$db['pass'] = 'access';
$db['goto'] = '[email protected]';
$db['from'] = '[email protected]';
$db['date'] = date("Y-m-d");
$db['temp'] = '/home/cpaneluser/tmp';

// What to call this sucker.
$mailfile 'FULL-backup-'.$db['date'].'.tar.gz';

// MySQL we need ya..
$link mysql_connect($db['host'], $db['user'], $db['pass']);
if(!
$link)
die ('Unable to connect to mysql server.. Could not Create Database Backup.');

// Result it
$result mysql_query('show databases') or die('Couldn\'t Show DB:' mysql_error());

// For each database put it into a nice array for use.
while($row mysql_fetch_array($result))
$databases[] = $row['Database'];

// Foreach of the databases we dump it
foreach($databases AS $dbs)
passthru("mysqldump -u " $db['user'] . " --password=" $db['pass'] . " " $dbs " > " $db['temp'] . '/' $dbs ".sql");

// Now we compress the whole temp folder.. wow...
@passthru('tar -czf '.$mailfile.' '.$db['temp'].'  -p > /dev/null 2>&1');

// Deletion time..
foreach($databases AS $dbs)
if(file_exists($db['temp'].'/'.$dbs.'.sql'))
unlink($db['temp'].'/'.$dbs.'.sql');

// The Fun crap.. Mail preperation..
$attachmentname array_pop(explode("/"$mailfile));   // If a path was included, strip it out for the attachment name
$message "Compressed database backup file $attachmentname attached.";
$mime_boundary "<<<:" md5(time());
$data chunk_split(base64_encode(implode(""file($mailfile))));
$headers "From: ".$db['from']."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";
$content "This is a multi-part message in MIME format.\r\n\r\n";
$content .= "--".$mime_boundary."\r\n";
$content .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content .= $message."\r\n";
$content .= "--".$mime_boundary."\r\n";
$content .= "Content-Disposition: attachment;\r\n";
$content .= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content .= "Content-Transfer-Encoding: base64\r\n\r\n";
$content .= $data."\r\n";
$content .= "--" $mime_boundary "\r\n";

// Now we finally mail it..
mail($db['goto'], 'MySQL backup file - ALL'$content$headers'-f'.$db['from']);

// Delete the big backup we just made..
unlink($mailfile);
?>
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 26, 2007, 11:15:16 AM
hm, which command then i should put in cron jobs
this mysqldump -ucpuser_username -ppassword  cpuser_database > /home/cpuser/backup.sql ?
Title: Re: Script for automatic back up of database for cron jobs?
Post by: SleePy on April 26, 2007, 05:25:18 PM
You could do that if you want to do it for one database or one for each database.
If you are using the php script you do

php -q /home/cpuser/path/to/phpscript.php
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 27, 2007, 02:20:09 AM
SleePy, I suggest you add a shebang line to the top of the file...
Above <?php, put:

#!/usr/bin/php -q


Then, just use the file path (without the php -q) in the cron job :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 27, 2007, 03:15:10 AM
you confusing me, which code i should use in cron jobs when i use daniel15's script for more then one databases? :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 27, 2007, 04:01:36 AM
If you use my script above, first edit the script to reflect the correct values (username, password, databases), and then change the cron job to something like /home/cpuser/database-backup.php :).
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 27, 2007, 04:18:52 AM
i don't need to put this mysqldump -ucpuser_username -ppassword  cpuser_database > before /home/cpuser/database-backup.php?


I get this error => /bin/sh: /home/farmaceu/database-backup.php: Permission denied

edit2=> now it's ok, it was needed to chmod file database-backup.php to 744.
thank u very much daniel!!!  :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Daniel15 on April 27, 2007, 05:41:55 AM
No problem ;)

Quote from: Farmacija on April 27, 2007, 04:18:52 AM
i don't need to put this mysqldump -ucpuser_username -ppassword  cpuser_database > before /home/cpuser/database-backup.php?
No, you didn't need to do that, as the PHP script does the mysqldump call itself :)
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on April 27, 2007, 05:51:44 AM
yes, i figure it out :)

--
But, now i have this error
Failed loading /root/milan/ioncube/ioncube_loader_lin_4.4.so:  /root/milan/ioncube/ioncube_loader_lin_4.4.so: cannot open shared object file: Permission denied
Title: Re: Script for automatic back up of database for cron jobs?
Post by: Farmacija on May 05, 2009, 12:03:51 PM
Me again.

Now i have problem because back up file is huge, in a last time it was about 8gb???


Does someone know where is the problem?