Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. #
  4. # Shell script to automate the backup of Microsoft SQL Server vNEXT backups on Linux.
  5. # Written by Bobby Allen <ballen@bobbyallen.me>, 26/04/2017
  6. #
  7. # Download this script and put into your servers' /usr/bin directory (or symlink) then make it executable (chmod +x)
  8. #
  9. # Usage example (backup databases into /var/backups, keep backups for 5 days, connect to server "localhost" with the account "sa" and a password of "P455w0RD"):
  10. # mssqlbackup "/var/backups" 5 "localhost" "sa" "P455w0rD"
  11. #
  12. # If you wanted to automate backups, here is a cron job example (create a new file under /etc/cron.d eg. '/etc/cron.d/mssql-backups' and copy into the following line) -This will backup all your databases daily at 00:15 to /var/backups and keep backups for 30 days:
  13. # 15 0 * * * root /usr/bin/mssqlbackup "/var/backups" 30 "localhost" "sa" "<YourPasswordHere>"
  14. #
  15.  
  16. # Set some default values
  17. MSSQL_HOST="localhost"
  18. BACKUP_DIR="/var/backups"
  19. BACKUP_DAYS=7 # You can test by changing file dates using: touch -d "8 days ago" mssql_20160504-1542.gz
  20. MSSQL_USER="sa"
  21. MSSQL_PASS=""
  22. MSSQL_EXEC="/opt/mssql-tools/bin/sqlcmd"
  23. DATE=`date +%Y%m%d-%H%M%S` # YYYYMMDD-HHMMSS
  24.  
  25. # Set Backup Parameters
  26. if [[ ! $1 == "" ]]
  27. then
  28. BACKUP_DIR=$1
  29. fi
  30. if [[ ! $2 == "" ]]
  31. then
  32. BACKUP_DAYS=$2
  33. fi
  34. if [[ ! $3 == "" ]]
  35. then
  36. MSSQL_HOST=$3
  37. fi
  38. if [[ ! $4 == "" ]]
  39. then
  40. MSSQL_USER=$4
  41. fi
  42. if [[ ! $5 == "" ]]
  43. then
  44. MSSQL_PASS=$5
  45. fi
  46.  
  47. echo ""
  48. echo "Starting MSSQL backup with the following conditions:"
  49. echo ""
  50. echo " Backup to: ${BACKUP_DIR}"
  51. echo " Backup timestamp: ${DATE}"
  52. echo " Keep backups for ${BACKUP_DAYS} days"
  53. echo ""
  54. echo " MSSQL Server: ${MSSQL_HOST}"
  55. echo " MSSQL User: ${MSSQL_USER}"
  56. echo " MSSQL Password: **HIDDEN**"
  57. echo ""
  58.  
  59. # If the backup directory does not exist, we will create it..
  60. if [[ ! -d $BACKUP_DIR ]]
  61. then
  62. echo ""
  63. echo "Backup directory does not exist, creating it..."
  64. mkdir -p $BACKUP_DIR
  65. echo ""
  66. fi
  67.  
  68. # Disable error messages.
  69. exec 2> /dev/null
  70.  
  71. # Connect to MSSQL and get all databases to backup...
  72. DATABASES=`$MSSQL_EXEC -S "$MSSQL_HOST" -U "$MSSQL_USER" -P "$MSSQL_PASS" -Q "SELECT Name from sys.Databases" | grep -Ev "(-|Name|master|tempdb|model|msdb|affected\)$|\s\n|^$)"`
  73.  
  74. # Iterate over all of our databases and back them up one by one...
  75. echo "Starting backups..."
  76. for DBNAME in $DATABASES; do
  77. echo -n " - Backing up database \"${DBNAME}\"... "
  78. $MSSQL_EXEC -H "$MSSQL_HOST" -U "$MSSQL_USER" -P "$MSSQL_PASS" -Q "BACKUP DATABASE [${DBNAME}] TO DISK = '${BACKUP_DIR}/${DBNAME}_${DATE}.bak' WITH NOFORMAT, NOINIT, NAME = '${DBNAME}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
  79. echo "Done!"
  80. done
  81. echo "Backups complete!"
  82. echo ""
  83.  
  84. # Re-enable error messages.
  85. exec 2> /dev/tty
  86.  
  87. # Now delete files older than X days
  88. find $BACKUP_DIR/* -mtime +$BACKUP_DAYS -exec rm {} \;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement