GurhanCagin
GurhanCagin

Reputation: 185

Iterate in directory to backup sqlite tables in linux

I write a script to backup sqlite files in directory, but I am adding more database to directory and want to put in for loop the script but I stuck building the script as in example below:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DATE=`date +%Y%m%d`
for fn in $DIR/*.db; do
echo $fn ".backup '$fn_$DATE.backup'"
done

when I run this code to check how I it works:

/db/buyuk_sozluk.db .backup '20190216.backup'
/db/doviz.db .backup '20190216.backup'
/db/football.db .backup '20190216.backup'
/kktc.db .backup '20190216.backup'
/db/ulkeler.db .backup '20190216.backup'

but I am expecting to get output like below:

/db/ulkeler.db .backup '/db/ulkeler.db_20190216.backup'

I am not able to ad file name before the date, I couldn't figure out why its working for first $fn but not for the second one. After I will manage to make it work, I will change the echo with sqlite3 command. Thanks a lot.

Upvotes: 0

Views: 93

Answers (1)

Poshi
Poshi

Reputation: 5762

That's because you don't have a variable called fn_. Probably you were trying to use fn. Try with

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DATE=$(date +%Y%m%d)
for fn in $DIR/*.db
do
    echo "${fn} .backup '${fn}_${DATE}.backup'"
done

Upvotes: 1

Related Questions