Advertisement
Blade83

SECURE SRV 2 SRV TRANSFER (SSH2 & BLOWFISH)

Jun 20th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.54 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. #   Author: Johannes Kraemer
  4. #   Email:  johnny@cplusplus-development.de
  5. #  
  6. #   SECURE SERVER TO SERVER TRANSFER VIA SSH2 WITH BLOWFISH ENCRYPTION
  7. #  
  8. #
  9. # ---------------------------------------------------------------------------
  10. # This Script graps a local folder, take a mysqldump from a database and
  11. # ziped them both to a file. If the file is createt, connect the Script via SSH2
  12. # to a target Server, takes a backup (files & database) from the target
  13. # Server and store them on it. If the target machine has done, the local
  14. # machine starts an upload. After uploading, the Script unziped the new
  15. # files on the target machine and insert the mysqldump to the database.
  16. # ---------------------------------------------------------------------------
  17. #
  18. # Usage:
  19. #
  20. #   Set up the SETTINGS Section in this File.
  21. #   Generate & Upload the Auth Keys to the target Server (see AUTHENTIFICATION)
  22. #   Open a Terminal and type in ./srv2srv.sh  <-Filename of this script
  23. #
  24. # ATENTION!
  25. #
  26. #   This Script can crash your Production Server,
  27. #   if one or both mysql login be incorect!
  28. #   The Sript copy your Files to the right Place but the Script
  29. #   can not write the new Tables to your Database !!!!!
  30. #
  31. #
  32. #################\
  33. #    SETTINGS    +
  34. #################/
  35. #
  36. # MySQL Data from local machine
  37. localDbUserName='root'
  38. localDbPassword='totalsecret'
  39. # MySQL Data from the target machine
  40. remoteDbUserName='root'
  41. remoteDbPassword='extremsecret'
  42. # DatabaseName
  43. # This value should to be equal on both machines
  44. localAndRemoteDatabaseName='the_data_base_name'
  45. # Sourcefolder on local machine. All files & folders within this folder will included
  46. localProjectFolder='/root/PhpstormProjects/localhost/cplusplus-development.de'
  47. # SSH Adress from target with username on first
  48. remoteSSHServerAdress='root@cplusplus-development.de'
  49. # target folder where the files should be stored
  50. remoteSSHServerDirectory='/www/htdocs/cplusplus-developement.de'
  51. # folder+zipfilename for the Backup (do not select the same/recursive folder as the remoteSSHServerDirectory)
  52. remoteBackupLocation='/www/htdocs/cplusplus-developement.zip'
  53. # Temporary Filenaem for transfered data
  54. fileNameForUpdatePackage='concrete5Patch.zip'
  55. #################\
  56. #  END SETTINGS  +
  57. #################/
  58.  
  59.  
  60. ##################################\
  61. #        AUTHENTIFICATION         +
  62. ##################################/
  63. #
  64. #>>>>>  CREATE AMD PUT SSH-KEY-PAIR TO THE TARGET SERVER !!!!!
  65. #
  66. #>>>>>  1) Run 'ssh-keygen -t rsa'  and
  67. #              'ssh-keygen -t dsa'  on the
  68. #          Clientside for generating a Key Pair.
  69. #
  70. #>>>>>  2) Run 'ssh-copy-id -i ~/.ssh/id_rsa.pub username@remotehost.com' and
  71. #              'ssh-copy-id -i ~/.ssh/id_dsa.pub username@remotehost.com'
  72. #          for copy your public key to the target Server.
  73. #          (for first time you need the Password!)
  74. #
  75. #>>>>>  3) Now connect with 'ssh user@host.tld' to the Server.
  76. #          If you got an Error Msg (Agent admitted failure to sign using the key),
  77. #          run the 'ssh-add' command.
  78. #
  79. #>>>>>  4) Now you can connect to a ssh Server without entering a password!
  80. #          Try it with: 'ssh username@remotehost.com'
  81. #
  82. ##################################\
  83. #       END AUTHENTIFICATION      +
  84. ##################################/
  85.  
  86.  
  87. # clear the terminal
  88. clear
  89. echo -e "\033[0m\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n~~~ UPDATING PRODUCTION SERVER ~~~\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  90. # check if user is superuser
  91. if [ $UID != '0' ]; then
  92.         echo -e "\033[31mYou need SuperUser rights!\033[32m"
  93.         exit 1
  94. fi
  95. sleep 1
  96. echo -e "\033[34m\n Collecting information from local Server....\n"
  97. sleep 1
  98. # change to local project folder
  99. cd "$localProjectFolder"
  100. echo -e " ... Prepairing the Database"
  101. # here you can change values in your database before it copied
  102. # this is usefull if you have different base pathÅ› or login names ...
  103. mysql -u "$localDbUserName" -p"$localDbPassword" << EOF
  104.   USE "$localAndRemoteDatabaseName";
  105.   UPDATE Config SET cfValue='$remoteSSHServerDirectory/files' WHERE cfKey='DIR_FILES_UPLOADED';
  106. EOF
  107. # read complete database and write it to a file
  108. mysqldump --databases --opt -Q "-u$localDbUserName" "-p$localDbPassword" "$localAndRemoteDatabaseName" > "$localAndRemoteDatabaseName".sql
  109. # make changes undo in database
  110. mysql -u "$localDbUserName" -p"$localDbPassword" << EOF
  111.   USE "$localAndRemoteDatabaseName";
  112.   UPDATE Config SET cfValue='/var/www/cplusplus-development.de/files' WHERE cfKey='DIR_FILES_UPLOADED';
  113. EOF
  114. echo -e "\033[34m\n ... Compressing Files on local Machine \033[33m"
  115. sleep 1
  116. # compress all Files
  117. zip -r "$fileNameForUpdatePackage" *
  118. echo -e "\033[34m\n\n ... Files comressed!\n"
  119. # delete the mysqldump File from local Machine because we dont need it again
  120. rm -f "$localAndRemoteDatabaseName".sql
  121. sleep 2
  122.  
  123. #
  124. echo -e " ... Connect to Production Server \"$remoteSSHServerAdress\"\n\n ... Change cwd \"$remoteSSHServerDirectory\"\n\n ... Start Database and File Backup from Production Server and store it in \"$remoteBackupLocation\"\n\n"
  125. sleep 2
  126. # create a backup from Production Database and store it in current cwd
  127. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; mysqldump --databases --opt -Q -u$remoteDbUserName -p$remoteDbPassword $localAndRemoteDatabaseName > $localAndRemoteDatabaseName.sql"
  128. # zip all Files in cwd and save it as backup
  129. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; zip -r $remoteBackupLocation *"
  130. # delete old files
  131. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -fr *" #&>/dev/null
  132. # upload a maintenancemode File and save it as index.php for visitors
  133. scp -c blowfish "$localProjectFolder/maintenancemode.php" "$remoteSSHServerAdress:$remoteSSHServerDirectory/index.php" &>/dev/null
  134. # truncate database
  135. ssh -c blowfish "$remoteSSHServerAdress" "mysql -u $remoteDbUserName -p$remoteDbPassword -D $localAndRemoteDatabaseName -e 'DROP DATABASE $localAndRemoteDatabaseName; CREATE DATABASE $localAndRemoteDatabaseName'"
  136.  
  137.  
  138. echo -e "\n\n... Start uploading new Files and Database to Production Server\n\n"
  139. # upload archive to Production server
  140. scp -c blowfish "$localProjectFolder/$fileNameForUpdatePackage" "$remoteSSHServerAdress:$remoteSSHServerDirectory"
  141. echo -e "\n\n ... Upload complete\n\n ... Delete temporary Archive"
  142. # delete archive from local machine
  143. rm -f "$localProjectFolder/$fileNameForUpdatePackage"
  144.  
  145.  
  146. echo -e "\n\n ... Start extracting \"$fileNameForUpdatePackage\" on the Production System"
  147. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; unzip -o $fileNameForUpdatePackage"
  148. echo -e "\n\n ... File extracting done\n\n ... Store Data from $localAndRemoteDatabaseName.sql into Database"
  149. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; mysql -u $remoteDbUserName -p$remoteDbPassword $localAndRemoteDatabaseName < $localAndRemoteDatabaseName.sql"
  150.  
  151. echo -e "\n\n ... Done with insert Tables!\n\n ... Deleting temporary Files"
  152. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -f $fileNameForUpdatePackage; rm -f $localAndRemoteDatabaseName.sql" &>/dev/null
  153.  
  154. echo -e "\n\n ... Set spezific Permissions (0777) to some Folders"
  155. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; chmod 0777 -R blocks/; chmod 0777 -R config/; chmod 0777 -R files/; chmod 0777 -R packages/; chmod 0777 -R updates/" #&>/dev/null
  156. ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -f maintenancemode.php" &>/dev/null
  157.  
  158. echo -e " ... READY!\n\n ... Your Server should be ROCKZz !\b\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement