MySQLDump batch file is empty while the power shell version has content - powershell

I would like to use windows task scheduler to backup tables from a MySQL database every night. I created a .bat file that runs via the task scheduler. It creates the file but the file is empty. Here is the code:
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" --host=localhost --protocol=tcp --user=root --password=password --set-gtid-purged=OFF --flush-privileges=TRUE --port=3306 --default-character-set=utf8 --skip-triggers "maintenance" "safe_main" > "D:\testDump2.sql"
This will create the file and name it appropriately but the file is empty. This happens even when I run the bat file as an admin.
However when I run the same line in Windows Powershell it will create the same file with content.
The end result needs to be an sql file with data every night. Any help would be appreciated.

Related

Run individual command as another windows user within batch file

I have the following batch file (in Jenkins). I would like to run only "SqlPackage.exe" command as another windows user. I have the domain user id and password for it.
Batch file:
CD Folder_Name
DEL Another_Folder
SqlPackage.exe /Action:Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:Server1 /op:Publish.sql
Copy Publish.sql Somewhere_Folder
I understand I can run the batch file as another user using windows runas feature. However, in this case, I would like to only run an individual command (sqlpackage.exe).
Is it possible?

PostgreSQL copy 0?

I have written a simple batch script which loops a directory and echoes some details about each file.When I view its results in the CMD terminal or output it to some file, I can view the results as expected.
The problem comes with PostgreSQL: when I try to import its results into a table, executing the following command:
copy schema.table(field) from program 'C:\\...\\my_bat.bat' with CSV header delimiter E'\t';
It imports 0 results, whereas if I run the same command pointing to a similar batch file in another directory, it works as expected.
What's going on? I am using windows.
Update: I have tried running the copy command calling program again on another batch script and this time, only a part of the string output is being imported.
The service user postgres needs sufficient permissions to run the program.
I remember that it was hard to change settings for that account on windows XP, I have not tried on more recent windows - service users are hidden by most GUI tools.

Postgresql failed to install correcty

I have been trying to install PostgreSQL 9.2 for the last couple of days. I guess I tried everything that I could find online but the service still will not start. I am currently running Windows 7 SP1. I used to have PostgreSQL 8.3 installed and working fine but a couple of weeks ago I decided to uninstall it. I thoroughly uninstalled it (delete the postgres user and the data file) and now every time I try to install any version I get this non-fatal error at the end of the setup when the loading of the database modules takes place.
I managed to see that the postgresql service does not start or as windows puts it starts then stops.
This is the relevant part of the log file of the installation:
Successfully processed 771 files; Failed processing 0 files
initcluster.vbs ran to completion
Script stderr:
Configuring database server startup...
Executing cscript //NoLogo "C:\Program Files (x86)\PostgreSQL\9.2\installer\server\startupcfg.vbs" 9.2 "NT AUTHORITY\NetworkService" "****" "C:\Program Files (x86)\PostgreSQL\9.2" "C:\Program Files (x86)\PostgreSQL\9.2\data" "postgresql-9.2"
Script exit code: 0
Script output:
startupcfg.vbs ran to completion
Script stderr:
Creating menu shortcuts...
Executing cscript //NoLogo "C:\Program Files (x86)\PostgreSQL\9.2\installer\server\createshortcuts.vbs" 9.2 "postgres" 5432 "PostgreSQL 9.2 (x86)" "C:\Program Files (x86)\PostgreSQL\9.2" "C:\Program Files (x86)\PostgreSQL\9.2\data" "postgresql-9.2"
Script exit code: 0
Script output:
Start FixupFile(C:\Program Files (x86)\PostgreSQL\9.2\scripts\serverctl.vbs)...
Opening file for reading...
Closing file (reading)...
Replacing placeholders...
Opening file for writing...
Closing file...
End FixupFile()...
Start FixupFile(C:\Program Files (x86)\PostgreSQL\9.2\scripts\runpsql.bat)...
Opening file for reading...
Closing file (reading)...
Replacing placeholders...
Opening file for writing...
Closing file...
End FixupFile()...
createshortcuts.vbs ran to completion
Script stderr:
Starting the database server...
Executing cscript //NoLogo "C:\Program Files (x86)\PostgreSQL\9.2\installer\server\startserver.vbs" postgresql-9.2
Script exit code: 0
Script output:
Starting postgresql-9.2
Service postgresql-9.2 started successfully
startserver.vbs ran to completion
Script stderr:
Loading additional SQL modules...
Executing cscript //NoLogo "C:\Program Files (x86)\PostgreSQL\9.2\installer\server\loadmodules.vbs" "postgres" "****" "C:\Program Files (x86)\PostgreSQL\9.2" "C:\Program Files (x86)\PostgreSQL\9.2\data" 5432
Script exit code: 2
Script output:
Installing the adminpack module in the postgres database...
Executing 'C:\Users\George\AppData\Local\Temp\radA5996.bat'...
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Failed to install the 'adminpack' module in the 'postgres' database
loadmodules.vbs ran to completion
Script stderr:
Program ended with an error exit code
Error running cscript //NoLogo "C:\Program Files (x86)\PostgreSQL\9.2\installer\server\loadmodules.vbs" "postgres" "****" "C:\Program Files (x86)\PostgreSQL\9.2" "C:\Program Files (x86)\PostgreSQL\9.2\data" 5432 : Program ended with an error exit code
[10:34:28] Write the data directory to the ini file...
[10:34:28] Write the port number, service ID, locale and superuser to the ini file...
[10:34:28] Write ServiceID(postgresql-9.2) to registry/ini...
[10:34:28] Write Superuser to ini file and windows registry...
[10:34:28] Write Service Account to ini file and windows registry...
[10:34:28] Write the server description to the ini file...
[10:34:28] Write the server branding to the ini file...
[10:34:28] Write the 'whether stack-builder is disabled or not' to the ini file...
[10:34:29] Finished running post-installation/upgrade tasks.
Creating Uninstaller
Creating uninstaller 25%
Creating uninstaller 50%
Creating uninstaller 75%
Creating uninstaller 100%
Installation completed
I had the same problem. At one point it needs to get a package via internet connection. This is not right because the install package s/b able to install without a connection but that's how it is. Your firewall is blocking the install. Here's what you need to do:
Shut Down Windows Firewall (Control Panel\All Control Panel Items\Windows Firewall\Customize Settings)
If you use this great program shut down Firewall Control (Exit from task bar or Use Task Mgr to kill UI & Service - 2 items)
For downloading Selected Packages:
Shut Down Key Protection Services:
Shut Down Base Filtering (Leaves you unprotected! - Dependencies will.restart when BFE is restarted)
Base Filtering Engine
IKE and AuthIP IPsec Keying Modules
IPsec Policy Agent
ICS
...
Don't forget to restart all protections.
I faced similar issues of Pg 9.2.4 installer on Win7 x64. It turned out that
'adminpack' module did not load due to stopped postgres service. It starts and then
terminates almost immediately. You may check this running Pg service manually to see if it
can load. If it cant this may be from missing config files from "data" directory. initdb
command cant create valid cluster and copy corresponding *.conf files. I tried to dig into
details and the installer has complicated checks of user permissions which could fail in your (and my) case.
Just try installing Pg from zip binaries. It took only the following commands (less than
20 minutes). Original article.
1) Create "postgres" windows user (with no admin rights) and granted it full privileges on
your Pg folder and "data" folder. To do this from command line run "cmd" as administrator (press Ctrl-Shift-Enter instead of Enter to run "cmd") and issue: "net user postgres
/add". Then right click on your Pg folder (and "data" folder if it is outside) and grant all permissions to "postgres".
2) Install Microsoft Visual C++ 20xx Redistributable Package which comes with one click Pg
installer (vcredist_x64.exe from Users\"your user"\AppData\Local\Temp\postgresql_installer_**** - folder created by one click Pg installer on first run).
3) Unzip Pg binaries to your Pg folder and "cmd" as administrator to its bin directory.
4) Run to init cluster: initdb -U postgres -A password -E utf8 -W -D "path to your Pg folder"\data
5) Run to install windows service: pg_ctl register -N "postgresql" -U ".\postgres" -D "path to your Pg folder/data" -w
6) Open windows services list and open "postgresql" service properties. Then set password for windows user postgres there. Try starting your new Pg service. If it starts then everything should be ok.
7) Open bin\pgAdmin3, connect to local server and open postgres database and its metadata. You should see a complaint about missing admin services and a propose to correct this. Just press Ok - 'adminpack' should be installed.
Install postgres only from windows account written in english. For example installation from user "Аня" always fail.
Solution is to shutdown the windows firewall before installing. After the installation you can enable the windows firewall.
Try installing outside of "C:\Program Files" which has special permissions on Windows.
We had similar issues and simply by installing into C:\Postres92 everything works right out of the box.
I had the same problem, caused by restricted access rights in the Program Files directory, due to the fact that the data directory was C:\Program Files (x86)\PostgreSQL\9.2\data
Changing the data directory to C:\ProgramData\PostgreSQL\9.2\data resolved the problem.

Running a .bat file in Scheduled Task

I am using a Windows Server 2008 machine where I need to run a batch file as a Scheduled Task. The scheduled batch file is not running, but when I click the batch file manually its running with no problem.
The Content of the batch file is as follows:
cd E:\SOAPUI\soapui-2.0.2\bin
e:
testrunner.bat -sDev_Test -a -j -f "D:\DocRepos\LensWebSvc\SOAPUI-Reports(Lens)"
"D:\DocRepos\LensWebSvc\SOAPUI\LensWebServiceTest(Lens)-soapui-project.xml"
Can anyone help me to run the batch file in scheduled task?
It could be a few things. Make sure you've set the task to start in the same path that the batch file is located. Also, make sure the account running the task has permission to run the task and access the location of the batch file.

Redirect input from another command in windows batch files

In linux I can do something like this:
mysql -u user -p=pass somedb <(echo "create database foo;")
How can I do that with windows batch scripts?
Basically, I want to make a batch file that runs a sql script without having to keep the script in a separate file.
Thanks
One way is to echo the SQL commands into a file, run the mysql command with option to include the SQL file, and then remove the file (if you really don't want it.)
You can do
echo create database foo;|mysql ...
just fine, but for multiple lines you really want to make a temporary file you just pass to MySQL to read.