Fix backup and restore scripts

This commit is contained in:
Josué Tille 2017-01-31 23:25:17 +01:00
parent 63e9bff3ff
commit c13c060b98
5 changed files with 184 additions and 56 deletions

View file

@ -18,15 +18,6 @@ init_script() {
app=$YNH_APP_INSTANCE_NAME
}
correct_path() {
if [ "${path:0:1}" != "/" ] && [ ${#path} -gt 0 ]; then
path="/$path"
fi
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
path="${path:0:${#path}-1}"
fi
}
set_configuration() {
if [[ -e /var/www/$app ]]
then
@ -59,4 +50,159 @@ get_source() {
then
ynh_die "Error : can't get seafile source"
fi
}
}
CHECK_VAR () { # Vérifie que la variable n'est pas vide.
# $1 = Variable à vérifier
# $2 = Texte à afficher en cas d'erreur
test -n "$1" || (echo "$2" >&2 && false)
}
EXIT_PROPERLY () { # Provoque l'arrêt du script en cas d'erreur. Et nettoye les résidus.
exit_code=$?
if [ "$exit_code" -eq 0 ]; then
exit 0 # Quitte sans erreur si le script se termine correctement.
fi
trap '' EXIT
set +eu
echo -e "\e[91m \e[1m" # Shell in light red bold
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!" >&2
if type -t CLEAN_SETUP > /dev/null; then # Vérifie l'existance de la fonction avant de l'exécuter.
CLEAN_SETUP # Appel la fonction de nettoyage spécifique du script install.
fi
# Compense le bug de ssowat qui ne supprime pas l'entrée de l'app en cas d'erreur d'installation.
sudo sed -i "\@\"$domain$path/\":@d" /etc/ssowat/conf.json
ynh_die
}
TRAP_ON () { # Activate signal capture
set -eu # Exit if a command fail, and if a variable is used unset.
trap EXIT_PROPERLY EXIT # Capturing exit signals on shell script
}
# Ignore the yunohost-cli log to prevent errors with conditionals commands
# usage: NO_LOG COMMAND
# Simply duplicate the log, execute the yunohost command and replace the log without the result of this command
# It's a very badly hack...
# Petite copie perso à mon usage ;)
NO_LOG() {
ynh_cli_log=/var/log/yunohost/yunohost-cli.log
sudo cp -a ${ynh_cli_log} ${ynh_cli_log}-move
eval $@
exit_code=$?
sudo mv ${ynh_cli_log}-move ${ynh_cli_log}
return $?
}
CHECK_USER () { # Vérifie la validité de l'user admin
# $1 = Variable de l'user admin.
ynh_user_exists "$1" || (echo "Wrong admin" >&2 && false)
}
CHECK_PATH () { # Vérifie la présence du / en début de path. Et son absence à la fin.
if [ "${path:0:1}" != "/" ]; then # Si le premier caractère n'est pas un /
path="/$path" # Ajoute un / en début de path
fi
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then # Si le dernier caractère est un / et que ce n'est pas le seul caractère.
path="${path:0:${#path}-1}" # Supprime le dernier caractère
fi
}
CHECK_DOMAINPATH () { # Vérifie la disponibilité du path et du domaine.
sudo yunohost app checkurl $domain$path -a $app
}
CHECK_FINALPATH () { # Vérifie que le dossier de destination n'est pas déjà utilisé.
final_path=/var/www/$app
if [ -e "$final_path" ]
then
echo "This path already contains a folder" >&2
false
fi
}
SETUP_SOURCE () { # Télécharge la source, décompresse et copie dans $final_path
# $1 = Nom de l'archive téléchargée.
wget -nv -i ../sources/source_url -O $1
# Vérifie la somme de contrôle de la source téléchargée.
md5sum -c ../sources/source_md5 --status || (echo "Corrupt source" >&2 && false)
# Décompresse la source
if [ "$(echo ${1##*.})" == "gz" ]; then
tar -x -f $1
elif [ "$(echo ${1##*.})" == "zip" ]; then
unzip -q $1
else
false # Format d'archive non pris en charge.
fi
# Copie les fichiers sources
sudo cp -a $(cat ../sources/source_dir)/. "$final_path"
# Copie les fichiers additionnels ou modifiés.
if test -e "../sources/ajouts"; then
sudo cp -a ../sources/ajouts/. "$final_path"
fi
}
STORE_MD5_CONFIG () { # Enregistre la somme de contrôle du fichier de config
# $1 = Nom du fichier de conf pour le stockage dans settings.yml
# $2 = Nom complet et chemin du fichier de conf.
ynh_app_setting_set $app $1_file_md5 $(sudo md5sum "$2" | cut -d' ' -f1)
}
CHECK_MD5_CONFIG () { # Créé un backup du fichier de config si il a été modifié.
# $1 = Nom du fichier de conf pour le stockage dans settings.yml
# $2 = Nom complet et chemin du fichier de conf.
if [ "$(ynh_app_setting_get $app $1_file_md5)" != $(sudo md5sum "$2" | cut -d' ' -f1) ]; then
sudo cp -a "$2" "$2.backup.$(date '+%d.%m.%y_%Hh%M,%Ss')" # Si le fichier de config a été modifié, créer un backup.
fi
}
FIND_PORT () { # Cherche un port libre.
# $1 = Numéro de port pour débuter la recherche.
port=$1
while ! sudo yunohost app checkport $port ; do
port=$((port+1))
done
CHECK_VAR "$port" "port empty"
}
### REMOVE SCRIPT
REMOVE_NGINX_CONF () { # Suppression de la configuration nginx
if [ -e "/etc/nginx/conf.d/$domain.d/$app.conf" ]; then # Delete nginx config
echo "Delete nginx config"
sudo rm "/etc/nginx/conf.d/$domain.d/$app.conf"
sudo service nginx reload
fi
}
SECURE_REMOVE () { # Suppression de dossier avec vérification des variables
chaine="$1" # L'argument doit être donné entre quotes simple '', pour éviter d'interpréter les variables.
no_var=0
while (echo "$chaine" | grep -q '\$') # Boucle tant qu'il y a des $ dans la chaine
do
no_var=1
global_var=$(echo "$chaine" | cut -d '$' -f 2) # Isole la première variable trouvée.
only_var=\$$(expr "$global_var" : '\([A-Za-z0-9_]*\)') # Isole complètement la variable en ajoutant le $ au début et en gardant uniquement le nom de la variable. Se débarrasse surtout du / et d'un éventuel chemin derrière.
real_var=$(eval "echo ${only_var}") # `eval "echo ${var}` permet d'interpréter une variable contenue dans une variable.
if test -z "$real_var" || [ "$real_var" = "/" ]; then
echo "Variable $only_var is empty, suppression of $chaine cancelled." >&2
return 1
fi
chaine=$(echo "$chaine" | sed "s@$only_var@$real_var@") # remplace la variable par sa valeur dans la chaine.
done
if [ "$no_var" -eq 1 ]
then
if [ -e "$chaine" ]; then
echo "Delete directory $chaine"
sudo rm -r "$chaine"
fi
return 0
else
echo "No detected variable." >&2
return 1
fi
}

View file

@ -1,8 +1,5 @@
#!/bin/bash
# Exit on command errors and treat unset variables as an error
set -eu
######## Actually we cant use common script in backup / restore script see this issue for more informations : https://dev.yunohost.org/issues/621
# # Import common cmd
# source ./_common.sh
@ -40,9 +37,6 @@ set_configuration() {
# Init script
init_script
# Retrieve arguments
app=$YNH_APP_INSTANCE_NAME
# Set configuration for user and final path
set_configuration
@ -54,7 +48,7 @@ APP=$2
# retrieve useful param
domain=$(ynh_app_setting_get ${APP} domain)
db_pwd=$(ynh_app_setting_get ${APP} db_pwd)
db_pwd=$(ynh_app_setting_get ${APP} mysqlpwd)
# Backup app files
sudo mkdir -p "${BACKUP_DIR}/www"

View file

@ -19,7 +19,7 @@ final_path=/var/www/$app
seafile_user=www-data
# Correct path if it is not correct
correct_path
CHECK_PATH
# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
@ -31,15 +31,6 @@ get_source $architecture $seafile_version
# Retrieve admin email
admin_email=$(sudo yunohost user info $admin | grep mail: | sed "s/mail: //g")
port=''
findPort () {
port=$1
while ! sudo yunohost app checkport $port ; do
port=$(($port + 1))
done
}
# Check dependencies
sudo apt-get update
sudo apt-get install -qq python2.7 python-setuptools python-simplejson python-imaging python-mysqldb python-flup expect
@ -57,11 +48,11 @@ sudo mv seafile-server-$seafile_version/* $final_path/seafile-server-$seafile_ve
sudo mv '/tmp/seafile_src.tar.gz' $final_path/installed/seafile-server_${seafile_version}.tar.gz
# Find available ports
findPort 8000
FIND_PORT 8000
seahub_port=$port
findPort 8082
FIND_PORT 8082
fileserver_port=$port
findPort 8080
FIND_PORT 8080
webdav_port=$port
# store config in yunohost

View file

@ -18,13 +18,13 @@ init_script() {
app=$YNH_APP_INSTANCE_NAME
}
correct_path() {
if [ "${path:0:1}" != "/" ] && [ ${#path} -gt 0 ]; then
path="/$path"
fi
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
path="${path:0:${#path}-1}"
fi
CHECK_PATH () { # Vérifie la présence du / en début de path. Et son absence à la fin.
if [ "${path:0:1}" != "/" ]; then # Si le premier caractère n'est pas un /
path="/$path" # Ajoute un / en début de path
fi
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then # Si le dernier caractère est un / et que ce n'est pas le seul caractère.
path="${path:0:${#path}-1}" # Supprime le dernier caractère
fi
}
######## End of common fonctions
@ -39,19 +39,16 @@ seafile_user=www-data
# The parameter $1 is the backup directory location dedicated to the app
BACKUP_DIR=$1
# The parameter $2 is the id of the app instance ex: strut__2
APP=$2
# retrieve useful param
domain=$(ynh_app_setting_get ${APP} domain)
db_pwd=$(ynh_app_setting_get ${APP} db_pwd)
path=$(ynh_app_setting_get ${APP} path)
domain=$(ynh_app_setting_get ${app} domain)
db_pwd=$(ynh_app_setting_get ${app} mysqlpwd)
path=$(ynh_app_setting_get ${app} path)
# Correct path if it is not correct
correct_path
CHECK_PATH
# Check domain/path availability
sudo yunohost app checkurl $domain$path -a ${APP} \
sudo yunohost app checkurl $domain$path -a ${app} \
|| (echo "Path not available: $domain$path" && ynh_die "Error : path not available")
# Restore dependencies
@ -65,21 +62,25 @@ sudo cp -a "${BACKUP_DIR}/www/." $final_path
sudo chown -R $seafile_user:$seafile_user $final_path
# Restore conf files
sudo cp -a "${BACKUP_DIR}/conf/${APP}.conf" /etc/nginx/conf.d/$domain.d/${APP}.conf
sudo cp -a "${BACKUP_DIR}/conf/${APP}" /etc/logrotate.d/${APP}
sudo cp -a "${BACKUP_DIR}/conf/${app}.conf" /etc/nginx/conf.d/$domain.d/${app}.conf
sudo cp -a "${BACKUP_DIR}/conf/${app}" /etc/logrotate.d/${app}
sudo cp -a "${BACKUP_DIR}/conf/seafile-server" /etc/init.d/seafile-server
sudo chmod +x /etc/init.d/seafile-server
# Restore data
seafile_data=/home/yunohost.app/seafile-data
mkdir -p $seafile_data
sudo mkdir -p $seafile_data
sudo cp -a "${BACKUP_DIR}/data/." /home/yunohost.app/seafile-data/.
sudo chown -R $seafile_user:$seafile_user $seafile_data
# Restore mysql dump
sudo su -c "mysql -u ${APP} -p$db_pwd ccnetdb < ${BACKUP_DIR}/ccnetdb.dmp"
sudo su -c "mysql -u ${APP} -p$db_pwd seafiledb < ${BACKUP_DIR}/seafiledb.dmp"
sudo su -c "mysql -u ${APP} -p$db_pwd seahubdb < ${BACKUP_DIR}/seahubdb.dmp"
dbuser=seafile
ynh_mysql_create_db ccnetdb "$dbuser" "$db_pwd"
ynh_mysql_create_db seafiledb "$dbuser" "$db_pwd"
ynh_mysql_create_db seahubdb "$dbuser" "$db_pwd"
sudo su -c "mysql -u ${app} -p$db_pwd ccnetdb < ${BACKUP_DIR}/ccnetdb.dmp"
sudo su -c "mysql -u ${app} -p$db_pwd seafiledb < ${BACKUP_DIR}/seafiledb.dmp"
sudo su -c "mysql -u ${app} -p$db_pwd seahubdb < ${BACKUP_DIR}/seahubdb.dmp"
# Restore sso persistent config
sudo python $final_path/add_sso_conf.py
@ -90,8 +91,8 @@ sudo yunohost service add seafile-server
# Reload/restart services
sudo update-rc.d seafile-server defaults
sudo service rsyslog restart
sudo service nginx reload
sudo yunohost app ssowatconf
sudo service nginx reload
# start seafile
sudo service seafile-server start

View file

@ -8,13 +8,9 @@ init_script
# Retrieve settings
installed_version=$(ynh_app_setting_get $app installed_version)
path=$(ynh_app_setting_get "$app" path)
architecture=$(ynh_app_setting_get $app architecture)
root_pwd=$(sudo cat /etc/yunohost/mysql)
# Correct path if it is not correct
correct_path
# Set configuration for user and final path
set_configuration