Skip to content

Instantly share code, notes, and snippets.

@taddev
Last active December 27, 2015 00:19
Show Gist options
  • Save taddev/7237241 to your computer and use it in GitHub Desktop.
Save taddev/7237241 to your computer and use it in GitHub Desktop.
This is a BASH script that will change permissions on the various directories of a Wordpress site, including the entire site. The script assumes that you have a different user and group permissions set on the directory; the permissions on each folder and file should be defined something like this <user>:<web server user>. In this way your user a…
#!/bin/bash
#
# Copyright (C) 2013 Tad DeVries <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see [http://www.gnu.org/licenses/].
WPDIR="/var/www/localhost/htdocs/blog/wp"
UNLOCKEDFILE=".unlocked"
WORKDIR=""
FORCE=false
LOCK=false
UNLOCK=false
lock () {
if [[ -f "$UNLOCKEDFILE"_$WORKDIR ]] || $FORCE; then
if $FORCE; then
echo "FORCED: Locking $WPDIR/$WORKDIR"
else
echo "Locking $WPDIR/$WORKDIR"
fi
sudo find $WPDIR/$WORKDIR -type f -exec chmod 644 {} \;
sudo find $WPDIR/$WORKDIR -type d -exec chmod 755 {} \;
rm -f "$UNLOCKEDFILE"_$WORKDIR
if [ "$WORKDIR" == "" ]; then
rm -f "$UNLOCKEDFILE"_*
fi
else
echo "$WORKDIR already locked, use --force to override"
fi
}
unlock () {
if [[ ! -f "$UNLOCKEDFILE"_$WORKDIR ]] && [[ ! -f "$UNLOCKEDFILE"_ ]] || $FORCE; then
if $FORCE; then
echo "FORCED: Unlocking $WPDIR/$WORKDIR"
else
echo "Unlocking $WPDIR/$WORKDIR"
fi
sudo find $WPDIR/$WORKDIR -type f -exec chmod 664 {} \;
sudo find $WPDIR/$WORKDIR -type d -exec chmod 775 {} \;
touch "$UNLOCKEDFILE"_$WORKDIR
else
echo "$WORKDIR already unlocked, use --force to override"
fi
}
usage () {
echo "usage: wp_secure.sh <<-l|--lock>|<-u|--unlock>> <-d|--directory> <directory> [-f|--force] [-h|--help]"
}
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
while [[ "$1" != "" ]]; do
case $1 in
-l | --lock ) if $UNLOCK; then
echo "Only define one option (lock/unlock) at a time"
exit 1
else
LOCK=true
fi
;;
-u | --unlock ) if $LOCK; then
echo "Only define one option (lock/unlock) at a time"
exit 1
else
UNLOCK=true
fi
;;
-f | --force ) FORCE=true
;;
-d | --directory ) shift
if [[ -d $WPDIR/$1 ]]; then
WORKDIR=$1
else
echo "$1 is not a valid Wordpress directory"
exit 1
fi
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if $LOCK; then
lock
elif $UNLOCK; then
unlock
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment