Home > Software design >  How to add datestamp to backup files in bash script
How to add datestamp to backup files in bash script

Time:11-25

I created such script it needed to be run everyday in cron:

db="SPECIFY_DB_NAME"
#specify collections
collection_list="<collection1> <collection2> <collection3>"
#if its running on local machine:
host=127.0.0.1

port="SPECIFY PORT"
#where to dump:
out_prefix=/Temp
for collection in $collection_list; do
    echo $collection
    out_dir="${out_prefix}/${db}_${collection}/"
    mkdir -p ${out_dir}
    mongodump --host $host --port $port --collection $collection --db $db --out ${out_dir}
done 

How to add to every file datestamp like : /Temp/collection.2021.11.22 /Temp/collection2.2021.11.22 ?

Thanks a lot

CodePudding user response:

If it's today's date that you want, simply call out to date with the desired format:

...
  out_dir="${out_prefix}/${db}_${collection}.$(date  %Y.%m.%d)/"
...

... which, for values of:

db="SPECIFY_DB_NAME"
collection=collection1

... and today's date, generates an "out_dir" value of:

/Tmp/SPECIFY_DB_NAME_collection1.2021.11.24/
  • Related