Tag code

MySQL backup script with emailing

A while ago I found a good script for backing up a MySQL database and sending it via email to a recipient here.

The script is cool, but I didn't like its structure and the fact, that you have to add the database values inline and that it can only backup a single database. For this reason I rewrote it a little and you can download it here.

Features:

  • Backup of mutliple databases
  • Sending of backups to multiple users

For every single database a mail is send and there is no file saved on the server. And wow: it's really easy to configure!

How do I use it?

  • Download the current version (0.1)
  • Adjust the backup.php (you're getting help by my wonderful comments)
  • upload everything to a directory of your choice
  • if applicable, create a cronjob to periodically execute the script

And here the backup.php that calls the appropriate classes and executes the backup (also included in the download):

ini_set("error_reporting", E_ALL);
// include the files
require_once "MySQLConfig.php";
require_once "MySQLBackup.php";

// add some databases to backup
// the domain will be appended to the email subject and is also included within the sql file for identification.
$cfgHost0 = new MySQLConfig("username0", "password0", "database_name0", "domain0");
$cfgHost1 = new MySQLConfig("username1", "password1", "database_name1", "domain1");

$backup = new MySQLBackup();
// the path to the directory where this script is resided
$backup->setExecutionPath("/srv/domain/backup/");
// add the database configs to backup
$backup->addDatabaseToBackup($cfgHost0);
$backup->addDatabaseToBackup($cfgHost1);
// the sender of the backup mail
$backup->setSender("admin@yourdomain.com");
// add some people to receive the backup
$backup->addRecipient("john@yourdomain.com");
$backup->addRecipient("frank@yourdomain.com");
// execute the whole thing
$backup->backup();

If there are any problems or suggestions or feature wishes, please comment this post - thanks!

PS: to create a cronjob log into your server using SSH, then execute crontab -e to edit the crontab and insert for example

0 2 * * 0,3 wget http://yourdomain.com/backup/backup.php -nc -q -O /dev/null

for an execution on sunday and wednesday at 2 am. Save and close the whole thing with :wq and that's it :-)

FE-Plugin with multilingual TYPO3

I always forget how to force a frontend plugin to display the texts localized through $this->pi_getLL($key).

This is how it works:

class tx_extkey_piX extends tslib_pibase {
   function main() {
      $this->sys_language_uid = (int) t3lib_div::_GP('L');
      ...
   }
}

Function/Class '...' was not prepended with 'user_'

I'm currently configuring one of my extension for realurl within Typo3. My aim is to have the categories and subcategories of an article center within the url. But the functionality of lookUpTable does not suffice as I have to observe the nesting of the categories. So I choose userFunc and enter the values as given in the realurl documentation:

'fixedPostVars' => array(
    'articlecenter' => array(
        'userFunc' => 'EXT:extkey/class.tx_userxyz.php:
            &tx_realurl_userfunctest->main',
    ),
),

then I clear the cache and it happens... nothing. Only a nice error message is presented:

|Function/Class 'E' was not prepended with 'user_'|

Seems that my configured values do not really reach the code they should. And of course that's the way it is. After some searching I detect that my configuration is wrong, because realurl is putting all configured values into a nested array wherein every key-value pair is put in as an array that has a numeric indice in the big array. (I hope you can follow my poor english). But my "userFunc" is not set up this way, its numeric indice is userFunc and the array with the key-value pairs is the string EXT:realurl/class.tx_realurl_userfunctest.php:&tx_realurl_userfunctest->main. As realurl runs through this array using foreach it has only "E" as value (that comes from the "EXT") and furthermore the error is thrown in t3lib_div:callUserFunction. Now the solution is quite easy, simply nest the userFunc within an additional array:

'fixedPostVars' => array(
    'articlecenter' => array(
        array(
            'userFunc' => 'EXT:extkey/class.tx_userxyz.php:
                &tx_realurl_userfunctest->main',
        ),
    ),
),

Now everything is working again. You only need to find it out...