Guest User

Untitled

a guest
Dec 11th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Variables
  3. DB_FILENAME=
  4. LOCAL_DIR=
  5. LOCAL_UPLOADS_DIR=
  6. SERVER_DIR=
  7. SERVER_UPLOADS_DIR=
  8. SERVER_THEME_DIR=
  9. LOCAL_DB_PASS=
  10. LOCAL_DB_USER=
  11. LOCAL_DB=
  12. LOCAL_URL=
  13. SERVER_DB_PASS=
  14. SERVER_DB_USER=
  15. SERVER_DB=
  16. SERVER_URL=
  17. SERVER_USERNAME=
  18. SERVER=
  19.  
  20. while getopts a: option; do
  21. case $option in
  22. a) action=$OPTARG;;
  23. esac
  24. done
  25.  
  26. if [ $action = 'push' ]
  27. then
  28. echo Dumping the localhost database
  29. mysqldump -u "${LOCAL_DB_USER}" -p"${LOCAL_DB_PASS}" --socket '/Applications/MAMP/tmp/mysql/mysql.sock' "${LOCAL_DB}" > "${LOCAL_DIR}${DB_FILENAME}"
  30.  
  31. # Find and replace the local hostname with the remote hostname
  32. # sed: stream editor
  33. # --in-place: perform find and replace in-place (on the same file)
  34. # .original: sed will make a backup of the original file with that extension appended. The extension can be anything.
  35. # If you type '' (empty string) as the extension, sed does not make a backup of the modified file.
  36. # s: substitute command (substitute old string with new string)
  37. # g: Global command (substitute everywhere in the file you see the regular expression
  38. # | delimiters of the regular expressions
  39. echo Performing find and replace on the dumped database
  40. sed -i.original "s|${LOCAL_URL}|${SERVER_URL}|g" "${LOCAL_DIR}${DB_FILENAME}"
  41.  
  42. echo Using rsync to transfer the db export file to the remote server
  43. rsync -rv -e 'ssh' "${LOCAL_DIR}${DB_FILENAME}" "${SERVER_USERNAME}@${SERVER}:${SERVER_DIR}"
  44.  
  45. # Transfer all the files in uploads to the remote server
  46. # A trailing / on a source name means "copy the contents of this directory". Without a trailing slash it means "copy the directory".
  47. rsync -rv -e 'ssh' "${LOCAL_UPLOADS_DIR}" "${SERVER_USERNAME}@${SERVER}:${SERVER_UPLOADS_DIR}"
  48.  
  49. # Commands to perform on the server via SSH
  50. COMMANDS="
  51.  
  52. # Import .sql file into database
  53. mysql -u ${SERVER_DB_USER} -p'${SERVER_DB_PASS}' ${SERVER_DB} < ${SERVER_DIR}${DB_FILENAME};
  54.  
  55. # Navigate to the theme directory
  56. cd ${SERVER_THEME_DIR};
  57.  
  58. # Pull code from Git repository
  59. git pull origin master;
  60.  
  61. exit"
  62.  
  63. # SSH onto the Domains server (using an SSH key for authentication)
  64. ssh "${SERVER_USERNAME}@${SERVER}" "${COMMANDS}"
  65. else
  66. # Commands to perform on the server via SSH
  67. COMMANDS="
  68.  
  69. # Dump the server database
  70. mysqldump -u ${SERVER_DB_USER} -p'${SERVER_DB_PASS}' ${SERVER_DB} > ${SERVER_DIR}${DB_FILENAME};
  71.  
  72. # Perform find and replace on the database dump
  73. sed -i.original 's|${SERVER_URL}|${LOCAL_URL}|g' ${SERVER_DIR}${DB_FILENAME}
  74.  
  75. exit"
  76.  
  77. # SSH onto the Domains server and perform the commands (using an SSH key for authentication)
  78. ssh "${SERVER_USERNAME}@${SERVER}" "${COMMANDS}"
  79.  
  80. # Use rsync to transfer all uploaded files from the server to the local uploads directory
  81. # A trailing / on a source name means "copy the contents of this directory". Without a trailing slash it means "copy the directory".
  82. rsync -rv -e 'ssh' "${SERVER_USERNAME}@${SERVER}:${SERVER_UPLOADS_DIR}" "${LOCAL_UPLOADS_DIR}"
  83.  
  84. # Use rsync to transfer the file from the Domains server to localhost
  85. rsync -rv -e 'ssh' "${SERVER_USERNAME}@${SERVER}:${SERVER_DIR}${DB_FILENAME}" "${LOCAL_DIR}"
  86.  
  87. # Import .sql file into database
  88. mysql -u "${LOCAL_DB_USER}" -p"${LOCAL_DB_PASS}" --socket '/Applications/MAMP/tmp/mysql/mysql.sock' "${LOCAL_DB}" < "./${DB_FILENAME}";
  89. fi
Add Comment
Please, Sign In to add comment