From 5d9232f0822d0ff33ecea49c4469b2d1910df01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Mon, 4 Apr 2016 11:55:32 +0200 Subject: [PATCH] [enh] Rewrite and update backup/restore scripts to use helpers --- README.md | 4 +- conf/owncloud-deps.control | 2 +- scripts/backup | 48 ++++++------ scripts/restore | 151 +++++++++++++++++-------------------- 4 files changed, 100 insertions(+), 105 deletions(-) diff --git a/README.md b/README.md index d755a05..a29db89 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,10 @@ or from the Web administration: ## TODO * Test the upgrade from the current official package - * Update backup and restore scripts * Develop an app to integrate SSOwat logout - see [here](https://doc.owncloud.org/server/9.0/developer_manual/app/hooks.html#session) - * Check the external storage plugin + * Update the external storage plugin configuration - see + [here](https://doc.owncloud.org/server/9.0/admin_manual/configuration_server/occ_command.html#files-external-label) * ... ## Links ## diff --git a/conf/owncloud-deps.control b/conf/owncloud-deps.control index 89a8e51..82d5258 100644 --- a/conf/owncloud-deps.control +++ b/conf/owncloud-deps.control @@ -1,6 +1,6 @@ Section: misc Priority: optional -Homepage: https://mediagoblin.org/ +Homepage: https://owncloud.org/ Standards-Version: 3.9.2 Package: owncloud-deps diff --git a/scripts/backup b/scripts/backup index e0f21a2..5d0b905 100755 --- a/scripts/backup +++ b/scripts/backup @@ -1,31 +1,37 @@ #!/bin/bash -APP=${!#} - -CAN_BIND=1 - -# The parameter $1 is the backup directory location dedicated to the app +# Retrieve arguments backup_dir=$1 +app=${!#} +# Set app specific variables +dbname=$app +dbuser=$app +# Source app helpers +. /usr/share/yunohost/helpers +# Retrieve app settings +domain=$(ynh_app_setting_get $app domain) +path=$(ynh_app_setting_get $app path) +dbpass=$(ynh_app_setting_get $app mysqlpwd) -domain=$(sudo yunohost app setting $APP domain) -path=$(sudo yunohost app setting $APP path) -user=$(sudo yunohost app setting $APP admin_user) +# Copy the app source files +DESTDIR="/var/www/$app" +sudo cp -a "$DESTDIR" ./www -# Backup sources & data -sudo cp -a /var/www/$APP $backup_dir/www -# TODO Shallow copy because data could be very big -sudo cp -a /home/yunohost.app/$APP/data $backup_dir/data +# Copy the data directory +DATADIR="/home/yunohost.app/${app}/data" +ynh_bind_or_cp "$DATADIR" "${backup_dir}/data" 1 -# Copy Conf -sudo mkdir -p "${backup_dir}/conf" -sudo cp -a /etc/nginx/conf.d/$domain.d/$APP.conf $backup_dir/conf/nginx.conf -sudo cp -a /etc/php5/fpm/pool.d/$APP.conf "${backup_dir}/conf/php-fpm.conf" -sudo cp -a /etc/php5/cli/conf.d/20-apc.ini $backup_dir/conf/ \ - || sudo cp -a /etc/php5/cli/conf.d/20-apcu.ini $backup_dir/conf/ +# Copy the conf files +mkdir ./conf +sudo cp -a "/etc/nginx/conf.d/${domain}.d/${app}.conf" ./conf/nginx.conf +sudo cp -a "/etc/php5/fpm/pool.d/${app}.conf" ./conf/php-fpm.conf -# Backup db -db_pwd=$(sudo yunohost app setting $APP mysqlpwd) -sudo su -c "mysqldump -u $APP -p"$db_pwd" --no-create-db $APP > ${backup_dir}/db.sql" +# Dump the database +mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./db.sql + +# Copy the control file of the dependency package +# FIXME: find a way to retrieve package name from _common.sh? +dpkg-query -s owncloud-deps > ./owncloud-deps.control diff --git a/scripts/restore b/scripts/restore index b11878f..d012ef0 100755 --- a/scripts/restore +++ b/scripts/restore @@ -1,100 +1,89 @@ #!/bin/bash -# This restore script is adapted to Yunohost >=2.4 -APP=${!#} -# The parameter $1 is the backup directory location dedicated to the app +# Retrieve arguments backup_dir=$1 +app=${!#} +# TODO: Put a simple die function in app helpers, redeclare it since +# _common.sh cannot be easily sourced +die() { + printf "%s" "$1" 1>&2 + exit "${2:-1}" +} -# Get old parameter of the app -domain=$(sudo yunohost app setting $APP domain) -path=$(sudo yunohost app setting $APP path) -user=$(sudo yunohost app setting $APP admin_user) +# Set app specific variables +dbname=$app +dbuser=$app -# Check domain/path availability -sudo yunohost app checkurl $domain$path -a $APP -if [[ ! $? -eq 0 ]]; then - echo "There is already an app on this URL : $domain$path" | sudo tee /dev/stderr - exit 1 -fi +# Source app helpers +. /usr/share/yunohost/helpers -final_path=/var/www/$APP -if [ -d $final_path ]; then - echo "There is already a directory: $final_path " | sudo tee /dev/stderr - exit 1 -fi -data_path=/home/yunohost.app/$APP/data -if [ -d $data_path ]; then - echo "There is already a directory: $data_path " | sudo tee /dev/stderr - exit 1 -fi +# Retrieve old app settings +domain=$(ynh_app_setting_get $app domain) +path=$(ynh_app_setting_get $app path) +dbpass=$(ynh_app_setting_get $app mysqlpwd) -conf_nginx=/etc/nginx/conf.d/$domain.d/$APP.conf -if [ -f $conf_nginx ]; then - echo "There is already a nginx conf file at this path: $conf_nginx " | sudo tee /dev/stderr - exit 1 -fi +# TODO: Check domain/path availability with app helper +sudo yunohost app checkurl $domain$path -a $app \ + || die "The path ${domain}${path} is not available for app installation." -conf_fpm=/etc/php5/fpm/pool.d/$APP.conf -if [ -f $conf_fpm ]; then - echo "There is already a nginx conf file at this path: $conf_fpm " | sudo tee /dev/stderr - exit 1 -fi +# Check destination directory +DESTDIR="/var/www/$app" +[[ -d $DESTDIR ]] && die \ +"The destination directory '$DESTDIR' already exists.\ + You should safely delete it before restoring this app." + +# Define app's data directory +DATADIR="/home/yunohost.app/${app}/data" + +# Check configuration files +nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" +[[ -f $nginx_conf ]] && die \ +"The NGINX configuration already exists at '${nginx_conf}'. + You should safely delete it before restoring this app." +phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" +[[ -f $phpfpm_conf ]] && die \ +"The PHP FPM configuration already exists at '${phpfpm_conf}'. + You should safely delete it before restoring this app." # Install dependencies -sudo apt-get update -qq -sudo DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" -y --force-yes -qq install acl smbclient php5-cli php-apc coreutils gnupg tar +ynh_package_install_from_equivs ./owncloud-deps.control \ + || die "Unable to install dependencies" -# Create user if not exists -sudo useradd -d /var/www/$APP $APP || echo "User $APP" +# Create a system account for ownCloud +sudo useradd -c "$app system account" \ + -d /var/lib/$app --system --user-group $app \ + || die "Unable to create $app system account" -# Restore sources & data -sudo cp -a "${backup_dir}/www" $final_path -sudo mkdir -p $data_path -sudo cp -a "${backup_dir}/data/." $data_path +# Restore the app files +sudo cp -a ./www "$DESTDIR" +sudo mkdir -p "$DATADIR" +sudo cp -a ./data/. "$DATADIR" -db_pwd=$(sudo yunohost app setting $APP mysqlpwd) -db_user=$APP -sudo yunohost app initdb $db_user -p $db_pwd -sudo su -c "mysql -u $db_user -p$db_pwd $APP < ${backup_dir}/db.sql" -# TODO Change config.php with potential new database name +# Create and restore the database +ynh_mysql_create_db $dbname $dbuser $dbpass +ynh_mysql_connect_as $dbuser $dbpass $dbname < ./db.sql -# Set permissions -sudo chown -hR $APP:www-data $final_path -sudo chown -hR $APP:www-data $data_path -sudo chown $APP:www-data /home/yunohost.app/$APP -sudo chmod 755 /home/yunohost.app -sudo chmod -R u=rwX,g=rwX,o=rX $final_path -sudo chmod -R u=rwX,g=rwX,o= $data_path -sudo chmod -R 665 $final_path -sudo find $final_path -type d -print0 | xargs -0 sudo chmod 775 \ - || echo "No file to modify" -sudo chmod -R 770 $data_path - -# Set permissions to owncloud directories and /home directories + add Home external storage -for i in $(ls /home) -do - sudo yunohost user list --json | grep -q "\"username\": \"$i\"" && ( - sudo setfacl -m g:$APP:rwx /home/$i || echo "ACL not available" - ) || true +# Iterate over users to extend their home folder permissions - for the external +# storage plugin usage - and create relevant ownCloud directories +for u in $(ynh_user_list); do + sudo mkdir -p "${DATADIR}/${u}" + sudo setfacl -m g:$app:rwx "/home/$u" || true done -# Needed for Jessie/PHP5.6 compatibility -sudo sed -i "s/;always_populate_raw/always_populate_raw/" /etc/php5/cli/php.ini +# Fix app ownerships & permissions +sudo chown -R $app: "$DESTDIR" "$DATADIR" +sudo find ${DESTDIR}/ -type f -print0 | sudo xargs -0 chmod 0644 +sudo find ${DESTDIR}/ -type d -print0 | sudo xargs -0 chmod 0755 +sudo find ${DATADIR}/ -type f -print0 | sudo xargs -0 chmod 0640 +sudo find ${DATADIR}/ -type d -print0 | sudo xargs -0 chmod 0750 +sudo chmod 640 "${DESTDIR}/config/config.php" +sudo chmod 755 /home/yunohost.app -# Restore conf files -sudo cp -a "${backup_dir}/conf/nginx.conf" $conf_nginx -sudo cp -a "${backup_dir}/conf/php-fpm.conf" $conf_fpm -sudo cp -a "${backup_dir}/conf/20-apc.ini" /etc/php5/cli/conf.d/ \ - || sudo cp -a "${backup_dir}/conf/20-apcu.ini" /etc/php5/cli/conf.d/ +# Restore configuration files +sudo cp -a ./conf/nginx.conf "$nginx_conf" +sudo cp -a ./conf/php-fpm.conf "$phpfpm_conf" -# Reload Services -sudo killall php5-fpm -sudo service php5-fpm start -sudo service nginx reload -sudo yunohost app setting $APP unprotected_uris -v "/" -sudo yunohost app ssowatconf - -# Set ssowat config -sudo yunohost app setting $APP unprotected_uris -v "/" -sudo yunohost app ssowatconf +# Reload services +sudo service php5-fpm restart || true +sudo service nginx reload || true