How to back up your daily logs to Dropbox

Written on 29 May 2013, 02:02pm

Tagged with: , , ,

Here is the complete process of backing up your daily WHM backup file(s) to Dropbox.

Pre-requisites
a. Enable backups in WHM: Home/Backup/Configure Backup/Backup Status:Enabled
b. In case you don’t already have one yet, create a Dropbox account (referral link)

dropbox

Once you have these pre-requisites, you can perform the following operations:
1. Create a Dropbox app dropbox.com/developers/apps
2. Download the Core APIs SDK. I downloaded the PHP flavour and put it in /home/user/dropbox/php-sdk-1.0.1
3. Make sure that the php_curl extension is enabled
4. Create /home/user/dropbox/php-sdk-1.0.1/backup – which will hold the necessary files
5. In the above directory, create a file called login.json with the following content:

{
  "key": "INSERT_APP_KEY",
  "secret": "INSERT_SECRET",
  "access_type": "FullDropbox" or "AppFolder"
}

6. Copy the files ‘authorize.php‘, ‘helper.php‘, ‘upload-file.php‘ from examples/ to backup/ directory
7. Authorize your Dropbox app:

php authorize.php login.json auth.json

This will read the credentials in login.json and attempt to create auth.json file. It will prompt you to open a Dropbox URL where you will authorize your Dropbox app to use your Dropbox account. After you allow, go back to shell, press enter and the auth.json file will be created
8. You’re ready to go. Assuming that WHM creates your daily backups in /backup/cpbackup/daily/ folder, you can run the command below to transfer your tar.gz backup file to Dropbox.

php /home/user/dropbox/php-sdk-1.0.1/backup/upload-file.php /home/user/dropbox/php-sdk-1.0.1/backup/auth.json /backup/cpbackup/daily/user.tar.gz /user.`date +\%Y-\%m-\%d`.tar.gz > /home/user/dropbox/logs/user.`date +\%Y-\%m-\%d`.log

This command will save the output of the operation to a log file for further reference.

9. Email [optional] You can change the command above to send the output by email:

{command above} && mail -s 'Daily backup' 'yourATemail.com' < /home/user/dropbox/logs/user.`date +\%Y-\%m-\%d`.log

10. Cron Put the command at step 8 (or 9) above in a cron: crontab -l or edit the file /var/spool/cron/root and make the changes directly.
Check the new cron jobs: crontab -l
Check the cron log: tail -f /var/log/cron

Cronjobs in CakePHP 2.*.* in 5 steps

Written on 30 August 2012, 07:34pm

Tagged with: , ,

Just adapting a solution from 2006 to the 2012 version of CakePHP 🙂
Calling controller actions from cron and the command line

Step 1: copy app/webroot/index.php to app/cron.php

Step 2: edit app/cron.php
Change the last 3 lines of code as follows:

//---------THESE LINES:
	App::uses('Dispatcher', 'Routing');

	$Dispatcher = new Dispatcher();
	$Dispatcher->dispatch(new CakeRequest(), 
		new CakeResponse(array('charset' => Configure::read('App.encoding'))));

//-----------CHANGE TO:
	App::uses('Dispatcher', 'Routing');

	define('CRON_DISPATCHER',true); 

	if($argc == 2) { 
		$Dispatcher = new Dispatcher();
		$Dispatcher->dispatch(new CakeRequest($argv[1]), 
			new CakeResponse(array('charset' => Configure::read('App.encoding'))));
	}

Step 3: create Controller/CronController.php

 class CronController extends AppController {

	public function beforeFilter() {
	    parent::beforeFilter();
	    $this->layout=null;
	}

	public function test() {
		// Check the action is being invoked by the cron dispatcher 
		if (!defined('CRON_DISPATCHER')) { $this->redirect('/'); exit(); } 

		//no view
		$this->autoRender = false;

		//do stuff...

		return;
	}
}

Step 4: Run your cron using the command line:

# php ./app/cron.php /cron/test

Step 5: Test that loading the same script in browser is not allowed (for security reasons):
http://yourdomain/cron/test redirects to http://yourdomain/