Skip to content

Instantly share code, notes, and snippets.

@summerblue
Last active April 23, 2017 12:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save summerblue/e34f1c79e100c5deac20 to your computer and use it in GitHub Desktop.
Save summerblue/e34f1c79e100c5deac20 to your computer and use it in GitHub Desktop.
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
Copy link

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

@summerblue
Copy link
Author

WOW Thanks @mrgodhani 😄

@marc-coll
Copy link

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