I have a shell script that I got from trante's answer in:
Linux shell script for database backup
The script runs correctly from the command line and creates the mysql backup.
However when it runs from a cron job the script executes and creates a the mysqldump file but the file is empty.
I have tried setting the PATH environment in the script but nothing I have tried works.
Regards
EDIT:
Adding script
#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/mybackupfolder"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=myuser --password=mypassword --default-character-set=utf8 mydatabase | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown myuser "$fullpathbackupfile"
chown myuser "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0
Finally I found the solution.
You have to put the path to mysql in the following line:
mysqldump --user=myuser --password=mypassword..
Which in my case is:
/opt/bitnami/mysql/bin/mysqldump --user=myuser --password=mypassword..
txt files that i download from a FTP server and fill a table with this files automatically with a .bat file.
I can already download the files and add only one file to the table here is my code so far:
#echo off
color 17
cd C:/Users/Silvia/Documents/Roboto
rename *.txt actual.txt
cd "C:\Users\Silvia\Documents\MySQL Server 5.6\bin"
mysql -h localhost -u root -ppass <C:\Users\Silvia\Documents\Roboto\crear.bat
pause
exit
And here is the crear.bat code:
use Operacion15;
create table db(PATENTE varchar(4), ADUANA varchar(3), PEDIMENTO varchar(7), RFC varchar(13));
LOAD DATA LOCAL INFILE "C:/Users/Silvia/Documents/Roboto/actual.txt"
INTO TABLE db
FIELDS TERMINATED BY '|'
(PATENTE, ADUANA, PEDIMENTO, RFC);
How can i make a cicle to run in all the downloaded files?
ill appreciate your help, thanks
For a first look how the FOR /F command works, start with next command typed in a command line window (you could use Copy and Paste:
for /F %G in ('dir /b "C:/Users/Silvia/Documents/Roboto\*.txt"') DO #echo %~fG
Then you could modify your code as follows:
#echo off
SETLOCAL
color 17
rem download directory
set "_pathDownload=C:\Users\Silvia\Documents\Roboto"
set "_pathToCrear=%_pathDownload%"
rem make sure a "backup_done" folder exists
set "_pathBackup=%_pathDownload%\backup_done"
md "%_pathBackup%\" 2>NUL
rem working directory
set "_pathMySQLbin=C:\Users\Silvia\Documents\MySQL Server 5.6\bin"
pushd "%_pathMySQLbin%"
rem LOCAL INFILE: elicit from crear.bat
for /F "tokens=5*" %%G in (
'type "%_pathToCrear%\crear.bat"^|FINDSTR INFILE'
) do (
set "_fileActual=%%~G"
)
rem main loop
for /F %%G in ('dir /b "%_pathDownload%\*.txt"') DO (
rem remove ECHO from next three ECHO-ed lines as soon as debugged
rem moreover: at same time, remove ^ ahead of < from mysql line
ECHO copy /B /Y "%%~fG" "%_fileActual%"
ECHO mysql -h localhost -u root -ppass ^<"%_pathToCrear%\crear.bat"
rem (optional) backup file treated
ECHO move /Y "%%~fG" "C:\Users\Silvia\Documents\Roboto\backup_done\"
ECHO(
rem previous ECHO( line: for ECHO-ed output only to make it easy to survey
)
pause
exit
Note:
backup_done folder: it's a sample name (as well as the path);
doubled % percentage sign for using in a batch file (%%G instead of %G);
items in a path are delimited with a \ backslash sign;
echo copy …, echo mysql … and echo move …: operational commands are ECHOed for debugging purposes only (to see eventual script behaviour): remove all echo prefix no sooner than code debugged;
there is a difference in path to the actual.txt file: …\Silvia\… in the batch file you have provided vs. …/Garber/… in the crear.bat code
Edit: code improved and commented
If your downloaded files allow to be merged before sending to mysql, then the main loop part in your script could be a bit more simple (with the only mysql call) as follows:
rem
rem include part of previous script here up to 'rem main loop'
rem
rem main loop
rem merge downloaded files
type nul > "%_fileActual%"
for /F %%G in ('dir /b "%_pathDownload%\*.txt"') DO (
rem remove ECHO from next ECHO-ed lines as soon as debugged
rem moreover: at same time, remove all ^ ahead of > from mysql line
if /I "%%~fG"=="%_fileActual%" (
rem no merge
) else (
ECHO type "%%~fG" ^>^> "%_fileActual%"
rem (optional) backup file treated
ECHO move /Y "%%~fG" "C:\Users\Silvia\Documents\Roboto\backup_done\"
ECHO(
)
)
mysql -h localhost -u root -ppass <"%_pathToCrear%\crear.bat"
pause
exit
I downloaded some website pages with wget utility, but html pages contain too much information which is not needed. I want files to contain only text before
</article> tag. I suspect that it is possible to do it with grep command, but which parameters do I need? And how to apply such command to all files in a dir?
here's the script
for i in *.htm; do (cat $i | grep -i "</article>" -B 9999) > $i; done;
I am working on java desktop application in which i use MYSQL database, but i have a problem , i want to embed MYSQL database, for this i want a script to install the MYSQL, i need Help to install MYSQL from batch file (windows).
i am using this script
#echo off
echo Installing MySQL Server. Please wait...
msiexec /i "mysql-installer-community-5.6.14.0.msi" /qn
echo Configurating MySQL Server...
"%ProgramFiles%\MySQL\MySQL Server 5.6\bin\mysqlinstanceconfig.exe"
-i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER
DatabaseType=MYISAM Port=3306 Charset=utf8
echo Installation was successfully
i get the error,"The system cannot find the path specified".
Any help will be appreciated.
I suspect that %ProgramFiles% is pointing to the wrong folder.
Try #echo %ProgramFiles% from a file to see what folder it is looking in. You have to make sure that it is not in the Program Files x86 folder.
I found this link also, maybe it can help you out?
Source: how to get program files x86 env variable?
EDIT
To be just sure, can you try it with the full path instead of the system variable?
Like this,
#echo off
echo Installing MySQL Server. Please wait...
msiexec /i "mysql-installer-community-5.6.14.0.msi" /qn
echo Configurating MySQL Server...
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqlinstanceconfig.exe"
-i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER
DatabaseType=MYISAM Port=3306 Charset=utf8
echo Installation was successfully
or even
#echo off
echo Installing MySQL Server. Please wait...
msiexec /i "mysql-installer-community-5.6.14.0.msi" /qn
echo Configurating MySQL Server...
cd "C:\Program Files\MySQL\MySQL Server 5.6\bin\" <-- set folder first, then run executeable
mysqlinstanceconfig.exe
-i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER
DatabaseType=MYISAM Port=3306 Charset=utf8
echo Installation was successfully
My batch file complete:
#echo off
cls
echo ==========================================
echo MySQL Server - Installation - v.17/03/2014
echo ==========================================
echo .
echo .
rem ------------------------------------------------
echo Installing. Wait ...
msiexec /i "mysql-5.5.28-win32.msi" /qn
echo Done.
rem ------------------------------------------------
echo .
echo .
rem ------------------------------------------------
echo Configurating. Waiting ...
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin\"
mysqlinstanceconfig.exe -i -q ServiceName=MySQL RootPassword=mypassword ServerType=DEVELOPER DatabaseType=INODB Port=myport Charset=utf8
echo Done.
rem ------------------------------------------------
echo .
echo .
rem ------------------------------------------------
echo Creating access to user. Waiting ...
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin\"
mysql -uroot -pmypassword --execute="GRANT ALL PRIVILEGES ON . TO 'root'#'%%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;"
mysql -uroot -pmypassword --execute="FLUSH PRIVILEGES;"
echo Done.
rem ------------------------------------------------
echo .
echo .
echo Installation ready.
echo .
echo .
pause
I need to send email with html format. I have only linux command line and command "mail".
Currently have used:
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv
But in my mail-agent i get only plain/text.
This worked for me:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo#example.com
My version of mail does not have --append and it too smart for the echo -e \n-trick (it simply replaces \n with space). It does, however, have -a:
mail -a "Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.html
Send email using command Line
This answer is over 11 years old, these days I use python's import ezgmail for a 4 line plug, auth and play solution
Create a file named tmp.html with the following contents:
<b>my bold message</b>
Next, paste the following into the command line (parentheses and all):
(
echo To: youremail#blah.com
echo From: el#defiant.com
echo "Content-Type: text/html; "
echo Subject: a logfile
echo
cat tmp.html
) | sendmail -t
The mail will be dispatched including a bold message due to the <b> element.
Shell Script
As a script, save the following as email.sh:
ARG_EMAIL_TO="recipient#domain.com"
ARG_EMAIL_FROM="Your Name <you#host.com>"
ARG_EMAIL_SUBJECT="Subject Line"
(
echo "To: ${ARG_EMAIL_TO}"
echo "From: ${ARG_EMAIL_FROM}"
echo "Subject: ${ARG_EMAIL_SUBJECT}"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html; charset='utf-8'"
echo
cat contents.html
) | sendmail -t
Create a file named contents.html in the same directory as the email.sh script that resembles:
<html><head><title>Subject Line</title></head>
<body>
<p style='color:red'>HTML Content</p>
</body>
</html>
Run email.sh. When the email arrives, the HTML Content text will appear red.
Related
How to send a html email with the bash command "sendmail"?
On OS X (10.9.4), cat works, and is easier if your email is already in a file:
cat email_template.html | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl#marx.com
The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.
Try:
mail --append="Content-type: text/html" -s "Built notification" address#example.com < /var/www/report.csv
--append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the To and Subject headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address#example.com automatically becomes the To).
With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.
The script I use (~/bin/sendmail-hook):
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $#
This script changes the values in the mail header as follows:
Content-Type: to text/html; charset=utf-8
Content-Transfer-Encoding: to 8bit (not sure if this is really needed).
To send HTML email:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address#example.com < /var/www/report.csv
Very old question, however it ranked high when I googled a question about this.
Find the answer here:
Sending HTML mail using a shell script
I was struggling with similar problem (with mail) in one of my git's post_receive hooks and finally I found out, that sendmail actually works better for that kind of things, especially if you know a bit of how e-mails are constructed (and it seems like you know). I know this answer comes very late, but maybe it will be of some use to others too. I made use of heredoc operator and use of the feature, that it expands variables, so it can also run inlined scripts. Just check this out (bash script):
#!/bin/bash
recipients=(
'john#example.com'
'marry#not-so-an.example.com'
# 'naah#not.this.one'
);
sender='highly-automated-reporter#example.com';
subject='Oh, who really cares, seriously...';
sendmail -t <<-MAIL
From: ${sender}
`for r in "${recipients[#]}"; do echo "To: ${r}"; done;`
Subject: ${subject}
Content-Type: text/html; charset=UTF-8
<html><head><meta charset="UTF-8"/></head>
<body><p>Ladies and gents, here comes the report!</p>
<pre>`mysql -u ***** -p***** -H -e "SELECT * FROM users LIMIT 20"`</pre>
</body></html>
MAIL
Note of backticks in the MAIL part to generate some output and remember, that <<- operator strips only tabs (not spaces) from the beginning of lines, so in that case copy-paste will not work (you need to replace indentation with proper tabs). Or use << operator and make no indentation at all. Hope this will help someone. Of course you can use backticks outside o MAIL part and save the output into some variable, that you can later use in the MAIL part — matter of taste and readability. And I know, #!/bin/bash does not always work on every system.
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
In your case would be:
mail -aContent-Type:text/html -s "Built notification" address#example.com < /var/www/report.csv
you should use "append" mode redirection >> instead of >
Try with :
echo "To: address#example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "MIME-Version: 1.0" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "Content-Disposition: inline" >> /var/www/report.csv
echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv
mail -s "Built notification" address#example.com < /var/www/report.csv