Advertisement
Guest User

Drupal - Fix File Permissions - Script

a guest
Jul 8th, 2014
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ##
  4. # Based from script found at: https://drupal.org/node/244924
  5. #
  6. # See README or code below for usage
  7. ##
  8.  
  9. # Is this really necessary?
  10. if [ $(id -u) != 0 ]; then
  11. printf "This script must be run as root.\n"
  12.   exit 1
  13. fi
  14.  
  15. # Script arguments
  16. drupal_path=${1%/}
  17. drupal_user=${2}
  18. httpd_group="${3:-www-data}"
  19.  
  20. # Help menu
  21. print_help() {
  22. cat <<-HELP
  23.  
  24. This script is used to fix permissions of a Drupal installation
  25. you need to provide the following arguments:
  26.  
  27. 1) Path to your Drupal installation.
  28. 2) Username of the user that you want to give files/directories ownership.
  29. 3) HTTPD group name (defaults to www-data for Apache).
  30.  
  31. Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER --httpd_group=GROUP
  32.  
  33. Example: (sudo) bash ${0##*/} --drupal_path=/usr/local/apache2/htdocs --drupal_user=john --httpd_group=www-data
  34.  
  35. HELP
  36. exit 0
  37. }
  38.  
  39. # Parse Command Line Arguments
  40. while [ $# -gt 0 ]; do
  41. case "$1" in
  42.     --drupal_path=*)
  43.       drupal_path="${1#*=}"
  44.       ;;
  45.     --drupal_user=*)
  46.       drupal_user="${1#*=}"
  47.       ;;
  48.     --httpd_group=*)
  49.       httpd_group="${1#*=}"
  50.       ;;
  51.     --help) print_help;;
  52.     *)
  53.       printf "Invalid argument, run --help for valid arguments.\n";
  54.       exit 1
  55.   esac
  56. shift
  57. done
  58.  
  59. # Basic check to see if this is a valid Drupal install
  60. if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/modules/system/system.module" ]; then
  61. printf "Please provide a valid Drupal path.\n"
  62.   print_help
  63.   exit 1
  64. fi
  65.  
  66. # Basic check to see if valiud user
  67. if [ -z "${drupal_user}" ] || [ $(id -un ${drupal_user} 2> /dev/null) != "${drupal_user}" ]; then
  68. printf "Please provide a valid user.\n"
  69.   print_help
  70.   exit 1
  71. fi
  72.  
  73. # Start changing permissions
  74. cd $drupal_path
  75. printf "Changing ownership of all contents of \"${drupal_path}\":\n user => \"${drupal_user}\" \t group => \"${httpd_group}\"\n"
  76. chown -R ${drupal_user}:${httpd_group} .
  77.  
  78. printf "Changing permissions of all directories inside \"${drupal_path}\" to \"rwxr-x---\"...\n"
  79. #find . -type d -exec chmod u=rwx,g=rx,o= '{}' \+
  80. find . -type d -not -path "./sites/*/files" -not -path "./sites/*/files/*" -not -name ".git" -exec chmod u=rwx,g=rx,o= '{}' \+
  81.  
  82. printf "Changing permissions of all files inside \"${drupal_path}\" to \"rw-r-----\"...\n"
  83. #find . -type f -exec chmod u=rw,g=r,o= '{}' \+
  84. find . -type f -not -path "./sites/*/settings.php" -not -path "./sites/*/default.settings.php" -not -path "./sites/*/files/*" -not -name ".gitignore" -exec chmod u=rw,g=r,o= '{}' \+
  85.  
  86. printf "Changing permissions of \"files\" directories in \"${drupal_path}/sites\" to \"rwxrwx---\"...\n"
  87. cd ${drupal_path}/sites
  88. find . -type d -name files -exec chmod ug=rwx,o= '{}' \+
  89.  
  90. printf "Changing permissions of settings.php and default.settings.php files inside all directories in \"${drupal_path}/sites\" to \"r--r-----\"...\n"
  91. for x in ./*/settings.php; do
  92. printf "Changing permissions ${x} ...\n"
  93.   find ${x} -type f -exec chmod ug=r,o= '{}' \+
  94. done
  95. for x in ./*/default.settings.php; do
  96. printf "Changing permissions ${x} ...\n"
  97.   find ${x} -type f -exec chmod ug=r,o= '{}' \+
  98. done
  99.  
  100. printf "Changing permissions of all files inside all \"files\" directories in \"${drupal_path}/sites\" to \"rw-rw----\"...\n"
  101. printf "Changing permissions of all directories inside all \"files\" directories in \"${drupal_path}/sites\" to \"rwxrwx---\"...\n"
  102. for x in ./*/files; do
  103. printf "Changing permissions ${x} ...\n"
  104.   find ${x} -type d -not -name ".git" -exec chmod ug=rwx,o= '{}' \+
  105.   find ${x} -type f -not -path "./*/files/.htaccess" -exec chmod ug=rw,o= '{}' \+
  106. done
  107.  
  108. printf "Changing permissions of .htaccess files inside all \"files\" directories in \"${drupal_path}/sites\" to \"rw-r----\"...\n"
  109. for x in ./*/files/.htaccess; do
  110. printf "Changing permissions ${x} ...\n"
  111.   find ${x} -type f -exec chmod u=rw,g=r,o= '{}' \+
  112. done
  113.  
  114. printf "Changing permissions of \".git\" directories and files in \"${drupal_path}\" to \"rwx------\"...\n"
  115. cd ${drupal_path}
  116. chmod -R u=rwx,go= .git
  117. chmod u=rwx,go= .gitignore
  118.  
  119. printf "Changing permissions of various Drupal text files in \"${drupal_path}\" to \"rwx------\"...\n"
  120. cd ${drupal_path}
  121. chmod u=rwx,go= CHANGELOG.txt COPYRIGHT.txt INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.txt LICENSE.txt MAINTAINERS.txt UPGRADE.txt
  122.  
  123. echo "Done setting proper permissions on files and directories"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement