Commit a3e2c08b authored by pasden's avatar pasden 💬
Browse files

Update backup_db.sh

parent 7abef530
#!/bin/sh
container_name="itinvent"
path_docker_containers="/volume1/docker"
backup_path_inside_container="/var/opt/mssql/backup"
sql_user="sa"
sql_password="yourStrong(!)Password"
sql_database="ITINVENT"
sql_port="1433"
CONTAINER_NAME="itinvent"
PATH_DOCKER_CONTAINERS="/volume1/docker"
BACKUP_PATH_INSIDE_CONTAINER="/var/opt/mssql/backup"
SQL_USER="sa"
SQL_PASSWORD="yourStrong(!)Password"
SQL_DATABASE="ITINVENT"
SQL_PORT="1433"
container_data_path="${path_docker_containers}/${container_name}"
CONTAINER_DATA_PATH="${PATH_DOCKER_CONTAINERS}/${CONTAINER_NAME}"
docker exec -d $container_name /opt/mssql-tools/bin/sqlcmd \
-S "localhost,${sql_port}" \
-U "${sql_user}" \
-P "${sql_password}" \
-d "${sql_database}" \
docker exec -t $CONTAINER_NAME /opt/mssql-tools/bin/sqlcmd \
-S "localhost,${SQL_PORT}" \
-U "${SQL_USER}" \
-P "${SQL_PASSWORD}" \
-d "${SQL_DATABASE}" \
-Q "
DECLARE @dateTime VARCHAR(19) = CONVERT(VARCHAR(19), SYSDATETIMEOFFSET() AT TIME ZONE 'Russian Standard Time', 20)
DECLARE @dateBackup VARCHAR(MAX) = REPLACE(REPLACE(@dateTime,':','-'),' ','_')
DECLARE @fullPathBackupFile VARCHAR(MAX) = N'${backup_path_inside_container}/backup_${sql_database}_' + @dateBackup + '.bak'
BACKUP DATABASE ${sql_database} TO DISK = @fullPathBackupFile WITH NOFORMAT, NOINIT, NAME = 'Полная База данных. Резервное копирование.', SKIP, NOREWIND, NOUNLOAD, STATS = 10
DECLARE @fullPathBackupFile VARCHAR(MAX) = N'${BACKUP_PATH_INSIDE_CONTAINER}/backup_${SQL_DATABASE}_' + @dateBackup + '.bak'
BACKUP DATABASE ${SQL_DATABASE} TO DISK = @fullPathBackupFile WITH NOFORMAT, NOINIT, NAME = 'Полная База данных. Резервное копирование.', SKIP, NOREWIND, NOUNLOAD, STATS = 10
DECLARE @backupSetId AS INT
SELECT @backupSetId = position FROM msdb..backupset WHERE database_name='${sql_database}' AND backup_set_id=(SELECT MAX(backup_set_id) FROM msdb..backupset WHERE database_name='${sql_database}')
SELECT @backupSetId = position FROM msdb..backupset WHERE database_name='${SQL_DATABASE}' AND backup_set_id=(SELECT MAX(backup_set_id) FROM msdb..backupset WHERE database_name='${SQL_DATABASE}')
IF @backupSetId IS NULL BEGIN RAISERROR(N'Ошибка верификации. Сведения о резервном копировании для базы данных не найдены.', 16, 1) END
RESTORE VERIFYONLY FROM DISK = @fullPathBackupFile WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
" \
-o "${backup_path_inside_container}/backup_${sql_database}_$(date +'%Y-%m-%d_%H-%M-%S').log"
-o "${BACKUP_PATH_INSIDE_CONTAINER}/backup_${SQL_DATABASE}_$(date +'%Y-%m-%d_%H-%M-%S').log"
find ${container_data_path}/backup -type f -name "*.bak" -or -name "*.log" -mmin +270 -exec rm -f {} \;
find ${CONTAINER_DATA_PATH}/backup -type f -name "*.log" -mmin +480 -exec rm -f {} \;
find ${CONTAINER_DATA_PATH}/backup -type f -name "*.bak" -mmin +480 -exec rm -f {} \;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment