initdemo.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/sh
  2. #------------------------------------------------------
  3. # Script to purge and init a database with demo values.
  4. # Note: "dialog" tool need to be available if no parameter provided.
  5. #
  6. # WARNING: This script erase all data of database
  7. # with data into dump file
  8. #
  9. # Regis Houssin - regis.houssin@inodbox.com
  10. # Laurent Destailleur - eldy@users.sourceforge.net
  11. #------------------------------------------------------
  12. # Usage: initdemo.sh confirm
  13. # usage: initdemo.sh confirm mysqldump_dolibarr_x.x.x.sql database port login pass
  14. #------------------------------------------------------
  15. export mydir=`echo "$0" | sed -e 's/initdemo.sh//'`;
  16. if [ "x$mydir" = 'x' -o "x$mydir" = 'x./' ]
  17. then
  18. export mydir="."
  19. fi
  20. export id=`id -u`;
  21. # ----------------------------- check if root
  22. if [ "x$id" != "x0" -a "x$id" != "x1001" ]
  23. then
  24. echo "Script must be ran as root"
  25. exit
  26. fi
  27. # ----------------------------- command line params
  28. confirm=$1;
  29. dumpfile=$2;
  30. base=$3;
  31. port=$4;
  32. admin=$5;
  33. passwd=$6;
  34. # ----------------------------- check params
  35. if [ "x$confirm" != "xconfirm" ]
  36. then
  37. echo "----- $0 -----"
  38. echo "Usage: initdemo.sh confirm [mysqldump_dolibarr_x.x.x.sql database port login pass]"
  39. exit
  40. fi
  41. # ----------------------------- if no params on command line
  42. if [ "x$passwd" = "x" ]
  43. then
  44. export dumpfile=`ls -v $mydir/mysqldump_dolibarr_*.sql | tail -n 1`
  45. export dumpfile=`basename $dumpfile`
  46. # ----------------------------- input file
  47. DIALOG=${DIALOG=dialog}
  48. DIALOG="$DIALOG --ascii-lines"
  49. fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$
  50. trap "rm -f $fichtemp" 0 1 2 5 15
  51. $DIALOG --title "Init Dolibarr with demo values" --clear \
  52. --inputbox "Input dump file :" 16 55 $dumpfile 2> $fichtemp
  53. valret=$?
  54. case $valret in
  55. 0)
  56. dumpfile=`cat $fichtemp`;;
  57. 1)
  58. exit;;
  59. 255)
  60. exit;;
  61. esac
  62. rm $fichtemp
  63. # ----------------------------- database name
  64. DIALOG=${DIALOG=dialog}
  65. DIALOG="$DIALOG --ascii-lines"
  66. fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$
  67. trap "rm -f $fichtemp" 0 1 2 5 15
  68. $DIALOG --title "Init Dolibarr with demo values" --clear \
  69. --inputbox "Mysql database name :" 16 55 dolibarrdemo 2> $fichtemp
  70. valret=$?
  71. case $valret in
  72. 0)
  73. base=`cat $fichtemp`;;
  74. 1)
  75. exit;;
  76. 255)
  77. exit;;
  78. esac
  79. rm $fichtemp
  80. # ---------------------------- database port
  81. DIALOG=${DIALOG=dialog}
  82. fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$
  83. trap "rm -f $fichtemp" 0 1 2 5 15
  84. $DIALOG --title "Init Dolibarr with demo values" --clear \
  85. --inputbox "Mysql port (ex: 3306):" 16 55 3306 2> $fichtemp
  86. valret=$?
  87. case $valret in
  88. 0)
  89. port=`cat $fichtemp`;;
  90. 1)
  91. exit;;
  92. 255)
  93. exit;;
  94. esac
  95. rm $fichtemp
  96. # ---------------------------- compte admin mysql
  97. DIALOG=${DIALOG=dialog}
  98. fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$
  99. trap "rm -f $fichtemp" 0 1 2 5 15
  100. $DIALOG --title "Init Dolibarr with demo values" --clear \
  101. --inputbox "Mysql user login (ex: root):" 16 55 root 2> $fichtemp
  102. valret=$?
  103. case $valret in
  104. 0)
  105. admin=`cat $fichtemp`;;
  106. 1)
  107. exit;;
  108. 255)
  109. exit;;
  110. esac
  111. rm $fichtemp
  112. # ---------------------------- mot de passe admin mysql
  113. DIALOG=${DIALOG=dialog}
  114. fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$
  115. trap "rm -f $fichtemp" 0 1 2 5 15
  116. $DIALOG --title "Init Dolibarr with demo values" --clear \
  117. --passwordbox "Password for Mysql user login :" 16 55 2> $fichtemp
  118. valret=$?
  119. case $valret in
  120. 0)
  121. passwd=`cat $fichtemp`;;
  122. 1)
  123. exit;;
  124. 255)
  125. exit;;
  126. esac
  127. rm $fichtemp
  128. export documentdir=`cat $mydir/../../htdocs/conf/conf.php | grep '^\$dolibarr_main_data_root' | sed -e 's/$dolibarr_main_data_root=//' | sed -e 's/;//' | sed -e "s/'//g" | sed -e 's/"//g' `
  129. # ---------------------------- confirmation
  130. DIALOG=${DIALOG=dialog}
  131. $DIALOG --title "Init Dolibarr with demo values" --clear \
  132. --yesno "Do you confirm ? \n Dump file : '$dumpfile' \n Dump dir : '$mydir' \n Document dir : '$documentdir' \n Mysql database : '$base' \n Mysql port : '$port' \n Mysql login: '$admin' \n Mysql password : --hidden--" 15 55
  133. case $? in
  134. 0) echo "Ok, start process...";;
  135. 1) exit;;
  136. 255) exit;;
  137. esac
  138. fi
  139. # ---------------------------- run sql file
  140. if [ "x$passwd" != "x" ]
  141. then
  142. export passwd="-p$passwd"
  143. fi
  144. #echo "mysql -P$port -u$admin $passwd $base < $mydir/$dumpfile"
  145. #mysql -P$port -u$admin $passwd $base < $mydir/$dumpfile
  146. #echo "drop old table"
  147. echo "drop table if exists llx_accounting_account;" | mysql -P$port -u$admin $passwd $base
  148. echo "mysql -P$port -u$admin -p***** $base < $mydir/$dumpfile"
  149. mysql -P$port -u$admin $passwd $base < $mydir/$dumpfile
  150. export res=$?
  151. if [ $res -ne 0 ]; then
  152. echo "Error to load database dump with mysql -P$port -u$admin -p***** $base < $mydir/$dumpfile"
  153. exit
  154. fi
  155. $mydir/updatedemo.php confirm
  156. export res=$?
  157. # ---------------------------- copy demo files
  158. export documentdir=`cat $mydir/../../htdocs/conf/conf.php | grep '^\$dolibarr_main_data_root' | sed -e 's/$dolibarr_main_data_root=//' | sed -e 's/;//' | sed -e "s/'//g" | sed -e 's/"//g' `
  159. if [ "x$documentdir" != "x" ]
  160. then
  161. $DIALOG --title "Reset document directory" --clear \
  162. --inputbox "DELETE and recreate document directory $documentdir/:" 16 55 n 2> $fichtemp
  163. valret=$?
  164. case $valret in
  165. 0)
  166. rep=`cat $fichtemp`;;
  167. 1)
  168. exit;;
  169. 255)
  170. exit;;
  171. esac
  172. echo "rep=$rep"
  173. if [ "x$rep" = "xy" ]; then
  174. echo rm -fr "$documentdir/*"
  175. rm -fr $documentdir/*
  176. fi
  177. echo cp -pr $mydir/documents_demo/* "$documentdir/"
  178. cp -pr $mydir/documents_demo/* "$documentdir/"
  179. mkdir "$documentdir/doctemplates/" 2>/dev/null
  180. echo cp -pr $mydir/../../htdocs/install/doctemplates/* "$documentdir/doctemplates/"
  181. cp -pr $mydir/../../htdocs/install/doctemplates/* "$documentdir/doctemplates/"
  182. echo cp -pr $mydir/../../htdocs/install/medias/* "$documentdir/medias/image/"
  183. cp -pr $mydir/../../htdocs/install/medias/* "$documentdir/medias/image/"
  184. mkdir -p "$documentdir/ecm/Administrative documents" 2>/dev/null
  185. mkdir -p "$documentdir/ecm/Images" 2>/dev/null
  186. rm -f "$documentdir/doctemplates/"*/index.html
  187. echo cp -pr $mydir/../../doc/images/* "$documentdir/ecm/Images"
  188. cp -pr $mydir/../../doc/images/* "$documentdir/ecm/Images"
  189. chmod -R u+w "$documentdir/"
  190. chown -R www-data "$documentdir/"
  191. else
  192. echo Detection of documents directory from $mydir failed so demo files were not copied.
  193. fi
  194. if [ -s "$mydir/initdemopostsql.sql" ]; then
  195. mysql -P$port $base < "$mydir/initdemopostsql.sql"
  196. fi
  197. if [ "x$res" = "x0" ]
  198. then
  199. echo "Success, file successfully loaded."
  200. else
  201. echo "Error, load failed."
  202. fi
  203. echo