Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add datestamp to backup files in bash script

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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/
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading