Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Generate private public ssh-keys
  4. if [ ! -f "$HOME/.ssh/id_rsa" ] && [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
  5. ssh-keygen -b 4096
  6. fi
  7.  
  8. read -p "Enter your ssh host:" ssh_host
  9. read -p "Enter your ssh username:" ssh_user
  10. read -p "Enter your remote ssh port:" ssh_port
  11.  
  12. # REMOTE SSH
  13.  
  14. if [ -z "$ssh_user" ]; then
  15. echo -e "Config: SSH Username missing"
  16. echo $ssh_user
  17. exit
  18. fi
  19.  
  20. if [ -z "$ssh_host" ]; then
  21. echo -e "Config: SSH Host is missing"
  22. exit
  23. fi
  24.  
  25. if [ -z "$ssh_port" ]; then
  26. echo -e "Config: SSH Port missing"
  27. exit
  28. fi
  29.  
  30. ssh-copy-id $ssh_user@$ssh_host -p $ssh_port -i "$HOME/.ssh/id_rsa" &>/dev/null
  31.  
  32. # REMOTE DATABASE
  33.  
  34. read -p "Enter path for wp-config.php:" wp_config_path
  35.  
  36.  
  37. # INIT
  38. db_details="cat $wp_config_path/wp-config.php"
  39. scp -P $ssh_port -r $ssh_user@$ssh_host:"$wp_config_path/*" .
  40.  
  41. # Might be dangerous if file contains malicious input in the values?
  42. eval $(awk -F "[()']" '/^define(/{printf "%s='''%s'''n", $3, $5;}' < wp-config.php | grep DB_*)
  43.  
  44. if [ -z "$DB_USER" ]; then
  45. echo -e "Config: RDB user missing"
  46. exit
  47. fi
  48.  
  49. if [ -z "$DB_PASSWORD" ]; then
  50. echo -e "Config: RDB password missing"
  51. exit
  52. fi
  53.  
  54. if [ -z "$DB_NAME" ]; then
  55. echo -e "Config: RDB name missing"
  56. exit
  57. fi
  58.  
  59. dump="mysqldump -u $DB_USER --password='$DB_PASSWORD' $DB_NAME"
  60.  
  61. read -p "Enter your LOCAL database user:" local_db_user
  62. read -p "Enter your LOCAL database name:" local_db_name
  63. read -s -p "Enter your local database password:" local_db_password
  64.  
  65. printf "33c"
  66.  
  67. ssh $ssh_user@$ssh_host -p $ssh_port $dump | mysql -u $local_db_user --password=$local_db_password $local_db_name
  68.  
  69. sed -i -e "s;(define([[:space:]]*'DB_USER',[[:space:]]*)(.*)(););1'$local_db_user'3;g" wp-config.php
  70. sed -i -e "s;(define([[:space:]]*'DB_PASSWORD',[[:space:]]*)(.*)(););1'$local_db_password'3;g" wp-config.php
  71. sed -i -e "s;(define([[:space:]]*'DB_NAME',[[:space:]]*)(.*)(););1'$local_db_name'3;g" wp-config.php
  72.  
  73. echo -e "Database imported to: $local_db_name"
  74. echo -e "Cleaning up..."
  75.  
  76. # CLEANING UP
  77.  
  78. # This is bad, and should NEVER be used on hosts with active pub/private key authentication.
  79. ssh $ssh_user@$ssh_host -p $ssh_port 'echo "" > $HOME/.ssh/authorized_keys'
  80.  
  81. ssh_user=
  82. ssh_host=
  83. ssh_port=
  84. DB_USER=
  85. DB_PASSWORD=
  86. DB_NAME=
  87. local_db_name=
  88. local_db_password=
  89. local_db_user=
  90. wp_config_path=
  91.  
  92. echo -e "Exiting"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement