How to make a full Drupal 8, 9, 10 site backup using a command line

Standard Drupal8 installation
Standard Drupal8 installation

This backup method is a good choice if your Drupal website is hosted on virtual or dedicated server on Amazon AWS, Digital Ocean, Linode, ... and used Unix based operation system, like Ubuntu, Debian, Fedora, Cent OS, Amazon Linux, ...
 

Prepared Drupal8 backup script, ready to use Drupal8 Backup Script
https://github.com/boiler256/drupal8_backup_script

 

For some users, a command line interface can be hard and uncomfortable, of course, a bit more technical skill is required, but it will reward with speed and flexibility.
Let's list our steps:

  • Create a dump of all files in Drupal8 website folder (drupal core + related files)
  • Create MySQL dump
  • Create a list of useful information about system (PHP version, Apache/Nginx configuration)
  • Combine all dumps into one file
  • Download to a local disk or Upload to remote backup storage(optional)

Warning, Important information
Drupal 8 sites in most cases are depend on Composer application, control files like composer.json and libraries directory 'vendor' are located one level above Drupal 8 public web directory, but the location can vary depending on a setup.
This example is based on standard Drupal 8 installation.

Our setup:
Home www folder: /home/ubuntu/web/
Drupal 8 project root folder: /home/ubuntu/web/drupal8
Drupal 8 public folder: /home/ubuntu/web/drupal8/web
Composer config file location: /home/ubuntu/web/drupal8/composer.json
Composer vendor libraries folder: /home/ubuntu/web/drupal8/vendor

Step 1: Create a dump of all files in Drupal8 website folder (drupal core + related files)
# Go to our web home folder
cd /home/ubuntu/web/

# Archive full Drupal8 project folder
zip -r drupal8-full-backup.zip drupal8
 Full drupal 8 backup first step
Result of a successful execution, new zip file was created "drupal8-full-backup.zip"
Step 2: Create MySQL dump
# Create a database dump
mysqldump --user=newuser --password=newpassword --host=localhost drupal8 > drupal8-sql-dump.sql

# Include mysqldump file to the archive file
zip -ur drupal8-full-backup.zip drupal8-sql-dump.sql

# Delete sql dump file (we have added it to the Drupal8 zip archive file.
rm drupal8-sql-dump.sql
Optional: Move created backup to a remote Amazon S3 storage
# Copy newly created backup to Amazon S3 storage
aws s3 cp drupal8-full-backup.zip s3://awstestaccount/drupal8-backups/

Fallowing best practices, we can move the backup to a second independent storage, in this case, it's Amazon S3 bucket.
To use this option, you need to install and setup Amazon "aws" command line tool.

Optional: Save local storage space
find /home/archive -type f -name "drupal8-full-*" -mtime +15 -delete

Keep required amount of  backups with Find command, as example store locally only for 15 days

Optional: Setup a cron job to run backups
0 0 * * * /home/drupal8_backup.sh

Example of a differential fullbackup every day at 0:00 am

To open the crontab in your default editor:
crontab -e

Conclusion

These simple steps, are excellent way to make a solid backup solution that can be used to quickly restore your Drupal 8 site.