简体   繁体   中英

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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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