summaryrefslogtreecommitdiff
path: root/files/wordpress.sh
blob: 055f2041373ef95f0e80d7ac794a8bfaf7d17b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#
# wordpress management script.
#

BASE="/var/www/data"
LOCALE="-pt_BR"
WP_CLI="/usr/local/bin/wp-cli"
BASENAME="`basename $0`"
WHOAMI="`whoami`"

# Set alternative base
function wordpress_set_base {
  if [ ! -z "$1" ]; then
    BASE="$1"

    if [ ! -d "$BASE" ]; then
      echo "No such folder $BASE"
      exit 1
    fi
  fi
}

# Check for existing installations
function wordpress_check_existing {
  if [ -e "$BASE/wordpress-$1" ]; then
    echo "Folder $BASE/wordpress-$1 already exists, skipping."
    exit 1
  fi
}

# Deploy a fresh wordpress tree
function wordpress_deploy {
  if [ -z "$1" ]; then
    echo "Usage: `basename $0` deploy <version> [folder]"
    exit 1
  fi

  # Setup
  new="$1"
  wordpress_set_base $2
  wordpress_check_existing $new

  # Deploy a fresh wordpress tree
  cd $BASE
  wget http://br.wordpress.org/wordpress-$new$LOCALE.zip
  unzip wordpress-$new$LOCALE.zip && rm wordpress-$new$LOCALE.zip
  mv wordpress wordpress-$new/

  if [ "$WHOAMI" == "root" ]; then
    chown -R root.root wordpress-$new/
  fi
}

# Upgrade a wordpress instance using upstream source.
function wordpress_upgrade {
  if [ -z "2" ]; then
    echo "Usage: `basename $0` upgrade <old_version> <new_version> [folder]"
    exit 1
  fi

  # Setup
  old="$1"
  new="$2"
  extra_folders=""
  wordpress_set_base $3
  wordpress_check_existing $new

  cd $BASE

  if [ ! -d "wordpress-$old" ]; then
    echo "Installation not found: wordpress-$old"
    exit
  fi

  cp -alf wordpress-$old wordpress-$old.backup

  # Deploy a fresh wordpress tree
  wordpress_deploy $new

  # Copy files
  for file in .htaccess wp-config.php; do
    if [ -e "wordpress-$old/$file" ]; then
      cp -a wordpress-$old/$file wordpress-$new/
    fi
  done

  # Extra folders
  for extra_folder in $extra_folders; do
    if [ -d ../wordpress-$old/$extra_folder ]; then
      cp -Rp ../wordpress-$old/$extra_folder .
    fi
  done

  # Files
  rsync -av wordpress-$old/wp-content/uploads/ wordpress-$new/wp-content/uploads/
  rsync -av wordpress-$old/wp-content/blogs.dir/ wordpress-$new/wp-content/blogs.dir/

  # Plugins
  for plugin in `ls wordpress-$old/wp-content/plugins/`; do
    if [ ! -e "wordpress-$new/wp-content/plugins/$plugin" ]; then
      cp -r wordpress-$old/wp-content/plugins/$plugin wordpress-$new/wp-content/plugins/
    fi
  done

  # Themes
  for theme in `ls wordpress-$old/wp-content/themes/`; do
    if [ ! -e "wordpress-$new/wp-content/themes/$theme" ]; then
      cp -r wordpress-$old/wp-content/themes/$theme wordpress-$new/wp-content/themes/
    fi
  done

  # Fix permissions
  if [ "$WHOAMI" == "root" ]; then
    chown -R root.root wordpress-$new/wp-content/{plugins,themes}
  fi

  # Set sunrise symlink
  if [ -f "wordpress-$new/wp-content/plugins/wordpress-mu-domain-mapping/sunrise.php" ]; then
    ( cd wordpress-$new/wp-content && ln -s plugins/wordpress-mu-domain-mapping/sunrise.php )
  fi

  # Change symlink to point to the new location
  if [ -h "site" ]; then
    link="`readlink site`"
    if [ "`basename $link`" == "wordpress-$old" ]; then
      rm site && ln -s wordpress-$new site
    fi
  fi

  # Done
  echo "Audit: `du -hs wordpress-$old`"
  echo "Audit: `du -hs wordpress-$new`"
  echo "Check procedure and remove wordpress-$old once you make sure that everything is fine."
  echo "Then run 'wordpress core update-db'"
}

# Usage
function wordpress_usage {
  echo "Installation management commands:"
  echo "    $BASENAME deploy <version>"
  echo "    $BASENAME upgrade <old-version> <new-version>"
  echo ""
  echo -n "Wordpress CLI "
  $WP_CLI help
}

# Main procedure
if [ -z "$1" ]; then
  wordpress_usage
  exit 1
elif [ "$1" == "deploy" ]; then
  shift
  wordpress_deploy $*
elif [ "$1" == "upgrade" ]; then
  shift
  wordpress_upgrade $*
elif [ "$1" == "help" ]; then
  wordpress_usage
#elif [ "$1" == "core" ]; then
#  echo "Please use deploy or upgrade instead."
else
  # Send all other requests to wp-cli
  #$WP_CLI --path=$BASE/wordpress $*
  $WP_CLI $*
fi