Escape password in mysqldump command - passwords

I am using this;
mysqldump -u userabc -pabc123 dbname |
gzip > /var/backups/archives/mysql/dbname_$(date +\%d-\%m-\%Y_\%T).sql.gz
This works but if the password contains a ^ for example it fails, how can I escape this character and still have mysqldump work with the -p flag;
mysqldump -u userabc -pabc^123 dbname |
gzip > /var/backups/archives/mysql/dbname_$(date +\%d-\%m-\%Y_\%T).sql.gz

quote the password
mysqldump -u fred7 -p'asdf^555^666'
if any of the following * ? [ < > & ; ! | $ ( ) perhaps ^ too

Related

Why is my script not printing output on one line?

This is an image of what I'm asking for
I am using the following -echo- in a script and after I execute, the output format is as shown below:
`echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile`
output: UPDATE table1 SET table1_f1='Fname' ,table1_f2='Lname' where table1_f3='id ';
the '; is appearing on a new line, why is that happening?
The variable $id in your shell script actually contains that newline (\n or \r\n) at the end; so there isn't really anything wrong in the part of the script you've shown here.
This effect is pretty common if the variable is created based on external commands (update:) or by reading external files as you are here.
For simple values, one way to strip the newline off the end of the value, prior to using it in your echo is:
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
or for scripts that already rely on a particular bash IFS value:
OLDIFS="${IFS}";
IFS=$'\n\t ';
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
IFS="${OLDIFS}";

Need to remove trailing .000 in sql output file

I have this query, and in the table/column ttransactionlog_1/occurdatetime, it returns the time value with a .000 at the end, can someone tell me how to remove that from each row? here is a line from the outputfile showing the .000 at the end of the time
0,112213,2021-03-11 14:00:00.000,Santiago,Melody,AdminClock.
Here is the query:
sqlcmd -S clock\punch -d test -U xxx -P xxxxx -Q "Select Distinct TTransactionLog_1.DecisionTimeInterval, TTransactionLog_1.UserID, TTransactionLog_1.OccurDateTime, TTransactionLog_1.lastname, TTransactionLog_1.firstname, TSystemLog1.Name
From TTransactionLog_1 Inner join TSystemLog1 On TTransactionLog_1.NodeID=TSystemLog1.NodeID where TSystemLog1.NodeID = 3 and TTransactionLog_1.OccurDateTime > = dateadd(hh, -1, getdate())
" -s "," -h-1 -W -o "C:\ATR\adminreport.csv"
Thanks in advance!
Does just using Convert on the OccurDateTime value work for you?
Example
declare #OccurDateTime datetime='20210311 14:00:00.000'
select Convert(varchar(19),#OccurDateTime,121)

SQLCMD: Pass parameter database startig with number to script using powershell

From powershell we are running the following command to create a Database. The database name starts with a number:
SQLCMD -S Z0EUW1WLSQL031\PRUEBA -U rdb00001 -P "1234" -v datos="`"D:\Datos\`"" dbName="3DataBase" log="`"E:\Logs\`"" collation="Latin1_General_CI_AS" -i 10_CreateDB.sql -o 10_CreateDB.log
The script "10_CreateDB.sql" contains this:
CREATE DATABASE $(dbName) ON PRIMARY
( NAME = N'$(dbName)', FILENAME = N'$(datos)$(dbName).mdf' )
LOG ON
( NAME = N'$(dbName)_log', FILENAME = N'$(log)$(dbName)_log.ldf' )
COLLATE $(collation)
GO
We are getting this error:
Msg 102, Level 15, State 1, Server Z0EUW1WLSQL031\PRUEBA, Line 1
Incorrect syntax near '3'.
$ArgumentList = #(
'-S Z0EUW1WLSQL031\PRUEBA',
'-U rdb00001',
'-P "1234"',
'-v datos="D:\Datos\"',
'dbName="3DataBase" ',
'log="E:\Logs\"',
'collation="Latin1_General_CI_AS"',
'-i 10_CreateDB.sql',
'-o 10_CreateDB.log'
)
Start-Process "fullPathHere\SQLCMD.exe" -ArgumentList $ArgumentList

Postgres copy to TSV file with header

I have a function like so -
CREATE
OR REPLACE FUNCTION ind (bucket text) RETURNS table (
middle character varying (100),
last character varying (100)
) AS $body$ BEGIN return query
select
fname as first,
lname as last
from all_records
; END;
$body$ LANGUAGE PLPGSQL;
How do I output the results of select ind ('Mob') into a tsv file?
I want the output to look like this -
first last
MARY KATHERINE
You can use the COPY command
example:
COPY (select * from ind('Mob')) TO '/tmp/ind.tsv' CSV HEADER DELIMITER E'\t';
the file '/tmp/ind.tsv' will contain you data
Postgres doesn't allow copy with header for tsv for some reason.
If you're using a linux based system you can do it with a script like this:
#create file with tab delimited column list (use \t between each column name)
echo -e "user_id\temail" > user_output.tsv
#now you can append the results of your query to that file by copying to STDOUT
psql -h your_host_name -d your_database_name -c "\copy (SELECT user_id, email FROM my_user_table) to STDOUT;" >> user_output.tsv
Alternatively, if your script is long and you don't want to pass it in with -c command you can use the same approach from a .sql file, use "--quiet" to avoid notices being passed into your file
psql --quiet -h your_host_name -d your_database_name -f your_sql_file.sql >> user_output.tsv

Export Vertica query result to csv file

I`m working with Vertica. I try to export data from SELECT query into csv. I tried making it with sql query:
SELECT * FROM table_name INTO OUTFILE '/tmp/fileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
I got an error:
[Vertica][VJDBC](4856) ERROR: Syntax error at or near "INTO"
Is there a way to export a query result to a csv file? I prefer not to use vsql, but if there no other way, I will use it. I tried the following:
vsql -c "select * from table_name;" > /tmp/export_data.txt
Here is how you do it:
vsql -U dbadmin -F ',' -A -P footer=off -o dumpfile.txt -c "select ... from ... where ...;"
Reference: Exporting Data Using vsql
Accordingly to https://my.vertica.com/docs/7.1.x/HTML/Content/Authoring/ConnectingToHPVertica/vsql/ExportingDataUsingVsql.htm
=> SELECT * FROM my_table;
a | b | c
---+-------+---
a | one | 1
b | two | 2
c | three | 3
d | four | 4
e | five | 5
(5 rows)
=> \a
Output format is unaligned.
=> \t
Showing only tuples.
=> \pset fieldsep ','
Field separator is ",".
=> \o dumpfile.txt
=> select * from my_table;
=> \o
=> \! cat dumpfile.txt
a,one,1
b,two,2
c,three,3
d,four,4
e,five,5
By following way you can write to CSV file as comma separated and no footer.
vsql -h $HOST -U $USER -d $DATABASE -w $PASSWORD -f $SQL_PATH/SQL_FILE -A -o $FILE -F ',' -P footer=off -q