download-backup-sftp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. #/###################################################################\
  3. #| Download backup's from backup.castanedo.es |
  4. #| Download a complete directory in a ssh server (sftp protocol). |
  5. #| Help: |
  6. #| -$host: host name or IP of the remote server. |
  7. #| Ex: backup.example.com |
  8. #| -$sftpuser: user for SFTP. |
  9. #| -$sftppass: password for SFTP. |
  10. #| -$sftpkey: keyfile for SFTP (if is set $sftppass will be the |
  11. #| key password). |
  12. #| -$backupDir: directory to save repository files. |
  13. #| Warning: |
  14. #| Store passwords in bash scripts is very unsecure. |
  15. #| (Contributions with keyrings are welcome). |
  16. #| |
  17. #| Guzmán Castanedo (guzman@castanedo.es) |
  18. #| January 2018 |
  19. #| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  20. #\###################################################################/
  21. host="backup.castanedo.es"
  22. sftpuser="sftpuser"
  23. sftppass="sftppass"
  24. sftpkey="" #Not set key file -> Normal Password Auth
  25. #sftpkey=/home/$USER/.ssh/your.key
  26. remotefile=/var/www/*.html
  27. backupDir=/home/$USER/Documentos/backup
  28. #Check dependencies
  29. if [ ! -x $(which ssh) ];then
  30. printf "ERROR:\tssh client Not Installed :O"
  31. exit 1
  32. fi
  33. if [ ! -x $(which sshpass) ];then
  34. printf "ERROR:\tsshpass Not Installed :O\n"
  35. exit 1
  36. fi
  37. #Check directory and create it
  38. if [ ! -d $backupDir/$host ];then
  39. mkdir -p $backupDir/$host
  40. if [ $? != 0 ];then
  41. printf "ERROR: Imposible crear %s\n" $backupDir/$host
  42. exit 1
  43. fi
  44. fi
  45. #Download with sftp
  46. printf "Destino backups:\t%s\n" $backupDir/$host
  47. cd $backupDir/$host
  48. echo "Sincronizando backup's desde sftp://$sftpuser@$host:$remotefile"
  49. if [ ! -f $sftpkey ] || [ -z $sftpkey ];then
  50. #Use Normal Password Auth
  51. export SSHPASS=$sftppass
  52. sshpass -e sftp -oBatchMode=no -a $sftpuser@$host:$remotefile
  53. if [ $? != 0 ];then
  54. printf "ERROR:\tSe han producido errores en la sincronizacion\n"
  55. exit 1
  56. fi
  57. else
  58. #Use Private Key File
  59. export SSHPASS=$sftppass
  60. sshpass -e sftp -oBatchMode=no -a -i $sftpkey $sftpuser@$host:$remotefile
  61. if [ $? != 0 ];then
  62. printf "ERROR:\tSe han producido errores en la sincronizacion\n"
  63. exit 1
  64. fi
  65. fi
  66. printf "Descarga correcta :)\n"