Advertisement
Guest User

fix-permissions.sh

a guest
Apr 5th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.01 KB | None | 0 0
  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=/var/www/nach5.dev/public_www
  17. drupal_user=filip
  18. httpd_group=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. while false; do
  42. case "$1" in
  43.     --drupal_path=*)
  44.       drupal_path="${1#*=}"
  45.       ;;
  46.     --drupal_user=*)
  47.       drupal_user="${1#*=}"
  48.       ;;
  49.     --httpd_group=*)
  50.       httpd_group="${1#*=}"
  51.       ;;
  52.     --help) print_help;;
  53.     *)
  54.       printf "Invalid argument, run --help for valid arguments.\n";
  55.       exit 1
  56.   esac
  57. shift
  58. done
  59.  
  60. # Basic check to see if this is a valid Drupal install
  61. if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/modules/system/system.module" ]; then
  62. printf "Please provide a valid Drupal path.\n"
  63.   print_help
  64.   exit 1
  65. fi
  66.  
  67. # Basic check to see if valiud user
  68. if [ -z "${drupal_user}" ] || [ $(id -un ${drupal_user} 2> /dev/null) != "${drupal_user}" ]; then
  69. printf "Please provide a valid user.\n"
  70.   print_help
  71.   exit 1
  72. fi
  73.  
  74. # Start changing permissions
  75. cd $drupal_path
  76. printf "Changing ownership of all contents of \"${drupal_path}\":\n user => \"${drupal_user}\" \t group => \"${httpd_group}\"\n"
  77. chown -R ${drupal_user}:${httpd_group} .
  78.  
  79. printf "Changing permissions of all directories inside \"${drupal_path}\" to \"rwxr-x---\"...\n"
  80. find . -type d -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.  
  85. printf "Changing permissions of \"files\" directories in \"${drupal_path}/sites\" to \"rwxrwx---\"...\n"
  86. cd ${drupal_path}/sites
  87. find . -type d -name files -exec chmod ug=rwx,o= '{}' \+
  88.  
  89. printf "Changing permissions of settings.php and default.settings.php files inside all directories in \"${drupal_path}/sites\" to \"r--r-----\"...\n"
  90. for x in ./*/settings.php; do
  91. printf "Changing permissions ${x} ...\n"
  92.   find ${x} -type f -exec chmod ug=r,o= '{}' \+
  93. done
  94. for x in ./*/default.settings.php; do
  95. printf "Changing permissions ${x} ...\n"
  96.   find ${x} -type f -exec chmod ug=r,o= '{}' \+
  97. done
  98.  
  99. printf "Changing permissions of all files inside all \"files\" directories in \"${drupal_path}/sites\" to \"rw-rw----\"...\n"
  100. printf "Changing permissions of all directories inside all \"files\" directories in \"${drupal_path}/sites\" to \"rwxrwx---\"...\n"
  101. for x in ./*/files; do
  102. printf "Changing permissions ${x} ...\n"
  103.   find ${x} -type d -exec chmod ug=rwx,o= '{}' \+
  104.   find ${x} -type f -exec chmod ug=rw,o= '{}' \+
  105. done
  106.  
  107. printf "Changing permissions of .htaccess files inside all \"files\" directories in \"${drupal_path}/sites\" to \"rw-r----\"...\n"
  108. for x in ./*/files/.htaccess; do
  109. printf "Changing permissions ${x} ...\n"
  110.   find ${x} -type f -exec chmod u=rw,g=r,o= '{}' \+
  111. done
  112.  
  113. printf "Changing permissions of \".git\" directories and files in \"${drupal_path}\" to \"rwx------\"...\n"
  114. cd ${drupal_path}
  115. chmod -R u=rwx,go= .git
  116. chmod u=rwx,go= .gitignore
  117.  
  118. printf "Changing permissions of various Drupal text files in \"${drupal_path}\" to \"rwx------\"...\n"
  119. cd ${drupal_path}
  120. chmod u=rwx,go= CHANGELOG.txt COPYRIGHT.txt INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.txt LICENSE.txt MAINTAINERS.txt UPGRADE.txt
  121.  
  122. echo "Done setting proper permissions on files and directories"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement