Automating MySQL backups to S3 using Laravel
How to automate MySQL backups using Laravel to AWS S3
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 usecomposer 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
- To publish the configuration file, run
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
- this will create a file under config calledbackup.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, underdestination
key, change the value ofdisks
array to includes3
'disks' => [
's3',
],
- Ensure that the default database is set to
mysql
'databases' => [
'mysql',
],
- 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!
- 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
}
StorageClass for S3 in Laravel Backup
On your config\filesystems.php
, under s3
group add an array key under backup_extra_options
called StorageClass
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'backup_extra_options' => [
'StorageClass' => 'Standard-IA' //Set the Storage Class value
]
],
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: