Tar file with newly created file

Hello guys
thanks for this helpful forum.

I'm new to scripting and doing a little script that dump a sql database with some extra files from a VM of the company I'm in, and create a tar archive to be saved on our serve.

How can I have the tarball to be created with the files generated in the same script?
I keep getting: "cannot extract file, archive empty"

I've tried a wait 10 but that doesn't help.

#!/bin/bash
#
#VARIABLES
#working directory
wd=/opt/fs_db_backup/
if [ ! -d $wd ] ; then
   mkdir $wd
fi
runDate=`date +%d_%m_%y-%H%M%S`
vers=$(/home/leo/bin/leostatus | grep vers | awk '{print $5}' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g")
#DUMPING THE DATABASE
/usr/local/pgsql/bin/pg_dump -U leo -f $wd/sql
#CREATES FILE NEEDED FOR RESTORATION
touch $wd/version
touch $wd/md5
#populate and change permissions
echo -n $vers >> $wd/version
#respect the order 
cd $wd
md5sum sql >> md5
md5sum version >> md5
chmod -R 640 $wd
#getting everything ready for archive
#mv /opt/fs_db_backup/* /opt/fs_db_backup/tar
#create tar file for backup with absolue path
tar -czvf leo_db_backup-"$runDate".tgz /opt/fs_db_backup/*
#chmod -R 660 leo_db_backup-*
#rsync to server
#rsync -ravptx $tarpath/leo* root@server01/
#weekly retention?

Thank you!

Things that I see -

  1. execute cd or do what it takes to make the process current working directory NOT to be the directory you are tarring up. Just a good idea.

  2. have tar write the archive to /tmp or some other directory, not $wd, then mv the archive file into $wd if that is its final resting place. What happens in your code now is tar creates the file because of the c option, then tries to backup the file it has open. My opinion anyway.

Good shout. Ended up mv files into subfolders and tar accordingly

what a brainfart!

this can be closed