aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/backup-restore-site
blob: e070255f4297e78aa659e7afd0349de4c72758c6 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
#
# Restore a website from backup.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

# Load.
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load

# Basic parameters.
SITE="$3"
SITES="/var/sites"
FOLDER="$SITES/$SITE"
WWW="/var/www/data"

# Syntax check.
if [ -z "$SITE" ]; then
  hydra_action_usage
  exit 1
fi

# Check restore strategy.
if [ "$1" == "backups" ]; then
  hydra_backup_environment_local_website $2 $3
elif [ "$1" == "localhost" ] || [ "$1" == "`facter hostname`" ]; then
  hydra_backup_environment_local
else
  hydra_backup_environment_remote $1 restore
fi

# Set site user.
if hydra_check_user $SITE; then
  SITE_USER="$SITE"
else
  SITE_USER="root"
fi

# Set site group.
if hydra_check_group $SITE; then
  SITE_GROUP="$SITE"
else
  SITE_GROUP="root"
fi

# Local backups are stored compressed
if [ "$1" == "localhost" ] || [ "$1" == "backups" ]; then
  if [ -e "$RESTOREDIR/$SITE.tar.bz2" ]; then
    ( cd $RESTOREDIR && tar xvf $SITE.tar.bz2 )
  fi
fi

# Check if folder exist on the backup.
if [ ! -d "$RESTOREDIR/$FOLDER" ]; then
  echo "Folder $FOLDER does not exist at restored backup $RESTOREDIR"
  exit 1
fi

# Check if there's already a site folder and backup it.
if [ -e "$FOLDER" ]; then
  echo "Folder $FOLDER already exists, backing it up first..."
  hydractl backup-site $SITE
  if [ "$?" != "0" ]; then
    echo "Error backing up $FOLDER"
    exit 1
  else
    echo "Erasing old site folder"
    rm -rf $FOLDER
  fi
fi

# Copy site folder from backup
if [ -d "$RESTOREDIR/$FOLDER" ]; then
  echo "Copying site $SITE from backup $RESTOREDIR..."
  cp -a $RESTOREDIR/$FOLDER $FOLDER
fi

# Deleted uncompressed local backup
if [ "$1" == "localhost" ]; then
  rm -rf $RESTOREDIR/$FOLDER 
fi

# Fix permissions
chown -R root.root $FOLDER

# Trac
if [ -e "$FOLDER/trac" ]; then
  ( cd $FOLDER/trac && chown -R $SITE_USER.$SITE_GROUP attachments conf db auth plugins .egg-cache )
fi

# PmWiki
if [ -e "$FOLDER/wiki" ]; then
  ( cd $FOLDER/wiki && chown -R $SITE_USER.$SITE_GROUP wiki.d uploads )
  chown $SITE.root $FOLDER/wiki/local/config.php
  chmod 660 $FOLDER/wiki/local/config.php
fi

# Site
if [ -e "$FOLDER/site" ]; then
  chown -R $SITE_USER.$SITE_GROUP $FOLDER/site
fi

# Drupal
if [ -e "$FOLDER/drupal" ]; then
  SERIES="$4"

  chown -R $SITE_USER.$SITE_GROUP $FOLDER/drupal/files
  chown root.$SITE_GROUP $FOLDER/drupal/settings.php
  chmod 640 $FOLDER/drupal/settings.php

  if [ -e "/etc/apache2/sites-available/$SITE" ]; then
    SERVER_ALIAS="`grep ServerAlias /etc/apache2/sites-available/$SITE | sed -e 's/ServerAlias//'`"
  fi

  if [ ! -z "$SERIES" ]; then
    if [ -e "$WWW/drupal-$SERIES/sites" ]; then
      echo "Creating basic drupal symlinks..."
      (
      cd $WWW/drupal-$SERIES/sites
      ln -sf $FOLDER/drupal $SITE
      ln -sf $FOLDER/drupal $SITE.`facter domain`

      for server_alias in $SERVER_ALIAS; do
        if [ ! -e "$server_alias" ]; then
          ln -sf $FOLDER/drupal $server_alias
        fi
      done
      )
    else
      echo "No drupal $SERIES found in the system, so no symlink handling"
    fi
  else
    echo "No drupal series argument provided, so no symlink handling"
  fi
fi

# Check if there are database dumps
if [ -f "$DB_DUMP_BASE/$SITE.sql.gz" ]; then
  DB_DUMP_EXT=".gz"
  DB_DUMP_EXTRACT="gunzip"
elif [ -f "$DB_DUMP_BASE/$SITE.sql.bz2" ]; then
  DB_DUMP_EXT=".bz2"
  DB_DUMP_EXTRACT="bunzip2"
elif [ -f "$DB_DUMP_BASE/$SITE.sql" ]; then
  DB_DUMP_EXT=""
  DB_DUMP_EXTRACT="true"
fi

# Restore database
if [ ! -z "$DB_DUMP_EXT" ]; then
  echo "Restoring database $SITE..."
  BASEDIR="$FOLDER"
  hydra_set_tmpfile $SITE -d
  cp $DB_DUMP_BASE/$SITE.sql$DB_DUMP_EXT $TMPWORK
  ( cd $TMPWORK && $DB_DUMP_EXTRACT $SITE.sql$DB_DUMP_EXT )
  hydra_truncate_database $SITE
  mysql $SITE < $TMPWORK/$SITE.sql
  hydra_unset_tmpfile $TMPWORK
else
  echo "Databases should be manually restored"
fi

# Ikiwiki
if [ -e "$FOLDER/ikiwiki" ]; then
  echo "Restoring ikiwiki for $SITE..."
  file="/etc/ikiwiki/$SITE.setup"
  if [ -e "$file" ] && [ -e "/var/git/repositories/$SITE.git" ]; then
    site="`basename $file .setup`"
    git clone file:///var/git/repositories/$SITE /var/sites/$SITE/ikiwiki_src
    ikiwiki --setup $file --rebuild
  else
    echo "Either $file or git repository not found for $SITE ikiwiki instance"
  fi
fi

# Moin
if [ -e "$FOLDER/moin" ]; then
  echo "Restoring moin for $SITE..."
  chown -R root.root $FOLDER/moin/
  chown -R $SITE_USER.$SITE_GROUP $FOLDER/moin/{cgi-bin,data,underlay}
fi