Bash tar and output to log file

22,741

Solution 1

The key issue is that

tar -czf arh.tar.gz public_html 1

doesn't create any output at all. So you need at least

tar -czvf arh.tar.gz public_html 1

to create any output. But as the output goes to STDERR instead of STDOUT you will need

tar -czvf arh.tar.gz public_html 1 2> 1.log

to catch any output in the log file


It's usually better to ask new questions instead of following-up in the same topic but here we go

for i in */ ; do
    sitefolder="$HOME/domains/${i%%/}/public_html"
    if [ -d "$sitefolder" ]; then
        echo "Backing up ${i%%/}" >> $logs/backups.log
        tar -czvf $tbackups/$i".tar.gz" public_html 2>> $logs/backups.log
        echo "Backup of ${i%%/} successfull" >> $logs/backups.log
    fi
done

Solution 2

tar czvf arh.tar.gz public_html 1 >1.log 2>&1 

I added a v so it's verbose, ie it will output the list of files/dirs it is tarring, and this will go into 1.log.

Are you sure you want to tar both public_html and a file/directory named 1 ? Because that's what is written

I also took out the - in front of the options (tar is compatible with very old versions of that command, that didn't use -, so it interprets the first argument it receives as options by default. It's a bit more compatible that way.

Share:
22,741

Related videos on Youtube

Alexander Kim
Author by

Alexander Kim

Middle Frontend-engineer. Working with JS: Vue/Nativescript/Electron/Express || Koa.

Updated on September 18, 2022

Comments

  • Alexander Kim
    Alexander Kim over 1 year

    How can i write TAR operation in to a log file?

    tar -czvf arh.tar.gz public_html > 1.log
    

    TAR -czvf worked thanks guys!

    Now in bash script:

    for i in */ ; do
        sitefolder="$HOME/domains/${i%%/}/public_html"
        if [ -d "$sitefolder" ]
        then
            ( tar -czvf $tbackups/$i".tar.gz" public_html >> $logs/backups.log ; )
        fi
    done
    

    How can i make output in a log file like:

    domain.com successfully archived domain1.com successfully archived and so on.

  • Alexander Kim
    Alexander Kim over 10 years
    Thanks now it works, can u look at my updated post please.
  • Alexander Kim
    Alexander Kim over 10 years
    do i need on this line tar -czvf $tbackups/$i".tar.gz" public_html 2>> $logs/backups.log - output to a log file?
  • 张小刀
    张小刀 over 10 years
    @Heihachi only if you want to log each file as it gets added to the archive