You are here

Code

Refreshing a development site from a staging site etc.,

paulbooker's picture
Submitted by paulbooker on Tue, 06/14/2016 - 15:56
#!/bin/bash
#############################################################################
#   Refresh a development site from a staging site etc., 
#
#   The script performs the following tasks:
#
#   1. Takes a backup of a cache-cleared dump of the destination database.
#   2. Overwrites the destination database with the database from a staging site etc.
#   3. Updates the database schema.
#   4. Imports the latest configuration saved in code.
#   5. Disables modules that shouldn't be enabled during development.
#   6. Enables modules that are helpful for development.
#   7. Clears all caches.
#   8. Overwrites the destination's files directory with the source's.
#   9. Runs cron.
#
#
# Usage:
#   resync-drupal-db-for-dev   []
#
# Parameters:
#   :      The site from which to copy the database. Required.
#   : The site to which the database should be copied. Required.
#   :         A list of space-separated modules to disable. Optional.
#
# Assumptions:
#   * The following settings should be in your settings.local.php:
#       $config['system.logging']['error_level'] = 'verbose';
#       $config['system.performance']['css']['preprocess'] = FALSE;
#       $config['system.performance']['js']['preprocess'] = FALSE;
#       $config['system.file']['path']['temporary'] = '/tmp';
#       $config['maillog.settings']['send'] = FALSE;
#
#############################################################################

# Set command paths.
DRUSH=/usr/local/bin/drush
GZIP=/bin/gzip
CUT=/usr/bin/cut
GREP=/bin/grep
ECHO=/bin/echo
DATE=/bin/date
SUDO=/usr/bin/sudo
CHOWN=/bin/chown
ID=/usr/bin/id
MKDIR=/bin/mkdir

# Make sure that the parameters are specified.
if [ -z "$2" ]; then
  $ECHO "Usage: $0   []"
  exit 1
fi

# Stop executing the script if any command fails.
# See http://stackoverflow.com/a/4346420/442022 for details.
set -e
set -o pipefail

# Set the web user.
if $ID -u "php-fpm" >/dev/null 2>&1
then
  # We've got a PHP-FPM user.
  USER_WEB=php-fpm
elif $ID -u "nginx" >/dev/null 2>&1
then
  # We've got Nginx, but no PHP-FPM user.
  USER_WEB=nginx
elif $ID -u "apache" >/dev/null 2>&1
then
  # We're using Apache on a non-Debian-based system.
  USER_WEB=apache
else
  # We're on Debian/Ubuntu or some descendant.
  USER_WEB=www-data
fi

# Set variables with defaults and arguments.
SOURCE=$1
DESTINATION=$2

# Set some modules to disable.
MODULES_ACQUIA="acquia_spi acquia_agent"
MODULES_ADVAGG="advagg advagg_js_compress advagg_mod advagg_css_compress advagg_css_cdn advagg_js_cdn advagg_bundler"
MODULES_DISABLE="automated_cron backup_migrate performance securepages $MODULES_ACQUIA $MODULES_ADVAGG $3"

$ECHO "Start time: $($DATE +%T)"

$ECHO "Clear the existing DB's cache to save disk space..."
$DRUSH $DESTINATION cache-rebuild

$ECHO "Saving a backup of the existing destination database..."
$DRUSH $DESTINATION sql-dump --create-db | $GZIP > ~/$DESTINATION.dump.mysql.gz

$ECHO "Sync the source site's database to the destination..."
# Always download and use Drush SQL Sync Pipe instead of Drush's standard
# sql-sync command.  It's more efficient, skips cache data by default, and
# actually works on Pantheon sites.
$DRUSH pm-download drush_sql_sync_pipe --destination=$HOME/.drush -y
$DRUSH cache-clear drush
# The "--dump" command would be useful for local caching, but it's currently
# broken.  See https://drupal.org/node/2286697 for details.
$DRUSH sql-sync-pipe --progress --sanitize $SOURCE $DESTINATION

$ECHO "Updating the database schema..."
$DRUSH $DESTINATION updatedb -y

$ECHO "Import the latest configuration saved in code..."
$DRUSH $DESTINATION config-import -y

$ECHO "Disabling modules not meant for development..."
$DRUSH $DESTINATION pm-uninstall -y $MODULES_DISABLE

$ECHO "Enabling extra modules for development..."
$DRUSH $DESTINATION pm-enable -y devel update admin_toolbar views_ui dblog maillog

$ECHO "Clearing the destination site's cache..."
$DRUSH $DESTINATION cache-rebuild

$ECHO "Run any periodic tasks that should be run..."
$DRUSH $DESTINATION cron

$ECHO "Updating the public files directory..."
FILES=$($DRUSH dd $DESTINATION:%files)
# Transfer files ownership to the current user for the rsync.
$SUDO $CHOWN -R $USER $FILES
# Perform the transfer of files excluding private and cache files.
$DRUSH -r $FILES rsync --exclude-paths=private:css:js:advagg_js:advagg_css:styles:ctools:xmlsitemap $SOURCE:%files $FILES
# And then set it back to the Web user.
$SUDO $CHOWN -R $USER_WEB $FILES

$ECHO "All done!"
$ECHO "End time: $($DATE +%T)"

Adapting an existing Drupal theme for mobile devices.

paulbooker's picture
Submitted by paulbooker on Fri, 06/10/2016 - 14:00
@media screen and (max-width: 680px) {
  #head,
  #page,
  #footer-region {
    width: 100%;
  }
  
  #content,
  #sidebar-primary {
    float: none;
    width: 100%;
  }
  
  #sidebar-primary .block {
    float: none;
    width: 100%;
  }
	
  .sidebar-first #content {
    width: 100%;	
  }	 
	
  #triptych-first, #triptych-second {
    padding: 0;
  }
	
  img {
    max-width: 300px;
    height: auto;
  }
  
  #logo  {
    bottom: auto !important;
    position: inherit !important;
    right: auto !important;
  }  	
}
/**
 * Implements hook_page_alter().
 */
function mytheme_page_alter($page) {
  $viewport = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1',
    ),
  );
  drupal_add_html_head($viewport, 'viewport');
}

Pages