Uploading a base on a shared hosting account

From GeneWeb
Jump to: navigation, search


#!/bin/sh
# Version 2, 24/11/2016, Henri
 
# Usage : update-base base_name [ images | src ]
 
# This shell script executes scp and ssh commands with the remote server
# To facilitate these commands, you should install a pair of private/public keys on both machines
# See "how to create a key pair" on the web
 
# If you have not installed your public key on your server, each ssh request will prompt you for a password
 
# An alternative is to supply your password along with the request
# by adding ":password" after username in each occurence of USER@SERVER 
# Be aware that in this case, password is sent in cleartext !!
 
S=tuxf
 
if [ "$S" = "tuxf" ] 
then
  SERVER="ssh.tuxfamily.org"
  HOME_S="/home/geneweb/geneweb-repository/demo"
  USER="aaaaa"
  WUSR="www-data"
  WGRP="www-data"
  P1=""
  P2=""
  BASES_S="/home/geneweb/demo.geneweb.tuxfamily.org-web/htdocs/gw7/bases"    # Bases location on server
  GW_DIR_S="/home/geneweb/demo.geneweb.tuxfamily.org-web/htdocs/gw7/gw"   # Binaries (gwd, gwc, ...) on Server
elif [ "$S" = "ovh" ] 
then
 SERVER="vps664433.ovh.net"
 HOME_S="/root"
 USER="root"
 WUSR="www-data"
 WGRP="www-data"
 P1=""
 P2=""
 BASES_S="~/GW/bases7"               # Bases location on server hgouraud.fr
 GW_DIR_S="~/GW/GeneWeb-7.0x/gw"     # Binaries (gwd, gwc, ...) on Server
elif [ "$S" = "phpn" ] 
then
  SERVER="p5678.phpnet.org"
  HOME_S=""
  USER="p5678"
  WUSR="www-data"
  WGRP="www-data"
  P1="-p 25678"
  P2="-P 25678"
  BASES_S="~/GeneWeb/bases"               # Bases location on server
  GW_DIR_S="~/GeneWeb/geneweb-7.00/gw"    # Binaries (gwd, gwc, ...) on Server
elif [ "$S" = "roglo" ] 
then
  SERVER="roglo.eu"
  HOME_S="/data/wiz/Wizzard"
  USER="Wizzard"
  WUSR="www-data"
  WGRP="www-data"
  P1=""
  P2=""
  BASES_S="/data/wiz/Wizzard/bases-v7"               # Bases location on server
  GW_DIR_S="/data/src/geneweb-7.00/GeneWeb-7.00/gw"    # Binaries (gwd, gwc, ...) on Server
  LOGS="/data/wiz/Wizzard"
else 
  echo "Bad remote server definition"
  exit
fi
 
ADDRESS="my.address@gmail.com"  # to which error report will be sent
LANGUE="fr"
BASE=""
BASES_PC="$HOME/Genea/GeneWeb-Bases"             # Bases location on PC
GW_DIR_PC="$HOME/Genea/GeneWeb/GeneWeb-7.00-Mac/gw"   # Binaries (gwd, gwc, ...) location on PC
 
check_errs()
{
  # Function. Parameter 1 is the return code
  # Para. 2 is text to display on failure.
  if [ "${1}" -ne "0" ]; then
    echo "ERROR # ${1} : ${2}"
    # as a bonus, make our script exit with the right error code.
    exit ${1}
  fi
}
 
# define BASE
BASE=$1
FILES=$2
 
if [ "$BASE" = "" ] || [ "$BASE" = "-h" ] || [ "$BASE" = "-help" ] ; then
  echo "Usage : update-base base_name [ images | src ]"
  echo "    if 'images' or 'src' are present, only the corresponding folders will"
  echo "    be copied in totality to the server"
  exit
fi
 
if [ "$FILES" = "images" ] || [ "$FILES" = "src" ] ; then
  # copy images files to server
  echo "Sending $FILES for $BASE to $SERVER"
  cd $BASES_PC/$FILES
  sed s:BASES_S:$BASES_S:g < $HOME/Genea/remote-files.txt | \
  sed s:BASE:$BASE:g | \
  sed s:HOME_S:$HOME_S:g | \
  sed s:WUSR:$WUSR:g | \
  sed s:WGRP:$WGRP:g | \
  sed s:LOGS:$LOGS:g | \
  sed s:ADDRESS:$ADDRESS:g  > ./remote.sh
  touch ./$FILES.tmp
  tar cf ./$BASE-files.tar $BASE/ ./remote.sh ./$FILES.tmp
  scp $P2 ./$BASE-files.tar $USER@$SERVER:$HOME_S
  ssh $P1 $USER@$SERVER  "cd $HOME_S; tar xf $BASE-files.tar; chmod +x ./remote.sh; ./remote.sh"
  rm -f ./remote.sh
  echo "Done sending $FILES for $BASE to $SERVER"
  exit
fi
 
#mot="o" # default is "oui"
HEB=${HEB:-"ovh"}
# Verify that the images and src/images folder are up to date. 
if [ $LANGUE = "fr" ] ; then
  echo "Cette procédure ne met pas à jour les deux dossiers images"
  #echo "Le message de compte-rendu signale les différences dans les dossiers images du serveur et du PC"
  echo "Vous pouvez cependant continuer."
  echo "Executez ./update-base basename images et/ou ./update-base basename src pour copier ces dossiers"
  echo "Pensez aussi à installer $BASE.gwf sur votre serveur"
  #read -p "Continuer (o/n)[o] :" mot
else
  echo "This procedure does not update the two images folders"
  #echo "It returns through mail a report indicating differences between the server and the PC"
  echo "Run ./update-base basename images and/or ./update-base basename src to upload a fresh copy of these folders."
  echo "You may nonetheless proceed"
  echo "Make sure you install the proper $BASE.gwf parameters on your server"
  #read -p "Proceed? (y/n)[y] :" mot
fi
 
#if [ "$mot" = "y" ] ; then
#  mot="o"
#fi
#mot=${mot:-"o"}
#if [ "$mot" = "n" ] ; then # or "$mot" = "" 
#  exit
#fi
 
echo "Producing a fresh $BASE.gw in $BASES_PC (gwu)"
cd $BASES_PC
$GW_DIR_PC/gwu ./$BASE -o $BASE.gw > comm.log
check_errs $? "Gwu failed on $BASE, see $BASES_PC/comm.log for details "
 
# Take a copy of current change history to send to server
if [ -f $BASES_PC/$BASE.gwb/history ] ; then
  cp -f $BASES_PC/$BASE.gwb/history .
else
  touch ./history
fi
 
# tags.txt is where gwd.arg says it should be !!
if [ -f $GW_DIR_PC/tags.txt ] ; then
  cp -f $GW_DIR_PC/tags.txt .
else
  touch ./tags.txt
fi
 
# Create a content listing for the images and person images folders
cd   $BASES_PC/src/$BASE
ls *.txt > $BASES_PC/ls-src-files.txt
cd   $BASES_PC/src/$BASE/images
ls *.jpg > $BASES_PC/ls-images.txt
cd   $BASES_PC/images/$BASE
ls *.jpg > $BASES_PC/ls-personnes.txt
 
cd $BASES_PC
 
aujourdhui=`date`
sed s/basename/$BASE/g < $BASES_PC/etc/trl-template.txt > $BASES_PC/etc/trl-tmp.txt
sed s/baseversion/"$aujourdhui"/g < $BASES_PC/etc/trl-tmp.txt > $BASES_PC/etc/trl-tmp2.txt
mv $BASES_PC/etc/trl-tmp2.txt $BASES_PC/etc/$BASE/trl.txt
rm $BASES_PC/etc/trl-tmp.txt
cp $BASES_PC/etc/$BASE/trl.txt ./trl.txt
 
# Build a command file to be executed on the server
sed s:GW_DIR_S:$GW_DIR_S:g < $HOME/Genea/remote-base.txt | \
sed s:BASES_S:$BASES_S:g | \
sed s:BASE:$BASE:g | \
sed s:HOME_S:$HOME_S:g | \
sed s:WUSR:$WUSR:g | \
sed s:WGRP:$WGRP:g | \
sed s:LOGS:$LOGS:g | \
sed s:ADDRESS:$ADDRESS:g > ./remote.sh
 
# suppressed trl.txt
echo "Building remote.tar (./remote.sh ./$BASE.gw ./history ./ls-personnes.txt ./ls-images.txt ./ls-src-files.txt ./tags.txt)"
tar cf ./$BASE.tar         ./remote.sh ./$BASE.gw ./history ./ls-personnes.txt ./ls-images.txt ./ls-src-files.txt ./tags.txt 
 
echo "Sending remote.tar to $SERVER"
scp $P2 ./$BASE.tar $USER@$SERVER:$HOME_S
echo "$BASE.tar sent"
# Execute a remote command on the server
#     un-tar the file just sent, make remote.sh executable and execute it
ssh $P1 $USER@$SERVER "cd $HOME_S; tar xf $BASE.tar; chmod +x ./remote.sh; ./remote.sh"
echo "Tar xf done; remote.sh executed"
 
echo "Cleanup (on local machine)"
rm -f ./$BASE.tar ./remote.* ./history ./ls-*.txt ./tags.txt ./._tags.txt ./trl.txt 
 
echo "Done updating $BASE on $SERVER"