Help in making a batch file

Hi,

I am currently doing the following operations on a windows 2012 server machine here are the steps

1. cd C:\Program Files\Tableau\Tableau Server\9.0\bin

2. tableaubackup.bat

3. aws s3 cp "D:\tableaubackups\tabmdbackup-2015-10-07.tsbak" s3://fanatics-tableau-prod/

Step 2 creates a file in the D:\ and the file is created with the name in the above format depending upon the date it was run

So I need a batch file from experts that I can run daily at 11:00 pm and ensure that the dailybackup of the metadata gets uploaded to s3. The only tricky part is ensuring that file for that particular day is picked (step 2 creates a file with the date in the file name. Also the batch file should create a log from which I can audit if all the steps were completed successfully and the backup was indeed done
anshumaEngineeringAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Assuming the step 2 .bat runs at same day, here's the code to get today's date and run the backup to aws.
Remove the ECHO to run for real:
REM Wed 10/07/2015
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set Date_YYYY-MM-DD=%%l-%%j-%%k
ECHO aws s3 cp "D:\tableaubackups\tabmdbackup-%Date_YYYY-MM-DD%.tsbak" s3://fanatics-tableau-prod/

Open in new window


You can use Task Scheduler to run .bat at desired time.
Basically, the Action setting is:
Program/script: cmd
Add arguments (optional): /c yourfile.bat
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, here's a script that you might want to build off of.  I added some logging in the script, and also worked in all the steps you want to automate.  You need to use a CALL before the tableaubackup.bat execution, so that when it finishes the main script continues.  Without it, when tableaubackup.bat finished, the first script would never finish.

I don't know if AWS that you execute with S3 CP ... is an EXE, or a BAT script.  If it is a BAT script too, then you will need a call in front of that as well.

Save this script someplace, perhaps D:\tableaubackups\awsbackup.bat, and then set up as a scheduled task in windows task scheduler.  I use a little trick that will seem a little cryptic but can actually really help troubleshooting problems, to capture logging info when this new script runs.  So in Task Scheduler, add a task, and for the ACTIONS, specify a PROGRAM of cmd, and then for the PARMS specifiy the following:

/c D:\tableaubackups\awsbackup.bat>>"D:\tableaubackups\awsbackup.log"2>>&1

The /c goes with the CMD program, and tells it to run the following command and then exit.  The rest is the command line we want to run, and starts with the BAT script (from below code) you create, and then specifies where to send the output of the script to.  This file will contain any logged info from the script or programs it runs.  I like to add the "2>>&1" to the end which also sends any error messages to the same log file.

Also set the start in directory for the scheduled task to the location of the scriptfile, say D:\tableaubackups

Let me know what questions you have about this, I'm sure there are some.  You can start by just crating the BAT script, and running it from a command prompt to make sure it works.


@echo off
setlocal EnableDelayedExpansion

REM Get current date in YYYY-MM-DD format
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined Today (
    set Today=%%A
    set Today=!Today:~0,4!-!Today:~4,2!-!Today:~6,2!
  )
)

REM Define location for log file, todays backup file, and AWS destination
set BaseDir=C:\Program Files\Tableau\Tableau Server\9.0\bin
set BackupFile=D:\tableaubackups\tabmdbackup-%Today%.tsbak
set AwsDest=s3://fanatics-tableau-prod/

REM Start of job log entry
echo =====================================================================
echo [%DATE% %TIME%] INFO - AWS Backup script started

REM Switch to backup folder
pushd %BaseDir%

REM Run backup
call tableaubackup.bat

If not exist "%BackupFile%" (
  echo [%DATE% %TIME%] ERROR - Missing backup file "%BackupFile%"
) else (
  REM Store in AWS
  echo [%DATE% %TIME%] INFO - Sending backup file "%BackupFile%" to AWS...
  aws s3 cp "%BackupFile%" %AwsDest% && (
    echo [%DATE% %TIME%] INFO - Send to AWS successful
  ) || (
    echo [%DATE% %TIME%] ERROR - Send to AWS failed
  )
)

REM Restore original current directory
popd

REM End of job log entry
echo [%DATE% %TIME%] INFO - AWS Backup script finished

Open in new window

~bp
anshumaEngineeringAuthor Commented:
Hello Bill

these are the contents of tableaubackup.bat , please incorporate them in your code so that this dependency can be completely handled from your code itself

tabadmin ziplogs -l -n -f -d
copy logs.zip D:\tableaubackups\logs
tabadmin backup D:\tableaubackups\tabmdbackup -d
tabadmin cleanup
Bill PrewIT / Software Engineering ConsultantCommented:
This should accomplish that:

@echo off
setlocal EnableDelayedExpansion

REM Get current date in YYYY-MM-DD format
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined Today (
    set Today=%%A
    set Today=!Today:~0,4!-!Today:~4,2!-!Today:~6,2!
  )
)

REM Define location for log file, todays backup file, and AWS destination
set BaseDir=C:\Program Files\Tableau\Tableau Server\9.0\bin
set BackupDir=D:\tableaubackups
set BackupFile=%BackupDir%\tabmdbackup-%Today%.tsbak
set AwsDest=s3://fanatics-tableau-prod/

REM Start of job log entry
echo =====================================================================
echo [%DATE% %TIME%] INFO - AWS Backup script started

REM Switch to backup folder
pushd %BaseDir%

REM Perform backup
tabadmin ziplogs -l -n -f -d
copy logs.zip %BackupDir%\logs
tabadmin backup %BackupDir%\tabmdbackup -d
tabadmin cleanup 

If not exist "%BackupFile%" (
  echo [%DATE% %TIME%] ERROR - Missing backup file "%BackupFile%"
) else (
  REM Store in AWS
  echo [%DATE% %TIME%] INFO - Sending backup file "%BackupFile%" to AWS...
  aws s3 cp "%BackupFile%" %AwsDest% && (
    echo [%DATE% %TIME%] INFO - Send to AWS successful
  ) || (
    echo [%DATE% %TIME%] ERROR - Send to AWS failed
  )
)

REM Restore original current directory
popd

REM End of job log entry
echo [%DATE% %TIME%] INFO - AWS Backup script finished

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.