Welcome to the Linux Foundation Forum!

An issue with tar

Trying to make a simple backup script that will tar at the end

Script:

!/bin/bash

What to back up

backup_files="home etc root var usr/local/bin usr/local/sbin srv opt "

Where

dest="/home/backup"

Create

day=$(date +%A)
hostname=$(hostname -s)
archive=$hostname-$day.tgz

Print start

echo "Backing up system stand by"
date
echo

Tar command

tar czf $dest/$archive_file $backup_files

Print end

echo
echo "Back up finished Thank you for your patience"
date

End of script

Results when ran
Backing up system stand by
Fri Sep 10 03:32:15 UTC 2021

tar: home: Cannot stat: No such file or directory
tar: etc: Cannot stat: No such file or directory
tar: root: Cannot stat: No such file or directory
tar: var: Cannot stat: No such file or directory
tar: usr/local/bin: Cannot stat: No such file or directory
tar: usr/local/sbin: Cannot stat: No such file or directory
tar: srv: Cannot stat: No such file or directory
tar: opt: Cannot stat: No such file or directory
tar (child): /home/backup/: Cannot open: Is a directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

Back up finished Thank you for your patience
Fri Sep 10 03:32:15 UTC 2021

What is the issue?

Answers

  • What stands out for me is the fact that the directories you are trying to backup are defined in the backup_files variable with relative paths. In other words, the default "home" folder can only be acessed like that if you are in root directory (/). Instead, the absolute path "/home" can be acessed anywhere in the machine - assuming you have the necessary permissions.

    First things first:

    • Consider using absolute paths
    • Watch out for permissions - if you want access to some directories, you need to be the root user
    • Rethink your strategy e.g. do you really need to backup everything in /var? Read about directories like /var/run and/var/lock

Categories

Upcoming Training