Deploy SQL File from VSS to SQL Server using Batch File - powershell

I want to know if there is any way for me to deploy a stored procedure (sql file) that is checked-in at VSS? I have a couple of procedures that I want to deploy to SQL Server. I'm trying to create a batch file to deploy them from VSS to SQL Server.
I want to achieve this since we would like to remove direct access to SQL Server. So that everything step we do on the procedures could be monitored.
Thanks!
EDIT:
I have also read that it's possible in Powershell. If anyone can point me to a good way to do it, that would be appreciated so much! I'm new to the VSS, Batch Files, and Powershells. So I'm a little bit confused where to start. Thanks!
This is what I have so far. But it doesn't work.
#echo off
cls
set path=C:\Program Files\Microsoft Visual SourceSafe
set ssdir=\\MySampel_VSS\VSS\SampleDB
set Recursive = Yes
set /p SName=Server Name :
set /p UName=User Name :
set /p Pwd=Password :
set /p DbName=Database Name :
set /p choice=ARE YOU SURE TO EXECUTE SCRIPTS in %DbName% (y/n) ?
if '%choice%'=='y' goto begin
goto end
:begin
if exist C:\Scripts\error.txt del C:\Scripts\error.txt
#echo on
sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -I -i $/Database/SampleDB/Procedures/MySample.sql >> error.txt 2>&1
#notepad error.txt
:end

You need to use Visual Source Safe command line in a batch file to deploy a project that is checked in. Take a look at the Checkout and Deploy commands in particular.

Related

Postgresql Auto backup using powershell and task scheduler

I am running attached script to backup postgresql database by using task scheduler. Script is executed successfully but backup is not happening. Same script i have run on powershell and it's working fine.enter image description here
I want to schedule daily backup on windows server. Please help me or suggest any alternative to automate the backup.
Regards
Sadam
Try this script, it will create a backup file with a name consisting of a timestamp:
First, create a backup.bat file and just run it (set your credentials and database name):
echo off
echo 'Generate backup file name'
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set BACKUP_FILE=%CUR_YYYY%-%CUR_MM%-%CUR_DD%_%CUR_HH%-%CUR_NN%-%CUR_SS%.custom.backup
echo 'Backup path: %BACKUP_FILE%'
echo 'Creating a backup ...'
set PGPASSWORD=strongpas
pg_dump.exe --username="postgres" -d AdventureWorks --format=custom -f "%BACKUP_FILE%"
echo 'Backup successfully created: %BACKUP_FILE%'
As a result, you should see such a picture, and a file with the extension .custom.backup should appear in the directory
If you get an error that the executable file "pg_dump.exe" was not found, then specify the full path to this file, something like "C:\Program Files\PostgreSQL\12\bin\pg_dump.exe", or add a directory with binaries PostgreSQL to environment variables (more details here)
-
To schedule regular execution, you can use the windows task scheduler
Press win + r and enter taskschd.msc
Select Create Basic Task
Then follow the steps of the wizard, specify the schedule, and in the "Action" section, specify "Start a program" and then the path to the backup.bat file
To check that everything is correct, find the task in the list and select Run
You can read more about Postgresql backups on Windows here

Batch file explore directory for PostgreSQL connection

Once again, I am asking for your help to manage a small batch script.
As announced, in a previous post, I'm used to work on linux and automation tasks are more obvious to me but I have to work today under Microsoft environment.
Writting a Batch file is definitly not an easy things.
I am able with this script to be connected to a PostgreSQL database and load data via ogr2ogr. Until then, no problem.
Currently, the path of the folder is hard written in my code, but I would like to have the possibility of choosing the working directory through windows explorer.
Also, concerning this directory, I would like to have the possibility to process the subfolders at the same time.
Here, below the piece of my *.bat code:
TITLE Upload Shapefile Files to PostgreSQL
#echo off
cls
color 9
echo ******************************************************************************
echo * Upload Shapefile Files to PostgreSQL *
echo ******************************************************************************
set /P Host=Please enter your Server Host (default:localhost):
set /P Port=Please enter your PGSQL port (default:5432):
set /p Database=Please enter your PGSQL Database Name (default:postgres):
set /P Schema=Please enter your Edigeo Schema (default:public):
set /P User=Please enter your PGSQL username (default:postgres):
set /P Password=Please enter your PGSQL password (default:postgres):
For /F %%H In ("C:\Users\stephj\Desktop\SHP\*.shp") do ogr2ogr -overwrite -t_srs EPSG:2154 -s_srs EPSG:2154 -f "PostgreSQL" PG:"host=%Host% port=%Port% user=%User% password=%Password% dbname=%Database%" -lco schema=%Schema%
pause
Thanks in advance for your time and your help.
Ok, here is a way. This will just demonstrate by echoing the files, you need to amend it to follow your command structure:
Note!! This is a batch/PowerShell hybrid script. It needs to be saved with a batch file extension, preferably .cmd:
#echo off
set "pshell="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Select Folder',0,0).self.path""
for /f "delims=" %%a in ('powershell %pshell%') do set "workdir=%%a"
for /r "%workdir%" %%i in (*.shp) do echo "%%~i"

How can i use Relative or Environment variable in Batch command

I am running below command to run Soapui Test suite and it is working fine
testrunner.bat -s"TestSuite4" "D:\Invesco\JP Groovy Code\ExploreGroovy.xml"
I ahve also used with below command and it is working fine as well
testrunner.bat -s"TestSuite4" "%USERPROFILE%\ExploreGroovy.xml"
Now I have added one Envrionment variable 'EnvP' and its value id 'D:\Invesco' and tried with following command but it is not working.
testrunner.bat -s"TestSuite4" "%EnvP%\ExploreGroovy.xml"
Can some one help me in this. I don't want to give hard coded path of any drive. Please suggest if anyone has any other solution.
Thanks.
the process starting testrunner.bat (probably explorer.exe?) must
know about the new variable. have you tried logging out and in again
after setting it?
if it is cmd, try finding the variable with set | find "EnvP". If it is not there, you need to start a new cmd session.
Use these commands and you should see why it fails:
#echo "%EnvP%"
#if not exist "%EnvP%\ExploreGroovy.xml" #echo Ouch!
#pause
testrunner.bat -s"TestSuite4" "%EnvP%\ExploreGroovy.xml"
#pause
For all my SoapUI projects I have multiple .bat scripts in the same location as the project.xml file to run different sets of test suites, and all of it goes into your source repository.
IF NOT DEFINED SOAPUI_ROOT SET SOAPUI_ROOT=%ProgramFiles%\SmartBear\soapUI-Pro-4.6.4
REM make certain we are where we _think_ we are
CD %~dp0
REM cleanup previous results
DEL /f /q *.log*
RMDIR /s /q results
REM run the tests
CALL "%SOAPUI_ROOT%\bin\testrunner.bat" -s"Smoke TestSuite" -fresults My-soapui-project.xml
REM determine if there are failures
IF errorlevel 0 (
ECHO All tests passed.
PAUSE
EXIT 0
) ELSE (
ECHO There are failures!
PAUSE
EXIT 100
)

MongoDB script to backup replication set locally to a Windows Server

I would like to make a Daily Backup of my MongoDB from a replication set running from Windows 2012 servers.
End goal would be to get a daily backup and write the backup to a remote or local share - Windows.
Can I batch the mongodump command?
Any help would be greatly appreciated!!
Sorry, it's a bit late but the following seems to work OK for me. The script dumps the database and compresses the output using 7-Zip.
1) Create backup script (backup.bat)
#echo off
REM move into the backups directory
CD C:\database_backups
REM Create a file name for the database output which contains the date and time. Replace any characters which might cause an issue.
set filename=database %date% %time%
set filename=%filename:/=-%
set filename=%filename: =__%
set filename=%filename:.=_%
set filename=%filename::=-%
REM Export the database
echo Running backup "%filename%"
C:\mongodb\mongodump --out %filename%
REM ZIP the backup directory
echo Running backup "%filename%"
"c:\Program Files\7-Zip\7z.exe" a -tzip "%filename%.zip" "%filename%"
REM Delete the backup directory (leave the ZIP file). The /q tag makes sure we don't get prompted for questions
echo Deleting original backup directory "%filename%"
rmdir "%filename%" /s /q
echo BACKUP COMPLETE
2) Schedule the backup
Open Computer Management
Go to Task Scheduler and select Create Task.
On the General tab, enter a description and select Run whether user is logged on or not if you want the task to run at night.
On the Triggers tab, select when you would like the task to run.
On the Actions tab, create a new action which points at your batch script.
I'm running on linux, not Windows 2012, but here is what I do. On one of the servers in the replica set, this script gets run every night via a cron job.
#config
BACKUPNAME=[backup file name]
DATAPATH=[path to mongo data folder]
DATESTAMP=$(date +"%Y-%m-%d")
FILENAME=backup.$BACKUPNAME.$DATESTAMP.tar.gz
TARPATH=$DATAPATH/$FILENAME
echo $DATESTAMP;
/etc/init.d/mongod stop
/usr/bin/mongodump --journal --dbpath $DATAPATH --out $DATAPATH/backup
tar czvf $TARPATH $DATAPATH/backup
rm -rf $DATAPATH/backup
/usr/bin/s3cmd put $TARPATH s3://[backup s3 bucket name]/$FILENAME
rm -f $TARPATH
/etc/init.d/mongod start
/scripts/prunebackups
I'm using s3cmd to send files to an S3 bucket on Amazon AWS, but you could just as easily copy the file anywhere. prunebackups is a script that deletes old backups from S3 based on how old they are.
On Windows I'd create a batch file that does similar tasks. In essence:
Stop mongod
run mongodump to generate the data
zip up the dumped data and move it somewhere
clean up files
start mongod again
You can then use Task Scheduler to run it periodically.
If you have other mongod instances in the replica set, you shouldn't run into any issues with downtime. The backup instance in my setup is never used for reads or writes, but only for backups and in case one of the other instances goes down.
MongoDB has documentation on different backup strategies: http://docs.mongodb.org/manual/administration/backup/
We chose the mongodump approach because for us it's cheaper to store dump backups than snapshots. This is the specific strategy we used: http://docs.mongodb.org/manual/tutorial/backup-databases-with-binary-database-dumps/. Good news is, we actually had to restore data from a backup to production once and it was pretty painless.

Visual Source Safe Automated Commit

I am currently attempting to set up an automated add/checkout/checking script using MS Visual Source Safe via command line. Online documentation is... lacking... and I was hoping that someone else had tried doing something like this in the past.
Before going any further, I am well aware that there are other, better alternatives to VSS, so please don't give "use SVN" as an answer.
The files I've got are a scripted version of our database schema, and looks like this in the repo:
$/project_name/DBScripts/servers/databases/object_types, where object_types are Tables, StoredProcedures, etc.
I am attempting to do the following:
1- Script all database object to files. This part is done and working correctly.
2- Add all new files to repo.
3- Commit all files that have changed. Make sure files do NOT remain checked out or read-only.
EDIT 2:
Removed old code again. Included current code below. Add works correctly, but the checkout command does NOT work on any files that were changed locally.
In this context, if I were to modify stored proc A, script it to file, then try running the batch commands below, all procs BUT A will be checked out.
I've included 2 examples of the checkout command. Neither is working...
set PATH=%path%;C:\Program Files\Microsoft Visual SourceSafe
set SSDIR=repo_path
cd DBScripts/server/database/StoredProcedures
ss cp $/project/DBScripts/server/database/StoredProcedures
for %%F in (*.*) do ss add %%~nF%%~xF -C- -I-N -K- -W
for %%F in (*.*) do ss checkout $/project/DBScripts/server/database/StoredProcedures/%%~nF%%~xF -C- -G- -M- -L+
ss checkout $/project/DBScripts/server/database/StoredProcedures *.* -C- -G- -M- -L+ -Vltemp
for %%F in (*.*) do ss checkin %%~nF%%~xF -C- -K- -P $/project/DBScripts/server/database/StoredProcedures -W
cd ../../../..
Note: SourceSafe's "-R" command is inconsistent. I'd rather loop through all subfolders manually and do "for %%F in (.)" commands.
Possible reason is the checkin command is executed before prior add/checkout commands are finished. Try checking if add is successful before the checkout command, and checking if checkout is successful before the checkin command.