TestsScoDoc7API/config/restore_scodoc_data.sh

144 lines
3.5 KiB
Bash
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/bin/bash
#
# ScoDoc: restore data (saved by save_scodoc_data) into current install
#
# Utile pour migrer ScoDoc d'un serveur a un autre
# A executer en tant que root sur le nouveau serveur
#
# E. Viennet, Sept 2011, Nov 2013, Mar 2017, Aug 2020
#
INSTANCE_DIR=/opt/scodoc/
SCODOC_DIR="${INSTANCE_DIR}/Products/ScoDoc"
SCODOC_VAR_DIR="${INSTANCE_DIR}/var/scodoc"
source utils.sh
2020-12-28 22:09:20 +01:00
check_uid_root "$0"
2020-09-26 16:19:37 +02:00
# Safety check
echo "Ce script va remplacer les donnees de votre installation ScoDoc par celles"
echo "enregistrees dans le fichier fourni."
echo "Ce fichier doit avoir ete cree par le script save_scodoc_data.sh, sur une autre machine."
echo
echo "Attention: TOUTES LES DONNEES DE CE SERVEUR SERONT REMPLACEES !"
echo "Notamment, tous les utilisateurs et departements existants seront effaces !"
echo
echo "TOUTES LES BASES POSTGRESQL SERONT EFFACEES !!!"
echo
echo -n "Voulez vous poursuivre cette operation ? (y/n) [n]"
2020-12-28 22:09:20 +01:00
read -r ans
2020-09-26 16:19:37 +02:00
if [ ! "$(norm_ans "$ans")" = 'Y' ]
then
echo "Annulation"
exit 1
fi
# Usage
if [ ! $# -eq 1 ]
then
echo "Usage: $0 directory_or_archive"
exit 1
fi
SRC=$1
if [ "${SRC:0:1}" != "/" ]
then
echo "Usage: $0 directory_or_archive"
echo "Erreur: utiliser un chemin absolu (commencant par /)"
exit 1
fi
# Source directory
if [ "${SRC##*.}" = 'tgz' ]
then
echo "Opening tgz archive..."
tmp=$(mktemp -d)
chmod a+rx "$tmp"
2020-12-28 22:09:20 +01:00
cd "$tmp" || terminate "directory error"
2020-09-26 16:19:37 +02:00
tar xfz "$SRC"
SRC=$(ls -1d "$tmp"/*)
IS_TMP=1
# If source is a tgz, can use mv
COPY="mv"
else
IS_TMP=0
# If source is a directory, does not modify its content
COPY="cp -rp"
fi
echo "Source is $SRC"
echo "Stopping ScoDoc..."
2020-12-05 17:29:26 +01:00
scodocctl stop
2020-09-26 16:19:37 +02:00
# Erase all postgres databases and load data
chmod a+rx "$SRC"
chmod a+r "$SRC"/scodoc.dump.txt
PG_DUMPFILE="$SRC/scodoc.dump.txt"
su -c "$SCODOC_DIR/config/psql_restore_databases.sh $PG_DUMPFILE" postgres
#
echo Copying data files...
2020-12-28 22:09:20 +01:00
rm -rf "${INSTANCE_DIR:?}/var"
2020-09-26 16:19:37 +02:00
$COPY "$SRC/var" "$INSTANCE_DIR"
2020-12-28 22:09:20 +01:00
if [ ! -e "${SCODOC_VAR_DIR:?}/config/" ]
2020-09-26 16:19:37 +02:00
then
2020-12-28 22:09:20 +01:00
mkdir "${SCODOC_VAR_DIR:?}/config/"
chown www-data.www-data "${SCODOC_VAR_DIR:?}/config/"
chmod 775 "${SCODOC_VAR_DIR:?}/config/"
2020-09-26 16:19:37 +02:00
fi
2020-12-28 22:09:20 +01:00
rm -rf "${SCODOC_DIR:?}/config/depts"
if [ -e "${SRC:?}/depts" ]
2020-09-26 16:19:37 +02:00
then
# legacy depts => move them to var
$COPY "$SRC/depts" "${SCODOC_VAR_DIR}/config/"
fi
rm -rf "$SCODOC_DIR/static/photos"
if [ -e "$SRC/photos" ]
then
# legacy photos (in <src>/static/photos) => move them to var
$COPY "$SRC/photos" "${SCODOC_VAR_DIR}/"
fi
2020-12-28 22:09:20 +01:00
rm -rf "${SCODOC_DIR:?}/logos"
2020-09-26 16:19:37 +02:00
$COPY "$SRC/logos" "$SCODOC_DIR/"
mv "$SCODOC_DIR/config/scodoc_config.py" "$SCODOC_DIR/config/scodoc_config.py.$(date +%Y%m%d-%H%M%S)"
$COPY "$SRC/scodoc_config.py" "$SCODOC_DIR/config/"
# Verifie le codage de ce fichier:
if [ -z "$(file $SCODOC_DIR/config/scodoc_config.py | grep -i UTF-8)" ]
then
mv "$SCODOC_DIR/config/scodoc_config.py" "$SCODOC_DIR/config/scodoc_config.py.orig"
iconv -f iso8859-15 -t utf-8 "$SCODOC_DIR/config/scodoc_config.py.orig" > "$SCODOC_DIR/config/scodoc_config.py"
fi
2020-12-28 22:09:20 +01:00
rm -rf "${INSTANCE_DIR:?}/log"
2020-09-26 16:19:37 +02:00
$COPY "$SRC/log" "$INSTANCE_DIR/"
# Fix file ownership and access rights
chown -R www-data.root "$INSTANCE_DIR/log"
chown -R www-data.root "$INSTANCE_DIR/var"
chmod 775 "$INSTANCE_DIR./log" "$INSTANCE_DIR./var"
chown -R www-data.root "$SCODOC_DIR"
chmod -R 775 "$SCODOC_DIR"
# Remove tmp directory
2020-12-28 22:09:20 +01:00
if [ "$IS_TMP" = "1" ]
2020-09-26 16:19:37 +02:00
then
2020-12-28 22:09:20 +01:00
rm -rf "${tmp}"
2020-09-26 16:19:37 +02:00
fi
# Mise a jour BD ScoDoc
2020-12-28 22:09:20 +01:00
cd ${SCODOC_DIR:?}/config || terminate "no config directory"
2020-09-26 16:19:37 +02:00
./upgrade.sh
#
echo
2020-12-05 17:29:26 +01:00
scodocctl start