I am assuming this is a simple issue, but I don't have any one to double check my work.
Here's my bash script
#!/bin/bash
# establish date format and dump name
DATE=$(date +"%Y%m%d-%H%M")
DUMPFILE=$DATE.dump
# path to log dir and name of output log file
LOGDIR=/opt/mongodb/backups/logs
DBLOG=$DATE-dump.log
# backup the database and output to a dump file, redirect output to a log
docker exec -i mongodb sh -c "mongodump --archive" > \
$DUMPFILE 2> \
$LOGDIR/$DBLOG
# archive the dump file and the file uploads
ARCHIVENAME=$DATE.tgz
ARCHIVELOG=$DATE-archive.log
tar -czvf $ARCHIVENAME /opt/mongodb/$DUMPFILE /opt/mongodb/files &> \
$LOGDIR/$ARCHIVELOG
Where I'm stuck, the script correctly outputs the dump log. However when it gets to the end I receive
./backup.sh: line 20: /opt/mongodb/backups/logs/20250328-0942-archive.log: No such file or directory
I've attempted using the absolute path instead of the of the var:
tar -czvf $ARCHIVENAME /opt/mongodb/$DUMPFILE /opt/mongodb/files &> \
/opt/mongodb/backups/logs/$ARCHIVENAME
But I get the same error. I'm assuming this is either my bad understanding of redirects, tar, or a syntax error. Any help would be appreciated.
EDIT: A file called "' '" gets created each time I run the script. If run
cat ' '
I see that file is the log, so it's does redirect the output to a file, just not in the desired manner.
ls -l /opt/mongodb/backups/logs
), can you see the expected log files?./backup.sh
) or with an explicit interpreter (bash backup.sh
)?./backup.sh
)