backup.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. echo "======================================="
  3. while read -p "Backup website: " domain
  4. do if [[ -d $domain ]]; then
  5. break
  6. else
  7. echo "$domain not found, please try again"
  8. fi
  9. done
  10. #Variable
  11. bkdir=backup/
  12. web=$domain/DocumentRoot
  13. #Check config
  14. if [ -f $web/wp-config.php ]; then
  15. config=$web/wp-config.php
  16. rm -f $domain/wp-config.php
  17. elif [ -f $domain/wp-config.php ]; then
  18. config=$domain/wp-config.php
  19. else
  20. echo "Config website not found"
  21. exit
  22. fi
  23. #Variable
  24. user=$(grep DB_USER $config | awk -F\' '{print$4}')
  25. db=$(grep DB_NAME $config | awk -F\' '{print$4}')
  26. pass=$(grep DB_PASSWORD $config | awk -F\' '{print$4}')
  27. mkdir -p $bkdir
  28. wget -q script.lehait.net/restore.sh -O "$bkdir"restore.sh
  29. #Backup database
  30. mysqldump --user $user --password=$pass $db > "$bkdir"backup.sql
  31. if [ $? -eq 0 ] ; then
  32. echo "======================================="
  33. echo "Backup database successful"
  34. else
  35. echo "Backup database fail"
  36. fi
  37. #Backup code
  38. zip -r "$bkdir"backup.zip $web -q -x $web/wp-content/cache/**\*
  39. echo "Backup code successful"
  40. echo "======================================="
  41. remote(){
  42. read -p "Server need copy website:" host
  43. read -p "Username SFTP:" acc
  44. scp -r -P 9090 $bkdir $acc@$host:/home/$acc/
  45. }
  46. #Connect remote hosting and tranfer file
  47. remote
  48. #Check connect
  49. while [ $? -ge 1 ]; do
  50. echo "Connect fail, please try again"
  51. remote
  52. done
  53. rm -rf $bkdir
  54. echo "======================================="
  55. echo "Password connect SSH Server"
  56. ssh -p 9090 $acc@$host "cd backup && sh restore.sh"
  57. while [ $? -ge 1 ]; do
  58. echo "Connect fail, try connect again..."
  59. ssh -p 9090 $acc@$host "cd backup && sh restore.sh"
  60. done
  61. exit