ScoDoc/tools/restore_scodoc_data.sh

141 lines
3.4 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, Jul 2021
2020-09-26 16:19:37 +02:00
#
2021-08-02 10:34:28 +02:00
# Le répertoire de ce script:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source "$SCRIPT_DIR/config.sh"
source "$SCRIPT_DIR/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/tools/psql_restore_databases.sh $PG_DUMPFILE" postgres
2020-09-26 16:19:37 +02:00
#
echo Copying data files...
rm -rf "${SCODOC_DIR:?}/var"
$COPY "$SRC/var" "$SCODOC_DIR"
2020-09-26 16:19:37 +02:00
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 "$SCODOC_USER"."$SCODOC_GROUP" "${SCODOC_VAR_DIR:?}/config/"
chmod 755 "${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
rm -rf "${SCODOC_DIR:?}/log"
$COPY "$SRC/log" "$SCODOC_DIR/"
2020-09-26 16:19:37 +02:00
# Fix file ownership and access rights
chown -R "$SCODOC_USER"."$SCODOC_GROUP" "${SCODOC_DIR}"
chmod -R 755 "$SCODOC_DIR"
2020-09-26 16:19:37 +02:00
# 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