Guest User

Untitled

a guest
May 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #!/bin/bash
  2. archiveLocation=/backups
  3. archiveTempPath=/backups/tmp_backup
  4. archiveName=$(date '+%d-%m-%Y_%H:%M');
  5. archiveFullName=$archiveName.tar.gz
  6. archivePath=$archiveLocation/$archiveFullName
  7. deleteLocalArchive=false
  8.  
  9. backupPath=/var/www
  10. backupToRemoteServer=false
  11. remoteServerProtocol=sftp
  12. remoteServerPort=22
  13. remoteServerHost=
  14. remoteServerUsername=
  15. remoteServerPassword=
  16. remoteServerPath=/
  17.  
  18. backupMysql=false
  19. backupMysqlUsername=
  20. backupMysqlPassword=
  21. backupMysqlDatabase=
  22.  
  23. # Create temporary folder
  24. echo 'Creating temporary folders';
  25. mkdir $archiveTempPath
  26. mkdir $archiveTempPath/files
  27. mkdir $archiveTempPath/databases
  28.  
  29. # Set permissions
  30. echo 'Setting file permissions';
  31. chmod -R 777 $archiveTempPath
  32.  
  33. # Copy all the files
  34. echo 'Copying all files to temporary location';
  35. rsync -avz $backupPath $archiveTempPath/files -q
  36.  
  37. # Export MySQL database
  38. if [ $backupMysql = true ]
  39. then
  40. echo "Exporting database: $backupMysqlDatabase";
  41. mysqldump -u$backupMysqlUsername -p$backupMysqlPassword $backupMysqlDatabase > $archiveTempPath/databases/$backupMysqlDatabase.sql 2>&1
  42. fi
  43.  
  44. # Tar all files
  45. cd $archiveTempPath
  46. tar -czf "$archivePath" *
  47. cd $archiveLocation
  48.  
  49. # Upload archive to external server
  50. if [ $backupToRemoteServer = true ]
  51. then
  52. echo "Uploading archive ($remoteServerProtocol)";
  53. if [ $remoteServerProtocol = 'ftp' ]
  54. then
  55. curl -T $archivePath ftp://$remoteServerHost:$remoteServerPort/$remoteServerPath/ --user $remoteServerUsername:$remoteServerPassword -s
  56. elif [ $remoteServerProtocol == 'sftp' ]
  57. then
  58. sshpass -p $remoteServerPassword scp -P $remoteServerPort $archivePath $remoteServerUsername@$remoteServerHost:$remoteServerPath/$archiveFullname
  59. fi
  60. fi
  61.  
  62. # Remove all the temporary files
  63. echo 'Removing temporary files';
  64. rm -rf $archiveTempPath
  65.  
  66. # Remove local archive if necessary
  67. if [ $deleteLocalArchive = true ]
  68. then
  69. echo 'Removing local archive';
  70. rm -rf $archivePath;
  71. fi
  72.  
  73. # Done!
  74. echo "Backup $archiveFullName was completed";
Add Comment
Please, Sign In to add comment