How to Backup Laravel to AWS S3 with Bash
Introduction
Amazon S3 is a cloud-based object storage service provided by Amazon Web Services that provides object storage through a web service interface and API. It allows you to store any amount of data at any time.
Manually backing up a Laravel database and storage from your own server to AWS S3 is a very hard and time-consuming process for any system administrator. SnapShooter provides an easier way to backup your Laravel to Amazon S3 automatically. It supports many cloud storage services, including Amazon AWS, Google Cloud, Vultr, DropBox, Hetzner, UpCloud, and more.
In this guide, we will show you how to backup Laravel to Amazon S3.
Create an S3 Bucket in AWS
First, you will need to create an S3 bucket in AWS to store any object in S3. Follow the below steps to create a bucket on AWS S3:
Step 1 - Login to Amazon Management Console, search for S3 and open the S3 service as shown below.
Step 2 - Click on the Create bucket button. You should see the General configuration screen:
Step 3 - Type your bucket name, select your region, select object ownership, block all public access, and click on the Create bucket button. Once the bucket is created, you should see the following screen:
Create a New IAM User
After creating an S3 bucket, you must create a new IAM user and assign permission to access your S3 bucket. Follow the below steps to create an IAM user:
Step 1 - Log in to AWS Management Console, search for IAM from the top services menu, and click on Users in the left sidebar. You should see the IAM user screen:
Step 2 - Click on the Add users button to add a new IAM user. You should see the following screen:
Step 3 - Type your username, select "Access Type Programmatic access", then click on the Next: Permissions:
Step 4 - Leave all default options and click on the Next: Tags:
Step 5 - Leave all default options and click on the Next: Review:
Step 6 - Review your configuration and click on the Create user button. You should see the AWS access key ID and secret access key on the following screen:
Step 7 - Note down the Access key ID and Secret access key in a safe location. You will need both keys to connect to the AWS S3 in the next step. Now, click on the Close button. You should see the following screen:
Step 8 - Click on your newly created user. You should see the following screen:
Step 9 - Click on Add inline policy. You should see the Create Policy screen:
Step 10 - Choose JSON Editor and add the following contents:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket", "s3:DeleteObject", "s3:GetObject", "s3:PutObject", "s3:PutObjectAcl" ], "Resource": [ "arn:aws:s3:::laravel-backup-s3/*", "arn:aws:s3:::laravel-backup-s3" ] } ]}
Step 11 - Replace the laravel-backup-s3 with the newly created bucket. Then, click on the Review policy button. You should see the following screen:
Step 12 - Provide your policy name and click on the Create policy button to save the policy.
Backup Laravel to AWS S3 Manually
This section will show you how to manually backup Laravel to AWS S3 using the AWS CLI tool.
Install AWS CLI Tool
The AWS CLI is a command-line tool used for managing AWS services via command-line. It helps you control multiple AWS services from the command line and automate them through scripts.
First, download the AWS CLI tool for Linux using the command given below:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Next, unzip the downloaded file and install it using the following command:
unzip awscliv2.zipsudo ./aws/install
After the installation, verify the AWS CLI version with the following command:
aws --version
You should see the AWS CLI version in the following output:
aws-cli/2.6.3 Python/3.9.11 Linux/5.4.0-99-generic exe/x86_64.ubuntu.20 prompt/off
Next, you will need to configure AWS CLI using your AWS credentials. You can configure it using the following command:
aws configure
Provide your AWS access key ID, secret key, and the region as shown below:
AWS Access Key ID [None]: YOUR-AWS-ACCESS-KEYAWS Secret Access Key [None]: YOUR-AWS-SECRET-KEYDefault region name [None]: us-east-1Default output format [None]:
Use AWS CLI Tool to Backup Laravel to Amazon S3
First, log in to the Laravel server and connect to the MySQL shell with the following command:
mysql -u root -p
Once you are logged in, list all available databases using the following command:
mysql> SHOW DATABASES;
You will get the following output:
+--------------------+| Database |+--------------------+| information_schema || laraveldb || mysql || performance_schema |+--------------------+
Next, backup the laraveldb database using the following command:
mysqldump -u root -p laraveldb > laraveldb.sql
After backing up your Laravel database, you will need to copy the database backup file to the AWS S3. You can copy the Laravel backup file named laraveldb.sql to AWS S3 using the following command:
aws s3 cp laraveldb.sql "s3://laravel-backup-s3"
You will get the following output:
upload: ./laraveldb.sql to s3://laravel-backup-s3/laraveldb.sql
Next, copy the Laravel website directory to AWS S3 using the following command:
aws s3 sync /var/www/html/laravel "s3://laravel-backup-s3"
You will get the following output:
upload: ../var/www/html/laravel/vendor/webmozart/assert/LICENSE to s3://laravel-backup-s3/vendor/webmozart/assert/LICENSEupload: ../var/www/html/laravel/vendor/webmozart/assert/.php_cs to s3://laravel-backup-s3/vendor/webmozart/assert/.php_csupload: ../var/www/html/laravel/vendor/webmozart/assert/.github/workflows/ci.yaml to s3://laravel-backup-s3/vendor/webmozart/assert/.github/workflows/ci.yamlupload: ../var/www/html/laravel/vendor/voku/portable-ascii/src/voku/helper/data/x0ff.php to s3://laravel-backup-s3/vendor/voku/portable-ascii/src/voku/helper/data/x0ff.phpupload: ../var/www/html/laravel/vendor/webmozart/assert/.editorconfig to s3://laravel-backup-s3/vendor/webmozart/assert/.editorconfigupload: ../var/www/html/laravel/vendor/webmozart/assert/CHANGELOG.md to s3://laravel-backup-s3/vendor/webmozart/assert/CHANGELOG.mdupload: ../var/www/html/laravel/vendor/webmozart/assert/psalm.xml to s3://laravel-backup-s3/vendor/webmozart/assert/psalm.xmlupload: ../var/www/html/laravel/vendor/webmozart/assert/README.md to s3://laravel-backup-s3/vendor/webmozart/assert/README.mdupload: ../var/www/html/laravel/vendor/webmozart/assert/composer.json to s3://laravel-backup-s3/vendor/webmozart/assert/composer.jsonupload: ../var/www/html/laravel/vendor/webmozart/assert/src/InvalidArgumentException.php to s3://laravel-backup-s3/vendor/webmozart/assert/src/InvalidArgumentException.phpupload: ../var/www/html/laravel/webpack.mix.js to s3://laravel-backup-s3/webpack.mix.jsupload: ../var/www/html/laravel/vendor/webmozart/assert/src/Assert.php to s3://laravel-backup-s3/vendor/webmozart/assert/src/Assert.phpupload: ../var/www/html/laravel/vendor/webmozart/assert/src/Mixin.php to s3://laravel-backup-s3/vendor/webmozart/assert/src/Mixin.php
To list your backup files on the AWS S3 bucket, run the following command:
aws s3 ls "s3://laravel-backup-s3"
You will get the following output:
PRE app/ PRE bootstrap/ PRE config/ PRE database/ PRE public/ PRE resources/ PRE routes/ PRE storage/ PRE tests/ PRE vendor/2022-05-25 10:31:55 258 .editorconfig2022-05-25 10:31:57 967 .env2022-05-25 10:31:57 899 .env.example2022-05-25 10:31:57 152 .gitattributes2022-05-25 10:31:57 207 .gitignore2022-05-25 10:31:57 194 .styleci.yml2022-05-25 10:31:57 3958 README.md2022-05-25 10:31:57 1686 artisan2022-05-25 10:31:57 1745 composer.json2022-05-25 10:31:57 290329 composer.lock2022-05-25 10:31:34 1048576 laraveldb.sql2022-05-25 10:31:57 473 package.json2022-05-25 10:31:57 1202 phpunit.xml2022-05-25 10:31:57 569 server.php2022-05-25 10:33:22 559 webpack.mix.js
You can also verify your Laravel backup file by logging in to the Amazon Web Console, search for S3 and click on your bucket name.
Scheduled Laravel (MySQL) Backups SnapShooter
Backup a Laravel website, database (MySQL) and optionally the file system.
Learn more about Laravel (MySQL) Backups
Get started for freeThank you for helping us improve!