Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Author: Johannes Kraemer
- # Email: johnny@cplusplus-development.de
- #
- # SECURE SERVER TO SERVER TRANSFER VIA SSH2 WITH BLOWFISH ENCRYPTION
- #
- #
- # ---------------------------------------------------------------------------
- # This Script graps a local folder, take a mysqldump from a database and
- # ziped them both to a file. If the file is createt, connect the Script via SSH2
- # to a target Server, takes a backup (files & database) from the target
- # Server and store them on it. If the target machine has done, the local
- # machine starts an upload. After uploading, the Script unziped the new
- # files on the target machine and insert the mysqldump to the database.
- # ---------------------------------------------------------------------------
- #
- # Usage:
- #
- # Set up the SETTINGS Section in this File.
- # Generate & Upload the Auth Keys to the target Server (see AUTHENTIFICATION)
- # Open a Terminal and type in ./srv2srv.sh <-Filename of this script
- #
- # ATENTION!
- #
- # This Script can crash your Production Server,
- # if one or both mysql login be incorect!
- # The Sript copy your Files to the right Place but the Script
- # can not write the new Tables to your Database !!!!!
- #
- #
- #################\
- # SETTINGS +
- #################/
- #
- # MySQL Data from local machine
- localDbUserName='root'
- localDbPassword='totalsecret'
- # MySQL Data from the target machine
- remoteDbUserName='root'
- remoteDbPassword='extremsecret'
- # DatabaseName
- # This value should to be equal on both machines
- localAndRemoteDatabaseName='the_data_base_name'
- # Sourcefolder on local machine. All files & folders within this folder will included
- localProjectFolder='/root/PhpstormProjects/localhost/cplusplus-development.de'
- # SSH Adress from target with username on first
- remoteSSHServerAdress='root@cplusplus-development.de'
- # target folder where the files should be stored
- remoteSSHServerDirectory='/www/htdocs/cplusplus-developement.de'
- # folder+zipfilename for the Backup (do not select the same/recursive folder as the remoteSSHServerDirectory)
- remoteBackupLocation='/www/htdocs/cplusplus-developement.zip'
- # Temporary Filenaem for transfered data
- fileNameForUpdatePackage='concrete5Patch.zip'
- #################\
- # END SETTINGS +
- #################/
- ##################################\
- # AUTHENTIFICATION +
- ##################################/
- #
- #>>>>> CREATE AMD PUT SSH-KEY-PAIR TO THE TARGET SERVER !!!!!
- #
- #>>>>> 1) Run 'ssh-keygen -t rsa' and
- # 'ssh-keygen -t dsa' on the
- # Clientside for generating a Key Pair.
- #
- #>>>>> 2) Run 'ssh-copy-id -i ~/.ssh/id_rsa.pub username@remotehost.com' and
- # 'ssh-copy-id -i ~/.ssh/id_dsa.pub username@remotehost.com'
- # for copy your public key to the target Server.
- # (for first time you need the Password!)
- #
- #>>>>> 3) Now connect with 'ssh user@host.tld' to the Server.
- # If you got an Error Msg (Agent admitted failure to sign using the key),
- # run the 'ssh-add' command.
- #
- #>>>>> 4) Now you can connect to a ssh Server without entering a password!
- # Try it with: 'ssh username@remotehost.com'
- #
- ##################################\
- # END AUTHENTIFICATION +
- ##################################/
- # clear the terminal
- clear
- echo -e "\033[0m\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n~~~ UPDATING PRODUCTION SERVER ~~~\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
- # check if user is superuser
- if [ $UID != '0' ]; then
- echo -e "\033[31mYou need SuperUser rights!\033[32m"
- exit 1
- fi
- sleep 1
- echo -e "\033[34m\n Collecting information from local Server....\n"
- sleep 1
- # change to local project folder
- cd "$localProjectFolder"
- echo -e " ... Prepairing the Database"
- # here you can change values in your database before it copied
- # this is usefull if you have different base pathÅ› or login names ...
- mysql -u "$localDbUserName" -p"$localDbPassword" << EOF
- USE "$localAndRemoteDatabaseName";
- UPDATE Config SET cfValue='$remoteSSHServerDirectory/files' WHERE cfKey='DIR_FILES_UPLOADED';
- EOF
- # read complete database and write it to a file
- mysqldump --databases --opt -Q "-u$localDbUserName" "-p$localDbPassword" "$localAndRemoteDatabaseName" > "$localAndRemoteDatabaseName".sql
- # make changes undo in database
- mysql -u "$localDbUserName" -p"$localDbPassword" << EOF
- USE "$localAndRemoteDatabaseName";
- UPDATE Config SET cfValue='/var/www/cplusplus-development.de/files' WHERE cfKey='DIR_FILES_UPLOADED';
- EOF
- echo -e "\033[34m\n ... Compressing Files on local Machine \033[33m"
- sleep 1
- # compress all Files
- zip -r "$fileNameForUpdatePackage" *
- echo -e "\033[34m\n\n ... Files comressed!\n"
- # delete the mysqldump File from local Machine because we dont need it again
- rm -f "$localAndRemoteDatabaseName".sql
- sleep 2
- #
- 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"
- sleep 2
- # create a backup from Production Database and store it in current cwd
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; mysqldump --databases --opt -Q -u$remoteDbUserName -p$remoteDbPassword $localAndRemoteDatabaseName > $localAndRemoteDatabaseName.sql"
- # zip all Files in cwd and save it as backup
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; zip -r $remoteBackupLocation *"
- # delete old files
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -fr *" #&>/dev/null
- # upload a maintenancemode File and save it as index.php for visitors
- scp -c blowfish "$localProjectFolder/maintenancemode.php" "$remoteSSHServerAdress:$remoteSSHServerDirectory/index.php" &>/dev/null
- # truncate database
- ssh -c blowfish "$remoteSSHServerAdress" "mysql -u $remoteDbUserName -p$remoteDbPassword -D $localAndRemoteDatabaseName -e 'DROP DATABASE $localAndRemoteDatabaseName; CREATE DATABASE $localAndRemoteDatabaseName'"
- echo -e "\n\n... Start uploading new Files and Database to Production Server\n\n"
- # upload archive to Production server
- scp -c blowfish "$localProjectFolder/$fileNameForUpdatePackage" "$remoteSSHServerAdress:$remoteSSHServerDirectory"
- echo -e "\n\n ... Upload complete\n\n ... Delete temporary Archive"
- # delete archive from local machine
- rm -f "$localProjectFolder/$fileNameForUpdatePackage"
- echo -e "\n\n ... Start extracting \"$fileNameForUpdatePackage\" on the Production System"
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; unzip -o $fileNameForUpdatePackage"
- echo -e "\n\n ... File extracting done\n\n ... Store Data from $localAndRemoteDatabaseName.sql into Database"
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; mysql -u $remoteDbUserName -p$remoteDbPassword $localAndRemoteDatabaseName < $localAndRemoteDatabaseName.sql"
- echo -e "\n\n ... Done with insert Tables!\n\n ... Deleting temporary Files"
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -f $fileNameForUpdatePackage; rm -f $localAndRemoteDatabaseName.sql" &>/dev/null
- echo -e "\n\n ... Set spezific Permissions (0777) to some Folders"
- 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
- ssh -c blowfish "$remoteSSHServerAdress" "cd $remoteSSHServerDirectory; rm -f maintenancemode.php" &>/dev/null
- echo -e " ... READY!\n\n ... Your Server should be ROCKZz !\b\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement