Skip to main content
Became Hot Network Question
Slightly more precise title. It’s the shell doing the redirecting
Link
Kusalananda
  • 355.7k
  • 42
  • 735
  • 1.1k

Tar The shell not redirecting output of tar to file

added 201 characters in body
Source Link
Ambre
  • 111
  • 2
  • 7

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.

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.

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.

Source Link
Ambre
  • 111
  • 2
  • 7

Tar not redirecting output to file

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.