Update mssql-backup.sh
This commit is contained in:
parent
5a6c61200c
commit
4af162317a
@ -23,11 +23,14 @@ fi
|
|||||||
: ${LOG_FILE:="/zucchetti/backupdb/backup_log.txt"}
|
: ${LOG_FILE:="/zucchetti/backupdb/backup_log.txt"}
|
||||||
: ${ERROR_LOG_FILE:="/zucchetti/backupdb/backup_err.txt"}
|
: ${ERROR_LOG_FILE:="/zucchetti/backupdb/backup_err.txt"}
|
||||||
: ${REMOTE_DIR:="/BackupDB"}
|
: ${REMOTE_DIR:="/BackupDB"}
|
||||||
|
: ${DO_LOCAL_BACKUP:=1} # 1 per eseguire il backup locale, 0 per disabilitarlo
|
||||||
|
: ${DO_FTP_BACKUP:=1} # 1 per eseguire il backup FTP, 0 per disabilitarlo
|
||||||
|
: ${TRUST_CERTIFICATE:=1} # 1 per accettare certificati autofirmati, 0 per richiederli validi
|
||||||
|
|
||||||
# Required variables check
|
# Required variables check
|
||||||
required_vars=(
|
required_vars=(
|
||||||
"USER" "PASSWORD" "FTP_SERVER" "FTP_USER" "FTP_PASSWORD"
|
"USER" "PASSWORD" "FTP_SERVER" "FTP_USER" "FTP_PASSWORD"
|
||||||
"SMTP_SERVER" "SMTP_USER" "SMTP_PASS"
|
"SMTP_SERVER" "SMTP_USER" "SMTP_PASS" "DO_LOCAL_BACKUP" "DO_FTP_BACKUP"
|
||||||
)
|
)
|
||||||
|
|
||||||
for var in "${required_vars[@]}"; do
|
for var in "${required_vars[@]}"; do
|
||||||
@ -42,13 +45,23 @@ DATE=$(LC_TIME=C date +%Y%m%d%H%M%S)
|
|||||||
|
|
||||||
# Function to get all user databases
|
# Function to get all user databases
|
||||||
get_all_databases() {
|
get_all_databases() {
|
||||||
$SQLCMD -S "$HOST" -U "$USER" -P "$PASSWORD" -h-1 \
|
local trust_cert_option=""
|
||||||
|
if [ "$TRUST_CERTIFICATE" -eq 1 ]; then
|
||||||
|
trust_cert_option="-C"
|
||||||
|
fi
|
||||||
|
|
||||||
|
$SQLCMD -S "$HOST" -U "$USER" -P "$PASSWORD" $trust_cert_option -h-1 \
|
||||||
-Q "SELECT name FROM sys.databases WHERE database_id > 4 AND state = 0 AND is_read_only = 0" \
|
-Q "SELECT name FROM sys.databases WHERE database_id > 4 AND state = 0 AND is_read_only = 0" \
|
||||||
-C -b | tr -d ' '
|
-W -s"|" | grep -v "rows affected" | grep -v "^-" | grep -v "^$" | tr -d ' '
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to check FTP connection
|
# Function to check FTP connection
|
||||||
check_ftp_connection() {
|
check_ftp_connection() {
|
||||||
|
# Skip if FTP backup is disabled
|
||||||
|
if [ "$DO_FTP_BACKUP" -eq 0 ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
local temp_file=$(mktemp)
|
local temp_file=$(mktemp)
|
||||||
ftp -inv $FTP_SERVER > $temp_file 2>&1 <<EOF
|
ftp -inv $FTP_SERVER > $temp_file 2>&1 <<EOF
|
||||||
user $FTP_USER $FTP_PASSWORD
|
user $FTP_USER $FTP_PASSWORD
|
||||||
@ -64,6 +77,11 @@ EOF
|
|||||||
|
|
||||||
# Function to perform FTP upload with retry mechanism
|
# Function to perform FTP upload with retry mechanism
|
||||||
perform_ftp_upload() {
|
perform_ftp_upload() {
|
||||||
|
# Skip if FTP backup is disabled
|
||||||
|
if [ "$DO_FTP_BACKUP" -eq 0 ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
local source_file="$1"
|
local source_file="$1"
|
||||||
local dest_file="$2"
|
local dest_file="$2"
|
||||||
local max_retries=3
|
local max_retries=3
|
||||||
@ -99,17 +117,25 @@ send_email() {
|
|||||||
local status_message=""
|
local status_message=""
|
||||||
|
|
||||||
# Costruisci il messaggio di stato dettagliato
|
# Costruisci il messaggio di stato dettagliato
|
||||||
|
if [ "$DO_LOCAL_BACKUP" -eq 1 ]; then
|
||||||
if [ $LOCAL_STATUS -eq 0 ]; then
|
if [ $LOCAL_STATUS -eq 0 ]; then
|
||||||
status_message="Local backup: SUCCESS\n"
|
status_message="Local backup: SUCCESS\n"
|
||||||
else
|
else
|
||||||
status_message="Local backup: FAILED\n"
|
status_message="Local backup: FAILED\n"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
status_message="Local backup: DISABLED\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$DO_FTP_BACKUP" -eq 1 ]; then
|
||||||
if [ $FTP_STATUS -eq 0 ]; then
|
if [ $FTP_STATUS -eq 0 ]; then
|
||||||
status_message+="FTP upload: SUCCESS\n"
|
status_message+="FTP upload: SUCCESS\n"
|
||||||
else
|
else
|
||||||
status_message+="FTP upload: FAILED\n"
|
status_message+="FTP upload: FAILED\n"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
status_message+="FTP upload: DISABLED\n"
|
||||||
|
fi
|
||||||
|
|
||||||
SUBJECT="Backup [${EMAIL_STATUS}] for $db_name on $SERVER_NAME - $(LC_TIME=C date)"
|
SUBJECT="Backup [${EMAIL_STATUS}] for $db_name on $SERVER_NAME - $(LC_TIME=C date)"
|
||||||
MESSAGE="Backup process completed with following status:\n\n${status_message}\nDetailed log:\n\n$(cat $LOG_FILE)"
|
MESSAGE="Backup process completed with following status:\n\n${status_message}\nDetailed log:\n\n$(cat $LOG_FILE)"
|
||||||
@ -136,12 +162,14 @@ perform_backup() {
|
|||||||
BACKUP_FILE="$LOCAL_DB_DIR/${db_name}_backup_$DATE.bak"
|
BACKUP_FILE="$LOCAL_DB_DIR/${db_name}_backup_$DATE.bak"
|
||||||
REMOTE_BACKUP_FILE="${db_name}_backup_$DATE.bak"
|
REMOTE_BACKUP_FILE="${db_name}_backup_$DATE.bak"
|
||||||
|
|
||||||
# Create local backup directory if it doesn't exist
|
# Create local backup directory if local backup is enabled
|
||||||
|
if [ "$DO_LOCAL_BACKUP" -eq 1 ]; then
|
||||||
mkdir -p "$LOCAL_DB_DIR"
|
mkdir -p "$LOCAL_DB_DIR"
|
||||||
chown mssql:mssql "$LOCAL_DB_DIR"
|
chown mssql:mssql "$LOCAL_DB_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
# Create remote directory on FTP server if it doesn't exist
|
# Create remote directory on FTP server if FTP backup is enabled
|
||||||
if check_ftp_connection; then
|
if [ "$DO_FTP_BACKUP" -eq 1 ] && check_ftp_connection; then
|
||||||
ftp -inv $FTP_SERVER <<EOF >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
ftp -inv $FTP_SERVER <<EOF >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
||||||
user $FTP_USER $FTP_PASSWORD
|
user $FTP_USER $FTP_PASSWORD
|
||||||
mkdir "$REMOTE_DB_DIR"
|
mkdir "$REMOTE_DB_DIR"
|
||||||
@ -153,10 +181,16 @@ EOF
|
|||||||
START_TIME=$(date +%s)
|
START_TIME=$(date +%s)
|
||||||
echo "$(LC_TIME=C date): Starting backup for database: $db_name on server $SERVER_NAME" > $LOG_FILE
|
echo "$(LC_TIME=C date): Starting backup for database: $db_name on server $SERVER_NAME" > $LOG_FILE
|
||||||
|
|
||||||
# Perform database backup with CHECK option (fixed SQLCMD syntax)
|
# Perform local backup if enabled
|
||||||
$SQLCMD -S "$HOST" -U "$USER" -P "$PASSWORD" \
|
if [ "$DO_LOCAL_BACKUP" -eq 1 ]; then
|
||||||
|
local trust_cert_option=""
|
||||||
|
if [ "$TRUST_CERTIFICATE" -eq 1 ]; then
|
||||||
|
trust_cert_option="-C"
|
||||||
|
fi
|
||||||
|
|
||||||
|
$SQLCMD -S "$HOST" -U "$USER" -P "$PASSWORD" $trust_cert_option \
|
||||||
-Q "BACKUP DATABASE [$db_name] TO DISK = N'$BACKUP_FILE' WITH CHECKSUM, COMPRESSION, INIT" \
|
-Q "BACKUP DATABASE [$db_name] TO DISK = N'$BACKUP_FILE' WITH CHECKSUM, COMPRESSION, INIT" \
|
||||||
-C -b >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
-b >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
||||||
|
|
||||||
# Check if the local backup was successful
|
# Check if the local backup was successful
|
||||||
if [ $? -eq 0 ] && [ -f "$BACKUP_FILE" ]; then
|
if [ $? -eq 0 ] && [ -f "$BACKUP_FILE" ]; then
|
||||||
@ -166,6 +200,10 @@ EOF
|
|||||||
LOCAL_STATUS=1
|
LOCAL_STATUS=1
|
||||||
echo "$(LC_TIME=C date): Error during local backup of $db_name. Check error log." >> $LOG_FILE
|
echo "$(LC_TIME=C date): Error during local backup of $db_name. Check error log." >> $LOG_FILE
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
LOCAL_STATUS=0
|
||||||
|
echo "$(LC_TIME=C date): Local backup disabled for $db_name." >> $LOG_FILE
|
||||||
|
fi
|
||||||
|
|
||||||
# End time and duration
|
# End time and duration
|
||||||
END_TIME=$(date +%s)
|
END_TIME=$(date +%s)
|
||||||
@ -175,8 +213,9 @@ EOF
|
|||||||
echo "End time: $(LC_TIME=C date -d @$END_TIME)" >> $LOG_FILE
|
echo "End time: $(LC_TIME=C date -d @$END_TIME)" >> $LOG_FILE
|
||||||
echo "Duration: $((DURATION / 60)) minutes and $((DURATION % 60)) seconds" >> $LOG_FILE
|
echo "Duration: $((DURATION / 60)) minutes and $((DURATION % 60)) seconds" >> $LOG_FILE
|
||||||
|
|
||||||
# Upload backup to remote FTP server only if local backup was successful
|
# Upload backup to remote FTP server if enabled and local backup was successful
|
||||||
if [ $LOCAL_STATUS -eq 0 ]; then
|
if [ "$DO_FTP_BACKUP" -eq 1 ]; then
|
||||||
|
if [ "$DO_LOCAL_BACKUP" -eq 0 ] || [ $LOCAL_STATUS -eq 0 ]; then
|
||||||
echo "$(LC_TIME=C date): Starting FTP upload for $db_name." >> $LOG_FILE
|
echo "$(LC_TIME=C date): Starting FTP upload for $db_name." >> $LOG_FILE
|
||||||
|
|
||||||
if ! check_ftp_connection; then
|
if ! check_ftp_connection; then
|
||||||
@ -196,14 +235,18 @@ EOF
|
|||||||
echo "$(LC_TIME=C date): Skipping FTP upload due to local backup failure." >> $LOG_FILE
|
echo "$(LC_TIME=C date): Skipping FTP upload due to local backup failure." >> $LOG_FILE
|
||||||
FTP_STATUS=1
|
FTP_STATUS=1
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
FTP_STATUS=0
|
||||||
|
echo "$(LC_TIME=C date): FTP upload disabled for $db_name." >> $LOG_FILE
|
||||||
|
fi
|
||||||
|
|
||||||
# Local retention policy
|
# Local retention policy if local backup is enabled
|
||||||
if [ $LOCAL_STATUS -eq 0 ]; then
|
if [ "$DO_LOCAL_BACKUP" -eq 1 ] && [ $LOCAL_STATUS -eq 0 ]; then
|
||||||
find "$LOCAL_DB_DIR" -name "${db_name}_backup_*.bak" -type f -mtime +$RETENTION -delete >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
find "$LOCAL_DB_DIR" -name "${db_name}_backup_*.bak" -type f -mtime +$RETENTION -delete >> $LOG_FILE 2>> $ERROR_LOG_FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remote retention policy
|
# Remote retention policy if FTP backup is enabled
|
||||||
if [ $FTP_STATUS -eq 0 ]; then
|
if [ "$DO_FTP_BACKUP" -eq 1 ] && [ $FTP_STATUS -eq 0 ]; then
|
||||||
echo "$(LC_TIME=C date): Performing remote retention check." >> $LOG_FILE
|
echo "$(LC_TIME=C date): Performing remote retention check." >> $LOG_FILE
|
||||||
|
|
||||||
temp_list_file=$(mktemp)
|
temp_list_file=$(mktemp)
|
||||||
@ -237,7 +280,8 @@ EOF
|
|||||||
rm -f $temp_list_file
|
rm -f $temp_list_file
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Determine email status
|
# Determine email status based on enabled backups
|
||||||
|
if [ "$DO_LOCAL_BACKUP" -eq 1 ] && [ "$DO_FTP_BACKUP" -eq 1 ]; then
|
||||||
if [ $LOCAL_STATUS -eq 0 ] && [ $FTP_STATUS -eq 0 ]; then
|
if [ $LOCAL_STATUS -eq 0 ] && [ $FTP_STATUS -eq 0 ]; then
|
||||||
EMAIL_STATUS="SUCCESS"
|
EMAIL_STATUS="SUCCESS"
|
||||||
elif [ $LOCAL_STATUS -eq 1 ] && [ $FTP_STATUS -eq 1 ]; then
|
elif [ $LOCAL_STATUS -eq 1 ] && [ $FTP_STATUS -eq 1 ]; then
|
||||||
@ -245,6 +289,21 @@ EOF
|
|||||||
else
|
else
|
||||||
EMAIL_STATUS="WARNING"
|
EMAIL_STATUS="WARNING"
|
||||||
fi
|
fi
|
||||||
|
elif [ "$DO_LOCAL_BACKUP" -eq 1 ]; then
|
||||||
|
if [ $LOCAL_STATUS -eq 0 ]; then
|
||||||
|
EMAIL_STATUS="SUCCESS"
|
||||||
|
else
|
||||||
|
EMAIL_STATUS="FAILED"
|
||||||
|
fi
|
||||||
|
elif [ "$DO_FTP_BACKUP" -eq 1 ]; then
|
||||||
|
if [ $FTP_STATUS -eq 0 ]; then
|
||||||
|
EMAIL_STATUS="SUCCESS"
|
||||||
|
else
|
||||||
|
EMAIL_STATUS="FAILED"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
EMAIL_STATUS="DISABLED"
|
||||||
|
fi
|
||||||
|
|
||||||
# Send email with log
|
# Send email with log
|
||||||
send_email "$db_name"
|
send_email "$db_name"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user