How to Backup Laravel to DigitalOcean Spaces with Bash
DigitalOcean Spaces is an object storage service used to store and serve a large amount of data. It provides S3-compatible storage and has a built-in content delivery network (CDN) that minimizes page load times, improves performance, and reduces bandwidth and infrastructure costs. The base rate of a DigitalOcean Spaces subscription is $5.00 per month and gives you the ability to create multiple Spaces.
Setting up an effective backup strategy is one of the most important tasks for any system or database administrator. If you are using a dedicated server for Laravel, then you’ll need to set up backups yourself. Regularly backing up a Laravel website data and database is a very hard and time-consuming process. In this case, you will need to think about a headache-free backup strategy or a backup tool that allows you to automatic backups from their control panel. This is where SnapShooter comes into the picture. SnapShooter provides a web-based backup solution with a user-friendly control panel to manage and automate Laravel backup via a web browser. SnapShooter works with a Laravel server hosted on your own server and Laravel managed in a DigitalOcean Managed databases.
This post will show you how to backup Laravel to DigitalOcean Spaces manually and using SnapShooter.
Create Your DigitalOcean Spaces
Before starting, you will need to create a new space on the DigitalOcean to save your backup files. Follow the below steps to create a new space on the DigitalOcean:
Step 1 - Sign in to your DigitalOcean account and click on the Spaces in the left pane.
Step 2 - Click on the Create => Space button. You should see the following screen:
Step 3 - Select your region, Restrict File Listing, Provide your unique space name and click on Create a Space button. Once the Space is created, you should see the following screen:
Create Your DigitalOcean Credentials
In order to create backups with DigitalOcean spaces, you will need the correct access key and secret key to authenticate with it. Follow the below steps to create a DigitalOcean Credentials:
Step 1 - Log in to your DigitalOcean Spaces account.
Step 2 - Click on the Manage Keys button on the right-hand side of the Spaces screen.
Step 3 - Click on the Generate New Key next to Spaces access keys. Then, provide a name for your key and click the checkmark. A new Key and Secret will be generated as shown below:
Backup Laravel to DigitalOcean Space Manually
In this section, we will show you how to back up a Laravel from your server to the DigitalOcean Space using the S3cmd tool.
Before starting, you will need to install the S3cmd tool on the Laravel.
S3cmd is a free and open-source command-line tool that allows you to upload, download and manage data from your server to DigitalOcean Space and other cloud storage service providers. Follow the below steps to install the S3cmd to your server.
First, install the Python and other dependencies by running the following command:
apt-get install python3 python3-setuptools curl -y
Next, download the latest version of S3cmd using the following command:
curl -LO https://github.com/s3tools/s3cmd/releases/download/v2.2.0/s3cmd-2.2.0.tar.gz
Next, extract the downloaded file with the following command:
tar -xvzf s3cmd-2.2.0.tar.gz
Next, navigate to the extracted directory and install it using the following command:
cd s3cmd-2.2.0python3 setup.py install
Once the S3cmd is installed, verify the S3cmd version using the following command:
You will get the following output:
s3cmd version 2.2.0
Next, you will need to configure the S3cmd using the DigitalOcean Access key and Secret key. You can configure it using the following command:
You will be asked to provide your DigitalOcean Access Key, Secret Key, Region, and Endpoint as shown below:
Enter new values or accept defaults in brackets with Enter.Refer to user manual for detailed description of all options.Access key and Secret key are your identifiers for Amazon S3. Leave them empty for using the env variables.Access Key: Your-Access-KeySecret Key: Your-Secret-KeyDefault Region [US]: Frankfurt 1Use "s3.amazonaws.com" for S3 Endpoint and not modify it to the target Amazon S3.S3 Endpoint [s3.amazonaws.com]: fra1.digitaloceanspaces.comUse "%(bucket)s.s3.amazonaws.com" to the target Amazon S3. "%(bucket)s" and "%(location)s" vars can be usedif the target S3 system supports dns based buckets.DNS-style bucket+hostname:port template for accessing a bucket [%(bucket)s.s3.amazonaws.com]: %(bucket)s.fra1.digitaloceanspaces.comEncryption password is used to protect your files from readingby unauthorized persons while in transfer to S3Encryption password:Path to GPG program:When using secure HTTPS protocol all communication with Amazon S3servers is protected from 3rd party eavesdropping. This method isslower than plain HTTP, and can only be proxied with Python 2.7 or newerUse HTTPS protocol [Yes]:On some networks all internet access must go through a HTTP proxy.Try setting it here if you can't connect to S3 directlyHTTP Proxy server name:New settings:Access Key: KMCAKDPNM5VEIIG2IMIASecret Key: 16YsRq0mb0q6VARA16DA0LSH6A1Cm3PjUHm6GKCagvoDefault Region: Frankfurt 1S3 Endpoint: fra1.digitaloceanspaces.comDNS-style bucket+hostname:port template for accessing a bucket: %(bucket)s.fra1.digitaloceanspaces.comEncryption password:Path to GPG program: NoneUse HTTPS protocol: TrueHTTP Proxy server name:HTTP Proxy server port: 0Test access with supplied credentials? [Y/n] YPlease wait, attempting to list all buckets...Success. Your access key and secret key worked fine :-)Now verifying that encryption works...Not configured. Never mind.Save settings? [y/N] YConfiguration saved to '/root/.s3cfg'
Once the S3cmd is configured, you can verify your bucket information using the following command:
s3cmd info s3://laravel-backup-do/
You will get the following output:
s3://laravel-backup-do/ (bucket):Location: fra1Payer: noneExpiration Rule: nonePolicy: noneCORS: noneACL: 6945323: FULL_CONTROL
Use S3cmd to Backup Laravel to DigitalOcean Spaces
First, log in to your Laravel server and connect to the MariaDB 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 should see your databases in 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
Next, run the S3cmd command to copy the laraveldb.sql file to the DigitalOcean Spaces:
s3cmd put laraveldb.sql s3://laravel-backup-do/
You should see the following output:
upload: 'laraveldb.sql' -> 's3://laravel-backup-do/laraveldb.sql' [1 of 1]1048576 of 1048576 100% in 2s 377.26 KB/s done
Next, run the S3cmd command again to copy the Laravel website data to the DigitalOcean Spaces:
tar -czvf - /var/www/html/laravel | s3cmd put - s3://laravel-backup-do/laravel.tar.gz
upload: '<stdin>' -> 's3://laravel-backup-do/laravel.tar.gz' [part 1 of -, 8MB] [1 of 1]8556627 of 8556627 100% in 2s 3.48 MB/s done
You can now verify all backups on the DigitalOcean Spaces using the following command:
s3cmd ls s3://laravel-backup-do
You will get the following output:
2022-05-25 11:05 8556627 s3://laravel-backup-do/laravel.tar.gz2022-05-25 11:05 1048576 s3://laravel-backup-do/laraveldb.sql
You can also log in to the DigitalOcean Spaces and verify your backup files as shown below:
Scheduled Laravel (MySQL) Backups SnapShooter
Backup a Laravel website, database (MySQL) and optionally the file system.
Learn more about Laravel (MySQL) BackupsGet started for free
Thank you for helping us improve!