DEV Community

adithyasrinivasan
adithyasrinivasan

Posted on • Originally published at adithya.dev on

Automating MySQL backups to S3 using Laravel

Having already setup automatic S3 snapshots for Kibana, the next item on the list is to automate MySQL backups. There are a couple of services like Snapshooter that can do this but I prefer doing it on my own.

To automate MySQL backups to AWS S3 with Laravel, I'm using Laravel Backup package by Spatie.

Setup

  • Install the package using composer require spatie/laravel-backup

If you see a similar error message like this, your PHP version is probably not supported by the package / dependencies. You might want to use composer require spatie/laravel-backup ^6

Argument 2 passed to Symfony\Component\Translation\Translator::addResource() must be an instance of Symfony\Component\Translation\mixed, array given, called in /var/www/html/vendor/nesbot/carbon/src/Carbon/AbstractTranslator.php on line 165
Enter fullscreen mode Exit fullscreen mode
  • To publish the configuration file, run php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" - this will create a file under config called backup.php
  • Go to your AWS Console, create a new IAM User (you can see the steps linked on the Kibana guide) and add AmazonS3FullAccess to the permissions attached to the user.
  • Grab the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  • Create a S3 Bucket from AWS console (the bucket name goes under AWS_BUCKET and your default region under AWS_DEFAULT_REGION)
  • On the config\backup.php file, under destination key, change the value of disks array to include s3
'disks' => [
    's3',
],
Enter fullscreen mode Exit fullscreen mode
  • Ensure that the default database is set to mysql
'databases' => [
    'mysql',
],
Enter fullscreen mode Exit fullscreen mode
  • Now if you run the command on your command line php artisan backup:run --only-db --only-to-disk=s3 the process should kick off and you should see similar messages.
Starting backup...
Dumping database database_name...
Determining files to backup...
Zipping 1 files and directories...
Created zip containing 1 files and directories. Size is 900 MB
Copying zip to disk named s3...
Successfully copied zip to disk named s3.
Backup completed!
Enter fullscreen mode Exit fullscreen mode
  • Once this is done, you can setup to run it using the scheduler on a frequency that you prefer. Go to your Console\Kernel.php and add an entry
protected function schedule(Schedule $schedule)
{        
    $schedule->command("backup:run --only-db --only-to-disk=s3")->weeklyOn(6, '7:00');

    //Other commands
}

Enter fullscreen mode Exit fullscreen mode

Error messages

The problem was incorrect permissions assigned to the IAM User

Copying zip failed because: There was an error trying to write to disk named s3.

Copying zip failed because: Error executing "ListObjects" on....
AWS HTTP error: cURL error 6: Could not resolve host:
Enter fullscreen mode Exit fullscreen mode

Discussion (0)