I would like to write a cake shell to do a nightly backup of my database using mysqldump. I could do this as a shell script, but if I can cram it into a CakePHP shell, then I will get the added benefit of it working across both the development and live server, if I can get it to automagically read my database config settings. I will cron the cake shell and have some peace-of-mind knowing that I have frequent backups of my DB.
In my shell I'm trying to build up a string which starts with "mysqldump --user=" and I'd like to get the username from app/config/database.php. How can I get at the data in database.php?
The CakePHP console provides a framework for creating shell scripts. The Console uses a dispatcher-type setup to load a shell or task, and provide its parameters. Note. A command-line (CLI) build of PHP must be available on the system if you plan to use the Console.
First open app/Config/database. php file in any code editor. The default database is public $default , if you want to use other database(db2, db3) you need to initialize new database in your Model using $useDbConfig predefined cakephp method see example.
If your shell class is in the right place, then it might be a problem that cake does not know where your app root is. You can specify this using the -app argument. Show activity on this post. Your cron command basically calls cd into the app directory and the cake command to run the shell together.
In cake 2.1 the format has changed to:
App::uses('ConnectionManager', 'Model');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With