Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Artisan for backup-manager Auto naming convenience

Introduction

backup-manager is an excelent package, but lack of auto naming feature, here is a workaround using Laravel's Artisan command line tool.

Creating the command

This is the command what we are going to accomplish:

php artisan db:cloudbackup

1. Generate a command file

php artisan command:make DatabaseBackupCommand

2. Activate it

File app/start/artisan.php add the following line in the end of the file:

Artisan::add(new DatabaseBackupCommand);

3. Command logic

Edit app/commands/DatabaseBackupCommand.php, Code is following:

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class DatabaseBackupCommand extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'db:cloudbackup';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Auto backup database to the cloud with auto naming.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        $now      = Carbon::now();
        $folder   = $now->format('Y-m') . '/';
        $database = Config::get('database.connections.mysql.database');
        $subfix   = '_' . $now->toDateTimeString() . '.sql';

        $filename = $folder . $database . $subfix;
        
        // database, destination, destinationPath, compression
        $this->call('db:backup', [
            '--database' => 'mysql',
            '--destination' => 'dropbox',
            '--destinationPath' => $filename,
            '--compression' => 'gzip',
        ]);
	}
}

Cron auto backup

1. create a bash script

vi /usr/sbin/dbcloudbackup

Add the following content:

#!/bin/sh

cd /path/to/your/project/root
php artisan db:cloudbackup

Add execute permission:

$ chmod +x /usr/sbin/dbcloudbackup

Give it a test

$ dbcloudbackup

See the result, in my case Dropbox:

2. add Cron

Edit crontab:

crontab -e 

Add the following line, run the script every day at 3:30:

30 3 * * * /usr/sbin/dbcloudbackup
@mrgodhani

This comment has been minimized.

Copy link

commented Jan 2, 2015

You can also use https://github.com/Indatus/dispatcher. To skip the step of adding cron or bash script.

@summerblue

This comment has been minimized.

Copy link
Owner Author

commented Jan 2, 2015

WOW Thanks @mrgodhani 😄

@marc-coll

This comment has been minimized.

Copy link

commented Feb 23, 2015

Thanks! Really, really useful!

Note: if you are using Laravel Forge, you only have to add
"php /home/forge/sitename/artisan db:cloudbackup" in the scheduler of the server and chose when you want to perform the backup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.