Last active
May 31, 2024 07:57
-
-
Save allebb/5d5f42db8ac78fc488ea25a0e5d4a051 to your computer and use it in GitHub Desktop.
Shell script to backup Microsoft SQL Server vNEXT databases on Linux.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Shell script to automate the backup of Microsoft SQL Server vNEXT backups on Linux. | |
# Written by Bobby Allen <[email protected]>, 26/04/2017 | |
# | |
# Download this script and put into your servers' /usr/bin directory (or symlink) then make it executable (chmod +x) | |
# | |
# 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"): | |
# mssqlbackup "/var/dbbackups" 5 "localhost" "sa" "P455w0rD" | |
# | |
# 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: | |
# 15 0 * * * root /usr/bin/mssqlbackup "/var/dbbackups" 30 "localhost" "sa" "<YourPasswordHere>" | |
# | |
# Set some default values | |
MSSQL_HOST="localhost" | |
BACKUP_DIR="/var/dbbackups" | |
BACKUP_DAYS=7 # You can test by changing file dates using: touch -d "8 days ago" mssql_20160504-1542.gz | |
MSSQL_USER="sa" | |
MSSQL_PASS="" | |
MSSQL_EXEC="/opt/mssql-tools/bin/sqlcmd" | |
DATE=`date +%Y%m%d-%H%M%S` # YYYYMMDD-HHMMSS | |
# Set Backup Parameters | |
if [[ ! $1 == "" ]] | |
then | |
BACKUP_DIR=$1 | |
fi | |
if [[ ! $2 == "" ]] | |
then | |
BACKUP_DAYS=$2 | |
fi | |
if [[ ! $3 == "" ]] | |
then | |
MSSQL_HOST=$3 | |
fi | |
if [[ ! $4 == "" ]] | |
then | |
MSSQL_USER=$4 | |
fi | |
if [[ ! $5 == "" ]] | |
then | |
MSSQL_PASS=$5 | |
fi | |
echo "" | |
echo "Starting MSSQL backup with the following conditions:" | |
echo "" | |
echo " Backup to: ${BACKUP_DIR}" | |
echo " Backup timestamp: ${DATE}" | |
echo " Keep backups for ${BACKUP_DAYS} days" | |
echo "" | |
echo " MSSQL Server: ${MSSQL_HOST}" | |
echo " MSSQL User: ${MSSQL_USER}" | |
echo " MSSQL Password: **HIDDEN**" | |
echo "" | |
# If the backup directory does not exist, we will create it.. | |
if [[ ! -d $BACKUP_DIR ]] | |
then | |
echo "" | |
echo "Backup directory does not exist, creating it..." | |
mkdir -p $BACKUP_DIR | |
echo "" | |
fi | |
# Disable error messages. | |
exec 2> /dev/null | |
# Connect to MSSQL and get all databases to backup... | |
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|^$)"` | |
# Iterate over all of our databases and back them up one by one... | |
echo "Starting backups..." | |
for DBNAME in $DATABASES; do | |
echo -n " - Backing up database \"${DBNAME}\"... " | |
$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" | |
echo "Done!" | |
done | |
echo "Backups complete!" | |
echo "" | |
# Re-enable error messages. | |
exec 2> /dev/tty | |
# Now delete files older than X days | |
find $BACKUP_DIR/* -mtime +$BACKUP_DAYS -exec rm {} \; |
Author
allebb
commented
Apr 26, 2021
via email
That's great to hear - I'm pleased that I was able to help!
All the best with your MSSQL backup script on Linux :)
…On Mon, 26 Apr 2021 at 13:27, Bilal Malik ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
thanks @allebb <https://github.com/allebb> it works, i save a lot of time
by using your magical script
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5d5f42db8ac78fc488ea25a0e5d4a051#gistcomment-3720154>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAF3NDAHWU3I2VYY5EII3D3TKVL43ANCNFSM4J5ZTDAQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment