How to reduce the risk of your Drupal 8 application using Backup & Restore?
Blog

How to reduce the risk of your Drupal 8 application using Backup & Restore?

Imagine this! You are typing hundred lines of code, fixing bugs after bugs and finally after countless efforts the program successfully runs. Uff!! What a feel when you finally crack the code! But wait, what if in a blink of an eye everything vanishes off? What if, your site which was running properly suddenly stumbles a minute later?
Isn’t it any coder’s nightmare? You curse yourself for not taking backup. This article will save you from that dreadful situation.
 
Not only is creating and storing site backup a good practise, but it also ensures site stability and helps in further updation.
 
In order to backup a Drupal site you need to take care of code and database.

  • The first problem we came across is placing the Drupal directory trees under revision control.
  • The second one is about database which requires a more careful analysis.

     

Before we learn about how to use Backup & Migrate module along with Drush, let us cover some of the optimal strategies for backing up and restoring MySQL database.

General practices:
 

  1. Always backup the entire site before updating or upgrading.
     
  2. Date your backups. Save each file with a title that includes the date of the backup.
     
  3. Save a copy of backup in a different location other than your web server. Remember, if your web server crashes, you may lose your backup files too.
     
  4. Inquire about your ISP or web host's backup policies. Most good web hosts have a backup plan that kicks in every 24 hours.
     
  5. In addition to your web host's backups, routinely do it yourself. Periodically run a separate backup monthly, weekly, daily or whatever fits your site's needs.
     
  6. Restore your backups using the same method as you took the backup.
     

Why should I backup?
 

  1. If for some reason your website goes awry, sometimes restoring it from a recent backup is the only option available.
     
  2. New development environments can be created easily.
     
  3. Module upgradation and patches can be tested better when using a recent backup of your production site.

Tools available for Backup & Restore
If you are on Linux you can do Mysql Database Backup / Restore using below mysql and mysqldump command lines


Backup Local Database without password
mysqldump db_name > db_file.sql
 
Backup / Local / Remote Database With Password
mysqldump -u USERNAME -p dico db_name > db_file.sql
 
Backup A Table
mysqldump -uUSERNAME -p db_name tab_name > tab_file.sql
 
Backup and compress using gzip
mysqldump -uUSERNAME -p db_name  | gzip > db_file.sql.gz
 
Restore a Database
mysql -uUSERNAME -pPASSWORD db_name < db_name.sql
 
Restore a gzip compressed file using gunzip

gunzip < db_file.sql.gz | mysql -uUSERNAME -pPASSWORD db_name


Backup & Migrate:
This module facilitates emergency recovery and site migration. We can configure it for automatic backups saved to the file system - with more frequent backups during development. We can also create a manual backup. That way, you have a "restore point" in case of disaster.
 
While there may be some issues of security when you save the database and content as a file, the benefits of having a rollback in case of disaster are significant.
 
Drush
Drush is a command line shell and UNIX scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes sql queries and DB migrations, and misc utilities like run cron or clear cache.
 
To use Drush you need to have shell (SSH) terminal access to your server and be able to install and configure the Drush utility code.

Step 1: Drupal has known problems in clearing some cache tables, that are left unaffected by the normal cache clearing in Drupal or Drush. Solve this by clearing expired cache elements manually by running Drush.
 
The most convenient way to rebuild Drupal's cache is by using Drush
RUN drush cache-rebuild
alias: drush cr
alias: drush rebuild

cache_clear


Step 2: Once you cleared the cache, the next step is running the backup command.
RUN: drush archive-dump

archive_dump



So, we can view our site dump is being downloaded to
/home/valuebound/drush-backups/archive-dump/20160217071827/drupal8.20160217_071828.tar.gz
This .tar file carry site & database dump.
 
If you would like to take just the sql dump,
RUN: drush sql-dump > ~/
This will create .sql file in your home directory.

sql_dump

 

Step 3: To Restore sql dump.
 
RUN: drush sql-cli < db_name.sql
Open a SQL command-line interface using Drupal's credentials.

data_restore


FTP
The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files between a client and server on a computer network.

FTP is built on client-server model architecture and uses separate control and data connections between the client and the server.

FTP

Where section a is local Directory
            section b is currently uploaded file location folder.
            section c is remote server folder list.
            section d is remote server file listing.
 
By pressing right key we can easily get the copy / download option for the particular folder.
 
Step 1:
Backup all the files and folders inside your Drupal directory. You can do that by downloading them via your favourite FTP client.
 
Step 2: Backup/Export your database. Then, you have to export your Drupal database. You can do that by using PHPMyAdmin.
 
Please, make sure that you choose the correct database, which your Drupal application uses. If you are unsure about the Drupal database name, you will be able to find it in the settings.php file.
 
Now you can sit back and relax as you have successfully backed up your data and you can restore it any time you want!

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch