Backup a sqlite3 database - database

I have a Python project where I use sqlite3 to save data.
I want to do a backup of the database ( I am really worried about datalock, cause my software will be used by like 10 peoples which will have to access to my database to write or read, and without luck, someone will try to access to the database at the bad moment, even if I will increase timeout)
If I do it manually with the Windows cmd, there is no problem.
v:
cd V:\directory\
sqlite3 mydatabase.db"
.backup backup_db.db
I try to do a batch file which will be call every hours by my python software to do it automatically.
I applied the same commands in it.
The batch file is launched, but the process stop after the opening of the database.
The dot command is not executed.
Where is my mistake?

Batch files do not work the way you think they do.
You currently think that after the line sqlite3 "mydatabase.db" you are in some kind of "database mode" and all the following lines in the batch file are passed to the sqlite3 process.
That's not the case.
Every line in a batch file is executed after the previous like has finished running.
The line sqlite3 "mydatabase.db" starts the sqlite3 process... and then waits until this process exits. Which never happens unless you do it manually by pressing a key.
And after that cmd.exe will try to execute the command .backup backup_db.db, but since that's not a command cmd.exe understands, it will fail with an error ("'.backup' is not recognized as an internal or external command, operable program or batch file.").
What you really want to do is create a script file and pass it to for SQLite for processing. This can be done by
redirecting a file into the sqlite3 process. Assume that create_backup.txt contains the commands to create a backup:
sqlite3 "mydatabase.db" < create_backup.txt
piping the file into the sqlite3 process, e.g. using type:
type create_backup.txt | sqlite3 "mydatabase.db"
alternatively you can use echo to output a command:
echo .backup backup_db.db | sqlite3 "mydatabase.db"
Obviously the echo method is easier for one-liners whereas the input redirect with < or writing out a file with type are easier to for more complex, multi-line operations.
Using the -init parameter of sqlite3.exe is another option you can try. Check the SQLite documentation.

The syntax for the sqlite3 executable is sqlite3 [db_file] [command]. So, in your batch file, you should include .backup on the same line.
v:
cd V:\directory\
sqlite3 mydatabase.db ".backup backup_db.db"

Related

Can't create Excel file from Octave script when running from batch file

Using Windows 10, Octave 4.4.1, and Excel 2016, I have an Octave script (call it "MyOctaveProgram.m") that takes data from a text file ("MyTextFile.txt"), which can be loaded with the uigetfile function, but for quicker access, I've created a batch file:
#echo off
C:\Octave\Octave-4.4.1\octave.vbs --force-gui --eval MyOctaveProgram("'%~d0%~p0'","'%~n0'")
cmd /c
The .bat file has the same name as the .txt file, other than the extension.
So far, this all works fine, and I can execute part of MyOctaveProgram.m via the GUI command line, or through the batch file. The difference comes in when I try to use a part of my code that creates a data table in Excel (with the COM interface), using
xls=xlsopen(ExcelFile)
xls=oct2xls(data...)
xls=oct2xls(more data...)
xls=oct2xls(etc...)
xlsclose(xls)
This works fine when I run MyOctaveProgram.m from the GUI command line (choosing MyTextFile.txt with the uigetfile function), but when I try to create the Excel file when running Octave through the batch script above, I get the following message:
(File pointer preserved. Try saving again later...)
This seems to produce no errors, and the session stays open (unlike the GUI, the batch-initiated Octave session will terminate if there's an error), but the Excel file is not created. There are no other Excel windows or tasks open, and I'm not trying to overwrite an existing file. Any advice would be appreciated!

How to automatically answer the user prompt from calling tsm command inside a .bat file

I am trying to write a set of tsm maintenance commands in .bat for performing some clean up on my Tableau Server.
The command tsm maintenance reset-searchserver, asks for a user prompt This operation will perform a server restart. Are you sure you wish to continue? (y/n): which I would like to answer automatically.
Stuff I have already tried:
I have already tried putting the command tsm maintenance reset-searchserver in a .bat file, say ABC.bat, and then typing echo y | ABC.bat on the command line. However, this did not work.
I also tried the method in which I saved the letter "y" in a text file, say Yes.txt, and then tying ABC.bat < Yes.txt on the command line. This did not work either.
Have also tried these two commands on the command line directly, they dont seem to work either:
echo y | tsm maintenance reset-searchserver and
tsm maintenance reset-searchserver<Yes.txt
you should use macro:
Edit macro file, put all your scripts and save as File.mac
Open administrative client and run:
C:\Program Files\Tivoli\TSM\baclient>dsmadmc -id=admin -pass=password macro c:\tmp\macro-file.mac

Executing bat file via SAS

I was wondering if it's possible to execute a bat file i created via SAS?
Reason for this is, unfortunately I do not have admin privileges to schedule the execution of my batch file on task scheduler when I'm not logged in...I need to be logged in for the batch file to execute.
On the contrary my team schedules jobs on SAS so I was wondering if there is a command that can execute my .bat file through the same concept?
I'm happy to accept any other solutions
Thanks.
I always suggest using a pipe fileref when calling OS commands, this way you can capture both STDOUT and STDERR.
data _null_;
infile "C: & C:\path\to\your.bat 2>&1" pipe;
input;
putlog _infile_;
run;
To explain the command:
1) First, change the drive (not necessary if SAS is on the same drive as the .bat file). This is the C: part.
2) Next, execute the file (C:\path\to\your.bat)
3) Finally, redirect STDERR to STDOUT (the 2>&1 part). This will show you any errors.
If you still get nothing, try executing the batch file manually using the same account as you use in SAS, and - on the same machine (eg where the SASApp server is hosted). In a batch environment this may be a batch user. In a Stored Process context it may be sassrv (or equivalent).
options noxwait noxsync;
x 'path\to\your\file.bat';

Batch Files - Closing Opened Text Files

I currently have a Windows batch file that runs an .exe file that uses a text file. I am trying to have the Windows batch file run the .exe file multiple times. This, however, requires the use of the same text files to read from. The command prompt gives me the error that the ".txt could not be opened" (I assume from this that it is already open.)
I am trying to see if there is a way in a .bat file to system call to kill that specific text file. The suggestions I see online are to use 'taskkill notepad.exe', but that returns "invalid argument" because the program doesn't open Notepad to use the text file.
Any suggestions would be appreciated.
It sounds like your existing script fails because the first instance of the exe is still open when the second instance starts.
One thing worth trying (and this depends on the nature of the application you are invoking) is to start the executable using the START /WAIT /B ... command. This makes the command interpreter wait for the program to exit before it moves onto the next command, so as long as nothing else is locking the text files you should be OK to move onto the next command.

Batch file to execute redis commands

I am trying to write below commands in batch file.
cd C:\Redis
redis-cli
select 6
file name is "xxx.bat"
It should connects to local redis instance and select database 6.
but it stops at second command "redis-cli". and does not even write second command "select 6"
on command prompt.
I mean i connects to server , but does not write next command.
I think i do not know something about executing commands in batch file.
I can run these commands outside of batch file without any problem.
Can somebody please spot the problem.
Regards
CMD does not pass the commands to the redis-cli.
You can test it with quit from the redis-cli, you will see a a CMD error for the select 6 command.
You should put all of your commands (without connecting with redis-cli) in a text file e.g test.txt and pass it all to to redis-cli. i.e:
type test.txt | redis-cli -x
p.s. there's no need to specify .exe or any other extension that is in you %pathext% variable, but it makes your scripts more readable.
Try
cmd /c "redis-cli select 6" >> output.txt
I need to specify exe with the command.
redis-cli.exe select 6
With my redis 2.4.6 on windows you can pass the command as an argument to the redis-cli executable. Here is my windows batch file:
SET REDIS_PATH=E:\Program Files\Redis
"%REDIS_PATH%\redis-cli" select 6
Of course you would change the redis path variable to wherever you have copied / installed Redis to.
Here is the output from running the windows batch file:

Resources