- 23 Views
- 12/05/2025
How to create a script to backup mssql database on the Linux server
I'll provide a comprehensive script for backing up MSSQL Server databases on Linux, incorporating best practices, error handling, compression, cleanup, and optional
Below is a complete script for backing up MSSQL Server databases on Linux, with Telegram notifications included (you can remove the Telegram part if not needed).
#!/bin/bash
# Configuration
SERVER="localhost" # SQL Server address
USERNAME="sa" # SQL Server username
PASSWORD="your_secure_password" # SQL Server password
BACKUP_PATH="/backup/mssql/$(date +%F)" # Backup directory (e.g., /backup/mssql/YYYY-MM-DD)
RETENTION_DAYS=7 # Days to keep backups
TELEGRAM_BOT_TOKEN="your_bot_token" # Telegram bot token (optional)
TELEGRAM_CHAT_ID="your_chat_id" # Telegram chat ID (optional)
LOG_FILE="/var/log/mssql_backup.log" # Log file for backup process
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Create backup directory
if [ ! -d "$BACKUP_PATH" ]; then
mkdir -p "$BACKUP_PATH"
chmod 755 "$BACKUP_PATH"
chown mssql:mssql "$BACKUP_PATH" # Adjust user:group as needed
if [ $? -ne 0 ]; then
log_message "ERROR: Failed to create backup directory $BACKUP_PATH"
exit 1
fi
fi
# Get list of databases
DATABASES=$(sqlcmd -S "$SERVER" -U "$USERNAME" -P "$PASSWORD" -Q "SET NOCOUNT ON; SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');" -h -1 -W)
if [ -z "$DATABASES" ]; then
log_message "ERROR: No databases found to back up."
exit 1
fi
# Count total databases
TOTAL_DBS=$(echo "$DATABASES" | wc -l)
SUCCESS_DBS=0
log_message "Found $TOTAL_DBS databases to back up."
# Loop through each database
for DATABASE in $DATABASES; do
if [ -n "$DATABASE" ]; then
BACKUP_FILE="$BACKUP_PATH/${DATABASE}_backup_$(date +%F_%H%M%S).bak"
log_message "Backing up database $DATABASE to $BACKUP_FILE..."
# Perform backup
sqlcmd -S "$SERVER" -U "$USERNAME" -P "$PASSWORD" -Q "BACKUP DATABASE [$DATABASE] TO DISK = N'$BACKUP_FILE' WITH INIT, NAME = 'Full Backup of $DATABASE'"
if [ $? -eq 0 ]; then
log_message "Backup of $DATABASE successful."
((SUCCESS_DBS++))
# Compress backup file
BACKUP_ZIP="${BACKUP_FILE}.gz"
if gzip -c "$BACKUP_FILE" > "$BACKUP_ZIP"; then
rm -f "$BACKUP_FILE"
log_message "Compressed backup to $BACKUP_ZIP"
else
log_message "ERROR: Failed to compress $BACKUP_FILE"
fi
else
log_message "ERROR: Backup of $DATABASE failed."
fi
fi
done
# Clean up old backups
log_message "Cleaning up backups older than $RETENTION_DAYS days..."
find /backup/mssql/ -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} \;
if [ $? -eq 0 ]; then
log_message "Old backups cleaned successfully."
else
log_message "ERROR: Failed to clean old backups."
fi
# Send Telegram notification (optional)
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then
MESSAGE="MSSQL Backup Report\n- Successful: $SUCCESS_DBS/$TOTAL_DBS databases\n- Time: $(date '+%Y-%m-%d %H:%M:%S')\n- Path: $BACKUP_PATH"
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" \
-d text="$MESSAGE" > /dev/null
if [ $? -eq 0 ]; then
log_message "Telegram notification sent."
else
log_message "ERROR: Failed to send Telegram notification."
fi
fi
log_message "Backup process completed."
How to Use
1. Save the script:
Save the script to a file, e.g., /scripts/backup_mssql.sh.
2. Test the script:
Check the log file (/var/log/mssql_backup.log) for details.
Verify backup files in /backup/mssql/YYYY-MM-DD/.
If using Telegram, check the chat for notifications.
Schedule with cron:
Add the following line to run daily at 2 AM:
Thanks for visit my website
