Advertisement
Guest User

Untitled

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