Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Scheduling SQL backups with laravel outside storage path

Hi everybody, I know it's possible to schedule commands to be run with laravel and cron job, my goal is tro create a command to backup my sql databases into my home directory inside my VPS, the example I found uses laravel storage path to drop the backups but I was wondering how can I choose another file outside my laravel app folder (my user home directory for example), is it possible to do?

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class BackupDatabase extends Command
{
    protected $signature = 'db:backup';

    protected $description = 'Backup the database';

    protected $process;

    public function __construct()
    {
        parent::__construct();

        $this->process = new Process(sprintf(
            'mysqldump -u%s -p%s %s > %s',
            config('database.connections.mysql.username'),
            config('database.connections.mysql.password'),
            config('database.connections.mysql.database'),
            storage_path('backups/backup.sql')
        ));
    }

    public function handle()
    {
        try {
            $this->process->mustRun();

            $this->info('The backup has been proceed successfully.');
        } catch (ProcessFailedException $exception) {
            $this->error('The backup process has been failed.');
        }
    }
}
0 likes
4 replies
Snapey's avatar

Don't risk your backups. Use the Spatie backup package.

If you want to use this script, you don't have to use the Storage package you can use regular PHP commands as well, or just put the path directly into the mysqldump command.

But... whats the point of backups that are still on your VPS?

See Freek's cautionary tale when his provider lost the complete disk for his server;

https://murze.be/taking-care-of-backups-with-laravel

1 like
Gabotronix's avatar

@SNAPEY - afaik Spatie's package can only sore inside laravel's folder structure so they are STILL in my VPS, ideally I'd like to dowload this to my google drive but it seems like a pain to set up.

Snapey's avatar

My backups all go to Amazon S3.

I have S3 listed as a disk in my filesystems/php file

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

and then the backup config just says

        'destination' => [

            /*
             * The filename prefix used for the backup zip file.
             */
            'filename_prefix' => '',

            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                's3',
            ],
        ],

I wrote this up ages ago (wow, December 2015) and it goes through setting up S3 as well as laravel-backup although this is for a much older version of the package

http://novate.co.uk/backing-up-a-laravel-site-to-amazon-s3-with-laravel-backup/

1 like

Please or to participate in this conversation.