简体   繁体   English

将文本从bash脚本插入mysql数据库并丢失格式

[英]Insert text from bash script to mysql database and losing format

I writing a script, which run backup with rsync and the output of rsync insert to database. 我编写了一个脚本,该脚本使用rsync运行备份,并将rsync的输出插入数据库。 Its working well except one little thing. 除一件小事外,它运作良好。 The text in the DB is one line and there is not 'new line' bunch of spaces or tabulator. 数据库中的文本是一行,并且没有“新行”一堆空格或制表符。 However if I writing the query to the screen, is fine. 但是,如果我将查询写到屏幕上,就可以了。 Its looking what i want in db cell. 它看起来我想要在数据库单元格。

Can anyone help in this issue? 任何人都可以帮助解决这个问题吗?

Here is the script: 这是脚本:

#!/bin/bash

DATUM=$(date +%Y-%m-%d)
IDO=$(date +%H:%M:%S)
LOG_FAJL="/media/2TB/MENTES_PC/log/"$DATUM"_"$IDO"_rsync_2TB.log"
LOG_FAJL_SED="/media/2TB/MENTES_PC/log/"$DATUM"_"$IDO"_rsync_2TB_SED.log"
CEL="/media/BACKUP_2TB/"
FORRAS="/media/2TB/"
KIZARAS1="--exclude='/media/2TB/lost+found/'" -- exclude='/media/2TB/.Trash-1000/'
DB_USER="user"
DB_PASSWORD="secret"
DB="xx"
TABLA="table"

if [ ! -f "$LOG_FAJL" ];
then
    printf "%b"     "\\\\n\\n\\t\\t\\t\\t\\t\\t\\t\\t##########################################" > "$LOG_FAJL"  # more fancy header...    
fi
printf "%b" "\\n\\n\\n   A mentés készült: $(date +%Y-%m-%d_%H:%M:%S)  \\n" >> "$LOG_FAJL"
printf "%b" "   *************************************\\n" >> "$LOG_FAJL"
KEZDES=$(date +%s)
printf "%b" "\\n   "$FORRAS" mentése ide: "$CEL >> "$LOG_FAJL"
printf "%b" "\\n   ==========================================\\n\\n" >> "$LOG_FAJL"
rsync -azvh --delete "$KIZARAS1" --stats --human-readable $FORRAS $CEL >> "$LOG_FAJL"
VEGE=$(date +%s)
ELTELT=$(($VEGE-$KEZDES))
printf "%b" "\\n\\n   A mentés tartott: " >> "$LOG_FAJL"
printf "%02dh:%02dm:%02ds" "$(($ELTELT/3600))" "$(($ELTELT%3600/60))"         "$(($ELTELT%60))"  >> "$LOG_FAJL"
printf "%b" "-ig\\n   ********************************" >> "$LOG_FAJL"

cat "$LOG_FAJL" | sed "s/'/\\\'/g" > "$LOG_FAJL_SED"
QUERY="INSERT INTO "$TABLA" (datum,ido,log) VALUES ('"$DATUM"','"$IDO"','"$(cat "$LOG_FAJL_SED")"');"
echo "$QUERY"
mysql -u$DB_USER -p$DB_PASSWORD $DB -e "$QUERY"

Curious what application you are using to view the inserted data, Mysql Workbench, Squirrel, etc? 想知道您正在使用哪个应用程序查看插入的数据,Mysql Workbench,Squirrel等?

I suspect that the newline/carriage return is actually there, it is just that the app is not showing them. 我怀疑换行符/回车符实际上在那里,只是该应用没有显示它们。

You can use mysqldump to extract the data to a file and see if the newlines are in the output. 您可以使用mysqldump将数据提取到文件中,并查看换行符是否在输出中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM