├── ChangeLog ├── README └── wpstatic /ChangeLog: -------------------------------------------------------------------------------- 1 | CHANGE LOG FILE for wpstatic by Ammon Shepherd 2 | 3 | FORMAT 4 | VERSION DATE (YYYY-MM-DD) NAME (if none, it's Ammon) 5 | 6 | 7 | 1.2.1 2011-03-24 8 | * Added the EPOCH seconds to the name of the mysql backups so that the 9 | original is not overwritten on subsequent attempts at making a static 10 | version. 11 | * Added option to make a zip file of the wp-content, .htaccess, and 12 | database files. 13 | 14 | 15 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ARCHIVED 2 | RRCHNM archived this repository in February 2022. Last actual code activity was November 2015. If you need more information or to unarchive this repository, please contact us at webmaster at chnm.gmu.edu 3 | 4 | 5 | Usage: wpstatic [options] 6 | 7 | Run the script in the directory where you keep the wp-config.php file. 8 | This will create a directory with a static version of the WordPress site, 9 | complete with HTML, CSS, Javascript and images or other media files. 10 | 11 | -a Skip everything (database backups, and htaccess check), just generate the static files 12 | 13 | -b Skip the first backup of database 14 | 15 | -d Skip the second backup of database 16 | 17 | -h Show help text 18 | 19 | -p Skip changing the permalink and closing comments options in the database 20 | 21 | -t Skip check for and changing the .htaccess file 22 | 23 | -z Make a zip file of the wp-content, .htaccess, and database files. 24 | 25 | -------------------------------------------------------------------------------- /wpstatic: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #------------------------------------------------------------------------------# 4 | # wpstatic: Make a static copy of your WordPress site # 5 | # # 6 | # Simply run this script in the folder where you keep the wp-config.php file # 7 | # # 8 | # @Author: Ammon Shepherd # 9 | # @Date: 03.24.11 # 10 | VERSION="1.2.1" 11 | #------------------------------------------------------------------------------# 12 | 13 | set -e # End the script if any statement returns a non-true return value 14 | set -u # End script if an unset variable is encountered. 15 | 16 | USAGE=' 17 | Usage: wpstatic [options] 18 | Version '$VERSION' 19 | 20 | Run the script in the directory where you keep the wp-config.php file. 21 | This will create a directory with a static version of the WordPress site, 22 | complete with HTML, CSS, Javascript and images or other media files. 23 | 24 | -a Skip everything (database backups, and htaccess check), just generate the static files 25 | 26 | -b Skip the first backup of database 27 | 28 | -d Skip the second backup of database 29 | 30 | -h Show help text 31 | 32 | -p Skip changing the permalink and closing comments options in the database 33 | 34 | -t Skip check for and changing the .htaccess file 35 | 36 | -z Skip creation of a zip file of the wp-content, .htaccess, and database files. 37 | ' 38 | 39 | 40 | # Get the options 41 | while getopts "abdhptz" options; do 42 | case $options in 43 | a ) SKIPALL="true";; 44 | b ) SKIPDB1="true";; 45 | d ) SKIPDB2="true";; 46 | h ) echo "$USAGE" 47 | exit 0;; 48 | p ) SKIPFIX="true";; 49 | t ) SKIPHT="true";; 50 | z ) SKIPZIP="true";; 51 | \? ) echo "$USAGE" 52 | exit 1;; 53 | * ) echo "$USAGE" 54 | exit 1;; 55 | esac 56 | done 57 | shift `expr $OPTIND - 1` 58 | 59 | 60 | if [[ ! -f wp-config.php ]]; then 61 | echo "No wp-config.php" 62 | exit 3 63 | fi 64 | 65 | 66 | epsecs=( $(date +'%s') ) 67 | path=( $(pwd) ) 68 | 69 | # Get information from the wp-config.php file in order to make a backup copy 70 | sqluser=( $(grep "DB_USER" wp-config.php | sed -r -e "s/^[^']+'.*', '([^']+)'.*/\1/") ) 71 | sqlpass=( $(grep "DB_PASSWORD" wp-config.php | sed -r -e "s/^[^']+'.*', '([^']+)'.*/\1/") ) 72 | db_name=`grep "DB_NAME" wp-config.php | sed -r -e "s/^[^']+'.*', '([^']+)'.*/\1/"` 73 | db_host=`grep "DB_HOST" wp-config.php | sed -r -e "s/^[^']+'.*', '([^']+)'.*/\1/"` 74 | tb_prefix=( $(grep "table_prefix" wp-config.php | cut -d";" -f1 | cut -d"=" -f2 | sed -e "s|'||g" -e 's|"||g' -e "s|;||g" -e "s| ||g") ) 75 | 76 | 77 | site_url=( $(mysql -u$sqluser -p$sqlpass -h$db_host $db_name --raw --silent --silent --execute="SELECT option_value FROM ${tb_prefix}options WHERE option_name = 'siteurl' LIMIT 1;") ) 78 | if [ "$?" -ne 0 ]; then 79 | echo "Error getting site_url from database. Check the wp-config.php file." 80 | exit 81 | fi 82 | #Get rid of the trailing slash if there, then add one. This makes sure there is always a trailing slash. 83 | site_url="${site_url%/}/" 84 | path=( $(echo $site_url | cut -d'/' -f4- ) ) 85 | if [[ -z "${path-}" ]]; then 86 | path="" 87 | fi 88 | static=( $(echo $site_url | awk -F/ '{DZ=NF-1}; END {print $DZ}' ) ) 89 | cdirs=( $(echo $site_url | cut -d'/' -f4- | awk -F/ '{print NF-1}' ) ) 90 | 91 | # home url needed for the actual URL of the wp install, could be different from the physical location 92 | home_url=( $(mysql -u$sqluser -p$sqlpass -h$db_host $db_name --raw --silent --silent --execute="SELECT option_value FROM ${tb_prefix}options WHERE option_name = 'home' LIMIT 1;") ) 93 | 94 | # If the home url is empty, set it to the site_url 95 | if [[ -z "${home_url-}" ]]; then 96 | home_url=$site_url 97 | fi 98 | 99 | home_url="${home_url%/}/" 100 | home_path=( $(echo $home_url | cut -d'/' -f4- ) ) 101 | home_static=( $(echo $home_url | awk -F/ '{DZ=NF-1}; END {print $DZ}' ) ) 102 | home_cdirs=( $(echo $home_url | cut -d'/' -f4- | awk -F/ '{print NF-1}' ) ) 103 | if [[ 0 -gt $home_cdirs ]]; then 104 | home_cdirs="0" 105 | fi 106 | 107 | 108 | # Check for skipping all backups and changes 109 | if [[ -z "${SKIPALL-}" ]]; then 110 | 111 | table_list=( $(mysql -u$sqluser -p$sqlpass -h$db_host $db_name --raw --silent --silent --execute="SHOW TABLES;") ) 112 | if [ "$?" -ne 0 ]; then 113 | echo "Error getting list of tables from database. Check results of site_url variable." 114 | exit 115 | fi 116 | 117 | # Clear the tables value 118 | tables="" 119 | for tablename in ${table_list[@]} 120 | do 121 | if [[ "$tablename" =~ $tb_prefix ]]; then 122 | tables+="$tablename " 123 | fi 124 | done 125 | 126 | 127 | 128 | 129 | ###################################################################### 130 | ##### Create backup of unmolested database 131 | if [[ -z "${SKIPDB1-}" ]]; then 132 | echo "Creating backup of MySQL database '$db_name' ..." 133 | mysqldump -u $sqluser -p$sqlpass -h$db_host $db_name $tables > .${db_name}_${tb_prefix}BACKUP-${epsecs}.sql 134 | if [ "$?" -ne 0 ]; then 135 | echo "Error making backup of database. Check value of database and user info variables." 136 | exit 137 | fi 138 | fi 139 | 140 | 141 | 142 | 143 | ###################################################################### 144 | ##### Update the permalink structure and close comments 145 | if [[ -z "${SKIPFIX-}" ]]; then 146 | echo 147 | echo "Updating permalink structure and public db fields. Closing comments and pings on all posts and pages." 148 | 149 | mysql -u $sqluser -p$sqlpass -h$db_host $db_name --execute="UPDATE ${tb_prefix}options SET option_value = '/%year%/%monthnum%/%day%/%postname%/' WHERE ${tb_prefix}options.option_name = 'permalink_structure' LIMIT 1; UPDATE ${tb_prefix}options SET option_value = 1 WHERE ${tb_prefix}options.option_name = 'blog_public' LIMIT 1; UPDATE ${tb_prefix}posts SET comment_status = 'closed'; UPDATE ${tb_prefix}posts SET ping_status = 'closed';" 150 | if [ "$?" -ne 0 ]; then 151 | echo "Error running update commands on database. Check value of database and user info variables." 152 | exit 153 | fi 154 | fi 155 | 156 | 157 | 158 | ###################################################################### 159 | ##### Check and edit .htaccess file 160 | if [[ -z "${SKIPHT-}" ]]; then 161 | echo 162 | echo "Checking .htaccess file" 163 | if [[ -f .htaccess ]]; then 164 | if [[ $(grep "RewriteBase /$path" .htaccess) ]]; then 165 | echo "Current .htaccess seems OK." 166 | cat .htaccess 167 | echo 168 | else 169 | echo "Added to existing .htaccess file." 170 | echo ' 171 | 172 | RewriteEngine On 173 | RewriteBase /'$path' 174 | RewriteCond %{REQUEST_FILENAME} !-f 175 | RewriteCond %{REQUEST_FILENAME} !-d 176 | RewriteRule . /'$path'index.php [L] 177 | 178 | ' >> .htaccess 179 | cat .htaccess 180 | echo 181 | fi 182 | else 183 | echo "Created new .htaccess file." 184 | echo ' 185 | 186 | RewriteEngine On 187 | RewriteBase /'$path' 188 | RewriteCond %{REQUEST_FILENAME} !-f 189 | RewriteCond %{REQUEST_FILENAME} !-d 190 | RewriteRule . /'$path'index.php [L] 191 | 192 | ' > .htaccess 193 | cat .htaccess 194 | echo 195 | fi 196 | fi 197 | 198 | 199 | 200 | ###################################################################### 201 | ###### Backup of updated Database 202 | if [[ -z "${SKIPDB2-}" ]]; then 203 | echo "Making new backup of database." 204 | mysqldump -u $sqluser -p$sqlpass -h$db_host $db_name $tables > .${db_name}_${tb_prefix}BACKUP2-${epsecs}.sql 205 | if [ "$?" -ne 0 ]; then 206 | echo "Error making second backup copy. Check value of database and user info variables." 207 | exit 208 | fi 209 | fi 210 | 211 | fi # end of skip all check 212 | 213 | echo "Don't forget to change the theme files to limit comment, RSS, meta, and login links, and search fields." 214 | 215 | echo -n "Run wget now? [y/n]: " 216 | read rwget 217 | if [ "y" == $rwget ]; then 218 | wget --mirror --cut-dirs=$home_cdirs -P $home_static-static -nH -np -p -k -E $home_url 219 | else 220 | echo "Next run wget --mirror --cut-dirs=$home_cdirs -P $home_static-static -nH -np -p -k -E $home_url" 221 | fi 222 | echo 223 | 224 | 225 | if [[ -z "${SKIPALL-}" ]]; then 226 | ###################################################################### 227 | ###### Make a zip file of the wp-contents and dabase backups 228 | if [[ -z "${SKIPZIP-}" ]]; then 229 | echo "Making archive of wp-content, .htaccess, and database backups." 230 | zip -r wp-content.zip wp-content .${db_name}_${tb_prefix}BACKUP-${epsecs}.sql .${db_name}_${tb_prefix}BACKUP2-${epsecs}.sql .htaccess 231 | mkdir -p ${home_static}-static 232 | mv wp-content.zip ${home_static}-static 233 | fi 234 | 235 | fi # end of skip all check 236 | 237 | 238 | exit 0 239 | --------------------------------------------------------------------------------