Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.67 KB | None | 0 0
  1. function f_isempty {
  2. if [ "x$1" = "x" ]; then
  3.     echo "This can't be left blank, please retry filling it"
  4.     sleep 2
  5.     return 1
  6. fi
  7. }
  8.  
  9. function f_ping {
  10. fonction="f_ping"
  11. f_debug $fonction
  12. if [ "$pingtest" = "1" ]; then
  13.     if ping -c 1 -w 1 -q $1 </dev/null &>/dev/null; then
  14.         echo "$1 Answers to pings: GOOD."
  15.         return 1
  16.     else
  17.         date=`date +%Y%m%d`
  18.         echo "$1 doesn't answer to ping on $date !!! " >> $pwd_init/log/error.log
  19.         return 2
  20.     fi
  21. else
  22.     echo "ping test disabled."
  23.     return 0
  24. fi
  25. }
  26. function f_genQL {
  27. # This function generates the filetree to be uploaded on the server, the key auth
  28. # mechanism and the ".htaccess" and ".htpasswd" files.
  29. # !!! You should check those files before uploading them on the server
  30. fonction="f_genQL"
  31. f_debug $fonction
  32. ########################################
  33. # <DATA RECOLLECTION>
  34. echo "What is the site's dns (compulsary)"
  35. read dns
  36. f_isempty $dns
  37. if [ "x$?" = "x1" ]; then
  38.     return 0
  39. fi
  40. if [ "`cat $datfile | grep $dns`" != '' ]; then
  41.     echo "$dns is allready in $datfile, please check and try again!"
  42.     return 0
  43. fi
  44. f_ping $dns
  45. if [ "x$?" = "x2" ]; then
  46.     echo "$dns does not reply to ping, are you sure you want to add it? (y or n)"
  47.     read yn
  48.     if [ "x$yn" != "xy" ]; then
  49.         return 0
  50.     fi
  51. fi
  52. mkdir -p var/$dns/$genQL__dir var/keys
  53. echo "Which protocol does the site use (ftp or defaults as ssh)"
  54. read protocol
  55. if [ "x$protocol" != "xftp" ]; then
  56.     protocol="ssh"
  57. fi
  58. echo "What is your login for that protocol (compulsary)"
  59. read l0gin
  60. f_isempty $l0gin
  61. if [ "x$protocol" = "xftp" ]; then
  62.     echo "What is your ftp password"
  63.     read ftpassword
  64.     echo "On which port does the ftp server listen (defaults to 21)?"
  65.     read $port
  66.     if [ "x$port" = "x" ]; then
  67.         port="21"
  68.     fi
  69. elif [ "x$protocol" = "xssh" ]; then
  70.     echo "On which port does the ssh server listen (defaults to 22)?"
  71.     read port
  72.     if [ "x$port" = "" ]; then
  73.     port="x22"
  74.     fi
  75.     echo "Do you already have a shared key on that server? (y or n)"
  76.     read sharedkey
  77.         if [ "x$sharedkey" = "xy" ]; then
  78.             ls var/keys/|cat -n
  79.             echo "Which is the key that should be used?"
  80.             read key
  81.             sshkeyname=`ls var/key | sed -n "$key"p`
  82.         elif [ "x$sharedkey" = "xn" ]; then
  83.             echo "What should be the key's name (!avoid a name that's allready in use!)"
  84.             read sshkeyname
  85.             for i in `ls var/keys/`
  86.                 do
  87.                 if [ "x$i" = "x$sshkeyname" ]; then
  88.                     echo "$i is allready in use as ssh keyname"
  89.                     return 1
  90.                 fi
  91.                 done
  92.             echo "Patience ...  $rsabits bits RSA keyset is being generated"
  93.             ssh-keygen -b $rsabits -t rsa -f key.$sshkeyname
  94.             mv key.$sshkeyname var/keys/
  95.             mv key.$sshkeyname.pub var/$dns/
  96.         else
  97.             echo "y or n ... Please try again"
  98.             return 1
  99.         fi
  100. else
  101.     echo "ftp or ssh ... Please try again"
  102.     return 1
  103. fi
  104. echo "What is your alternative dns (defaults as site's dns)"
  105. read altdns
  106. if [ "x$altdns" = "x" ]; then
  107.     altdns=$dns && echo "Alternative dns set to $altdns"
  108. fi
  109. echo "What's the site's path on the server?"
  110. echo "!!Start at / for ssh and at ~ for ftp!!"
  111. read rpath
  112. f_isempty $rpath
  113. echo "At what time should the site be backed up? (defaults random)"
  114. read timesite
  115. if [ "x$timesite" = "x" ]; then
  116.     f_randomhour
  117.     timesite="$R" && echo "Time Site set to $timesite"
  118. fi
  119. echo "On what day(s) should the site be backed up? (defaults random)"
  120. read daysite
  121. if [ "$xdaysite" = "x" ]; then
  122.     f_randomday
  123.     daysite="$d" && echo "Day Site set to $daysite"
  124. fi
  125. echo "At what time should the db be backed up? (defaults random)"
  126. read timedb
  127. if [ "x$timedb" = "x" ]; then
  128.     f_randomhour
  129.     timedb="$R" && echo "Time db set to $timedb"
  130. fi
  131. echo "On what day(s) should the db be backed up? (defaults every day)"
  132. read daydb
  133. if [ "x$daydb" = "x" ]; then
  134.     daydb="Mon-Tue-Wed-Thu-Fri-Sat-Sun" && echo "Day db set to $daydb"
  135. fi
  136. echo "Site's coefficient (default 1 - will be used to define disk usage)"
  137. read coef
  138. if [ "x$coef" = "x" ]; then
  139.     coef="1"
  140. fi
  141. echo "Site Allowance in Gigabite (default 1)"
  142. read GB
  143. if [ "x$GB" = "x" ]; then
  144.     GB="1"
  145. fi
  146. echo "Site's priority (from 0 to 10 ; smallest are done sooner - Default 5)"
  147. read priority
  148. if [ "x$priority" = "x" ]; then
  149.     priority="5"
  150. fi
  151. echo "Is the site active (put 0 for no ; defaults as 1)"
  152. read active
  153. if [ "x$active" = "x" ]; then
  154.     active="1"
  155. fi
  156. echo "Please enter the database server's name (defaults to localhost)"
  157. read dbserv
  158. if [ "x$dbserv" = "x" ]; then
  159.     dbserv='localhost'
  160. fi
  161. echo "Please enter the name of the database:"
  162. read db
  163. f_isempty $db
  164. echo "Please enter the name of that database's user:"
  165. read user
  166. f_isempty $user
  167. echo "Please enter the corresponding password:"
  168. read pass
  169. f_isempty $pass
  170. # </DATA RECOLLECTION>
  171. ########################################
  172. # <FILE GENERATION>
  173. echo "Data Recollection complete, adding config to $datfile and preparing files to upload..."
  174. touch $datfile
  175. mkdir -p var/$dns/$genQL__dir/mysql
  176. #index.php
  177. echo '<?' > var/$dns/$genQL__dir/index.php
  178. echo \$rep_backup\ \=\ \'.\/mysql\/\'\; >> var/$dns/$genQL__dir/index.php
  179. echo '$heure_j = date("H-i");' >> var/$dns/$genQL__dir/index.php
  180. echo '$date_j = date("Ymd");' >> var/$dns/$genQL__dir/index.php
  181. echo \$heure_j\ \=\ str_replace\(\'-\'\,\ \'H\'\,\ \$heure_j\)\; >> var/$dns/$genQL__dir/index.php
  182. echo '$filename = '$db'."-".$date_j."-".$heure_j.".sql";' >> var/$dns/$genQL__dir/index.php
  183. echo 'echo "Your DB is being backed up<p>";' >> var/$dns/$genQL__dir/index.php
  184. echo 'system("'"mysqldump --host=$dbserv --user=$user --password=$pass -C -Q -e --default-character-set=utf8 $db | gzip -c > mysql/\$filename.gz"'");' >> var/$dns/$genQL__dir/index.php
  185. echo 'echo "Done, you can now recover the backup";' >> var/$dns/$genQL__dir/index.php
  186. echo "?>" >> var/$dns/$genQL__dir/index.php
  187. #majeur.php
  188. echo \<\?php > var/$dns/$genQL__dir/majeur.php
  189. echo echo \"Your database is being restored ...... >> var/$dns/$genQL__dir/majeur.php
  190. echo \<br\>\"\; >> var/$dns/$genQL__dir/majeur.php
  191. echo system\(\"cat mysql\/notsuperdb.sql \| mysql --host\=$dbserv --user\=$user --password\=$pass --default-character-set\=utf8 $db\"\)\; >> var/$dns/$genQL__dir/majeur.php
  192. echo echo \"Done, your database has been restored on this hosting.\"\; >> var/$dns/$genQL__dir/majeur.php
  193. echo \?\> >> var/$dns/$genQL__dir/majeur.php
  194. #Les .htaccess et .htpasswd
  195. echo "AuthUserFile "$rpath"/$genQL__dir/.htpasswd" >> var/$dns/$genQL__dir/.htaccess
  196. echo "AuthGroupFile /dev/null" >> var/$dns/$genQL__dir/.htaccess
  197. echo 'AuthName "Restraint Access"' >> var/$dns/$genQL__dir/.htaccess
  198. echo "AuthType Basic" >> var/$dns/$genQL__dir/.htaccess
  199. echo "<Limit GET POST>" >> var/$dns/$genQL__dir/.htaccess
  200. echo "require valid-user" >> var/$dns/$genQL__dir/.htaccess
  201. echo "</Limit>" >> var/$dns/$genQL__dir/.htaccess
  202. loginhtacces=`head -c $htstrength < /dev/urandom | uuencode -m - | tail -n 2 | head -n 1`
  203. passhtaccess=`head -c $htstrength < /dev/urandom | uuencode -m - | tail -n 2 | head -n 1`
  204. htpasswd -bc htpassword $loginhtacces $passhtaccess
  205. #echo "$htaccess__user:$htaccess__pwd_crptd">>$genQL__dir/.htpasswd
  206. touch var/$dns/$genQL__dir/index.html
  207. echo "AuthUserFile "$rpath"/$genQL__dir/mysql/.htpasswd" >> var/$dns/$genQL__dir/mysql/.htaccess
  208. echo "AuthGroupFile /dev/null" >> var/$dns/$genQL__dir/mysql/.htaccess
  209. echo 'AuthName "Restraint Access"' >> var/$dns/$genQL__dir/mysql/.htaccess
  210. echo "AuthType Basic" >> var/$dns/$genQL__dir/mysql/.htaccess
  211. echo "<Limit GET POST>" >> var/$dns/$genQL__dir/mysql/.htaccess
  212. echo "require valid-user" >> var/$dns/$genQL__dir/mysql/.htaccess
  213. echo "</Limit>" >> var/$dns/$genQL__dir/mysql/.htaccess
  214. touch var/$dns/$genQL__dir/mysql/.htpasswd
  215. touch var/$dns/$genQL__dir/mysql/index.html
  216. echo "$dns;$active;$protocol;$l0gin;$ftpassword;$sshkeyname;$loginhtacces;$passhtaccess;$altdns;$port;$rpath;$timesite;$daysite;$timedb;$daydb;$coef;$GB;$priority" >> $datfile
  217. # </FILE GENERATION>
  218. ########################################
  219. # Upload Files
  220. #scp -p $port var/$dns/$dns.pub $l0gin@:~/.ssh/authorized_keys
  221. #####################################
  222. #Should the script upload the files?
  223. #if [ x$protocol = "xftp" ]; then
  224. #       echo "patience, uploading!"
  225. #       lftp -c "open ftp.$b && user $c $d && cd www && mirror --reverse --delete var/$dns/$genQL__dir var/$dns/$genQL__dir" 2>>$pwd_init/log/error.log && echo "genQL is in place on $b"
  226. #   elif [ "$xservice" = "xssh" ]; then
  227. #       echo "patience, uploading!"
  228. #       rsync -qaEzc -e ssh var/$dns/$genQL__dir root@$sme__domain:$sme__basedir/$site/html/ 2>>$pwd_init/log/error.log && echo "$genQL__dir is in place on $site"
  229. #       ssh $sme__domain -l root "chown -R www:clients /home/e-smith/files/ibays/$site/html/" && echo "chown done"
  230. #   OR  rsync -qaEzc -e ssh var/$dns/$genQL__dir root@$site:/home/sites/$site/web/ 2>>$pwd_init/log/error.log && echo "$genQL__dir is in place on $site"
  231. #       andthenchownthefiles
  232. #       rep=`ssh $site -l root ls -l /home/sites/|grep $site | cut -f 4 -d"/"`
  233. #       ssh $site -l root "chown -R apache:$rep /home/sites/$site/web/" && echo "chown done"
  234. # Eapeasy ! but so far, just:
  235. tar -czf var/$dns.tar.gz var/$dns && rm -Rf var/$dns && echo "########################################" && echo "Everything has been generated as should be, you can now proceed to upload"
  236. echo "/var/$dns.tar.gz in order to place the $genQL__dir"
  237. if [ "$xprotocol" = "xssh" ]; then
  238.     if [ "x$sharedkey" = "xn" ]; then
  239.         echo "key.$sshkeyname in the ~/.ssh/authorized_keys"
  240.     fi
  241. fi
  242. echo "########################################"
  243. #Don't forget to put the right right on database with "mysql -u root -p"
  244. # REVOKE ALL PRIVILEGES ON `dbname` . * FROM 'dbuser'@'localhost';
  245. # GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON `dbname` . * TO 'dbuser'@'localhost';
  246. }
  247. ##################################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement