Drupal - Site Backup
Drupal - Site Backup
Section titled “Drupal - Site Backup”Regularly backing up your Drupal site is crucial for disaster recovery, before major updates, or when migrating your site. A complete backup consists of two main parts: the database and the files.
1. Backing Up the Database
Section titled “1. Backing Up the Database”The database stores your site’s content, configuration (in Drupal 8+ much configuration is also exportable to files), user information, and more.
Method A: Using Drush (Recommended for Developers/CLI Users)
Section titled “Method A: Using Drush (Recommended for Developers/CLI Users)”Drush is a command-line shell and Unix scripting interface for Drupal. If you have Drush installed (typically via Composer: composer require drush/drush):
-
Open your terminal or command prompt.
-
Navigate to your Drupal site’s root directory (where your main
index.phpandcomposer.jsonfiles are located). -
Run the command:
drush sql:dump --result-file=backup.sql(orvendor/bin/drush sql:dump --result-file=my_database_backup.sql).
This will create an SQL dump file (e.g., my_database_backup.sql) in your Drupal root directory. You can specify a different path for the output file.
Method B: Using phpMyAdmin (Common on Shared Hosting)
Section titled “Method B: Using phpMyAdmin (Common on Shared Hosting)”-
Log in to your hosting control panel (e.g., cPanel) and open phpMyAdmin.
-
Select your Drupal database from the list on the left sidebar.
-
Click the ‘Export’ tab.
-
For ‘Export method’, ‘Quick’ is usually sufficient. Ensure the ‘Format’ is set to ‘SQL’.
-
Click ‘Go’ (or ‘Export’). The database dump file (.sql) will be downloaded to your computer.
Method C: Using Hosting Provider Tools
Section titled “Method C: Using Hosting Provider Tools”Many hosting providers offer built-in backup tools or services. Check your hosting provider’s documentation for specific instructions.
2. Backing Up Files
Section titled “2. Backing Up Files”This includes your Drupal codebase (core, modules, themes) and user-uploaded files (images, documents).
Method A: Backing Up the Entire Drupal Root Directory
Section titled “Method A: Backing Up the Entire Drupal Root Directory”This is the most comprehensive file backup. You need to copy your entire Drupal project directory.
-
Via SFTP/FTP Client (e.g., FileZilla, Cyberduck, WinSCP): Connect to your server. Navigate to your Drupal root directory (e.g.,
public_html,htdocs,www). Download the entire directory to your local computer. -
Via SSH/Command Line: If you have SSH access, you can create a compressed archive:
cd /path/to/drupal_root_parent_directorytar -czvf drupal_files_backup.tar.gz your_drupal_directory_name/Then download
drupal_files_backup.tar.gz.
Key Directories to Ensure are Backed Up:
Section titled “Key Directories to Ensure are Backed Up:”- The entire Drupal root directory is safest.
- At a minimum, ensure you back up:
-
sites/default/files(or your active site’s files directory, e.g.,sites/yourdomain.com/files): Contains user-uploaded files.
-
themes/: Your custom and contributed themes.
-
modules/: Your custom and contributed modules.
-
composer.jsonandcomposer.lock: Define your project’s dependencies.
-
vendor/: Managed by Composer, but sometimes included in full backups for quick restoration.
-
- Configuration files (if using Configuration Management workflow): Typically stored in a directory like
config/syncorsites/default/config/syncand should be version-controlled with Git.
- Configuration files (if using Configuration Management workflow): Typically stored in a directory like
3. Using Backup Modules (e.g., Backup and Migrate)
Section titled “3. Using Backup Modules (e.g., Backup and Migrate)”The ‘Backup and Migrate’ module (contrib) allows you to perform database and file backups (limited scope for files) directly from the Drupal UI. It can also schedule backups. While convenient, for robust and comprehensive backups, especially of files, CLI methods or hosting provider tools are often preferred.
4. Using Drush for a Full Archive (Code, Files, DB)
Section titled “4. Using Drush for a Full Archive (Code, Files, DB)”Drush can also create a full site archive:
drush archive:dump --destination=/path/to/backup/site-archive.tar.gz
This command bundles the codebase, public files directory, and a database dump into a single archive.
Important Backup Practices
Section titled “Important Backup Practices”- Regularity: Perform backups regularly, especially before updates or major changes.
- Off-site Storage: Store backups in a separate, secure location (e.g., cloud storage, different server, external drive).
- Testing: Periodically test your backups by restoring them to a development or staging environment to ensure they work.
- Automation: Automate your backup process using cron jobs and scripts if possible.