I am trying to run a script similar to this one (example), but it is not running correctly. The txt file is successfully generated, with the database names row by row, but when you back up the databases, a single large file is generated instead of one file for each database. In my txt contains 30 names, as there are 30 databases.
@echo off
:: Update variable date environment
set today=%date:~0,2%-%date:~3,2%-%date:~6,4%
:: Set folder root of backup files to send to network and to GDRIVE
set BACKUPPATHROOT=%systemdrive%\TMPSQL
:: Create local folder from date
mkdir %BACKUPPATHROOT%\%today% >nul 2>&1
:: Set local folder to save backup files
set BACKUPPATH=%BACKUPPATHROOT%\%today%
:: Set SQL Server location
set SERVERNAME=localhost
:: Get password of backup file compress
set PSWDZIP=123456
:: Build a list of databases to backup
set DBList=%BACKUPPATH%\SQLDBList.txt
SqlCmd -E -S %SERVERNAME% -h-1 -W -Q "SET NoCount ON; SELECT Name FROM sys.databases WHERE [Name] NOT IN ('master','model','msdb','tempdb')" > "%DBList%"
:: Backing UP
For /f "tokens=*" %%i in (%DBList%) do (
set BACKUPFILENAME=%BACKUPPATH%\%%i.bak
echo Fazendo backup do banco de dados %%i
sqlcmd -E -S %SERVERNAME% -Q "BACKUP DATABASE [%%i] TO DISK=N'%BACKUPFILENAME%' WITH NOFORMAT, NOINIT, NOREWIND, NOUNLOAD"
7za a -r -tzip -bso0 -bsp0 -sdel -p%PSWDZIP% %BACKUPFILENAME%.zip %BACKUPFILENAME%
echo.
)
Can anyone help me identify the error?
First lines of txt file:
MYDBTEST
MYDBTEST_ADM
DBTEST
DBTEST_ADM
MYDBZBX
MYDBZBX_OLD
Remove old backup files from GDRIVEcode uses the incorrect syntax for a batch file and yourgdrivecommands would certainly benefit from using the--no-headeroption. I would suggest that you use the recommended syntax for setting variables,Set "VariableName=StringValue", and to doublequote variables which may contain spaces or poison characters.:: Get date 10 days ago on GDRIVE, but then you go on to capture a date 11 days ago withset day=-11. I'm aware that is because you're using less than,<, in your GDrive command, and I'm unsure whether it accepts the commonly used less than or equal to,<=, but if it does, that would certainly tidy up your code. Inexplicably however, you then want to:: Delete folder older than 10 daysbut proceed to use/D -5in yourForFilescommand.Z:via a previously defined variable, but then opt to unmount driveS:at the end of your code.