Created
February 5, 2018 16:58
-
-
Save dchatry/1b0673306fe65a3251db75d759f0a272 to your computer and use it in GitHub Desktop.
Duplicate a Drupal instance for dev/test purposes (files + database)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Backup/duplicate a Drupal website and its database. | |
| # Usage: sh backup.sh | |
| # Prompt for website to backup. | |
| read -p "Website directory to backup (/var/www/html/drupal): " WEBSITE_DIRECTORY | |
| if [ -z "$WEBSITE_DIRECTORY" ]; then | |
| WEBSITE_DIRECTORY="/var/www/html/drupal" | |
| fi | |
| # Check if website is a Drupal installation. | |
| if [ ! -f "$WEBSITE_DIRECTORY/sites/default/settings.php" ]; then | |
| tput setaf 1; echo "Couldn't find Drupal installation, please check path."; tput sgr0; | |
| exit 1 | |
| fi | |
| read -p "Backup directory (${WEBSITE_DIRECTORY}_dev): " BACKUP_DIRECTORY | |
| if [ -z "$BACKUP_DIRECTORY" ]; then | |
| BACKUP_DIRECTORY="${WEBSITE_DIRECTORY}_dev" | |
| fi | |
| # Create temporary settings file to get database credentials from settings.php. | |
| printf '<?php include("settings.php");echo "DATABASE=".$databases["default"]["default"]["database"].";";echo "USERNAME=".$databases["default"]["default"]["username"].";";echo "PASSWORD=".$databases["default"]["default"]["password"].";";echo "HOST=".$databases["default"]["default"]["host"].";";' > "$WEBSITE_DIRECTORY/sites/default/settings.tmp.php" | |
| eval `php "$WEBSITE_DIRECTORY/sites/default/settings.tmp.php"` | |
| rm "$WEBSITE_DIRECTORY/sites/default/settings.tmp.php" | |
| # Error getting database credentials. | |
| if [ -z "$DATABASE" ]; then | |
| tput setaf 3; echo "Couldn't get database credentials."; tput sgr0; | |
| # Ask user for credentials. | |
| read -p "Database host (localhost): " HOST | |
| if [ -z "$HOST" ]; then | |
| HOST="localhost" | |
| fi | |
| read -p "Database user (postgres): " USERNAME | |
| if [ -z "$USERNAME" ]; then | |
| USER="postgres" | |
| fi | |
| read -sp "Database password (postgres): " PASSWORD | |
| if [ -z "$PASSWORD" ]; then | |
| PASSWORD="postgres" | |
| fi | |
| read -p "Database name (drupal): " DATABASE | |
| if [ -z "$DATABASE" ]; then | |
| DATABASE="drupal" | |
| fi | |
| fi | |
| # Backup website directory into target directory. | |
| if [ -d "$BACKUP_DIRECTORY" ] && find "$BACKUP_DIRECTORY" -mindepth 1 -print -quit | grep -q .; then | |
| tput setaf 1; echo "Target directory is not empty, files were not copied."; tput sgr0; | |
| exit 1 | |
| else | |
| read -p "Backup database name, will be created if not exists (${DATABASE}_dev): " TARGET_DATABASE | |
| if [ -z "$TARGET_DATABASE" ]; then | |
| TARGET_DATABASE="${DATABASE}_dev" | |
| fi | |
| tput smso; echo "Copying website files..."; tput sgr0; | |
| cp -r "$WEBSITE_DIRECTORY/." "$BACKUP_DIRECTORY" | |
| tput setaf 2; echo "Website files copied successfully!"; tput sgr0; | |
| tput smso; echo "Updating file permissions"; tput sgr0; | |
| chown -R apache:apache "$BACKUP_DIRECTORY" | |
| tput setaf 2; echo "OK"; tput sgr0; | |
| tput smso; echo "Updating settings.php"; tput sgr0; | |
| sed -i -e "s/'database' => '$DATABASE'/'database' => '$TARGET_DATABASE'/g" "$BACKUP_DIRECTORY/sites/default/settings.php" | |
| tput setaf 2; echo "OK"; tput sgr0; | |
| tput smso; echo "Updating robots.txt"; tput sgr0; | |
| printf "User-agent: *\nDisallow: /" > "$BACKUP_DIRECTORY/robots.txt" | |
| tput setaf 2; echo "OK"; tput sgr0; | |
| fi | |
| # Database duplication. | |
| if [ $HOST = "localhost" ] || [ $HOST = "127.0.0.1" ] ; then | |
| tput smso; echo "Database backup..."; tput sgr0; | |
| psql -U $USERNAME -c 'CREATE DATABASE $TARGET_DATABASE' && pg_dump -U $USERNAME $DATABASE | psql -U $USERNAME $TARGET_DATABASE && exit 1 | |
| tput setaf 2; echo "OK"; tput sgr0; | |
| else | |
| # SSH through remote database. | |
| tput smso; echo "SSH connection initialization..."; tput sgr0; | |
| read -p "User for ${HOST} connection (root): " SSH_USER | |
| if [ -z "$SSH_USER" ]; then | |
| SSH_USER="root" | |
| fi | |
| tput smso; echo "Database backup..."; tput sgr0; | |
| ssh "$SSH_USER"@"$HOST" "psql -U $USERNAME -c 'CREATE DATABASE $TARGET_DATABASE' && pg_dump -U $USERNAME $DATABASE | psql -U $USERNAME $TARGET_DATABASE && exit 1" | |
| tput setaf 2; echo "OK"; tput sgr0; | |
| fi | |
| tput setaf 2; echo "Everything went smoothly, you can check out your new website!"; tput sgr0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment