0

I'm trying to learn how to use bash scripts; I have googled answers but can not find any answer to my problem.

The script looks like this:

#!/bin/bash
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
DESDIR=home/pi/backups
SRC=home/pi/backuplist.txt
find -iname '*.orig' -o -iname '*.py' > home/pi/backuplist.txt
tar -zcvf $DESDIR/$FILENAME $SRC

when I run this it doesn't create the backuplist.txt so there is no result to use with tar.

i tried using find -iname directly with tar in a pipeline but just got the response -iname command does not exist.

When I use find to output first in the terminal and then use tar it works.

What am I doing wrong?

enter image description here

closed as off-topic by Michael Homer, Anthon, thrig, G-Man, countermode Apr 18 '17 at 7:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Anthon, countermode
If this question can be reworded to fit the rules in the help center, please edit the question.

  • ... and it's assumed that it's run from the root directory (/)? Otherwise there may be an issue creating the output file(s). Do you get errors? – Kusalananda Apr 16 '17 at 18:33
  • oops, that typo isn't in the actual script tho – Mårten Ahlberg Apr 16 '17 at 18:34
  • yes a whole bunch. home/pi/backuplist.txt: The file does not exist. 'ckups/backup-apr-16-17.tar.gz: Function "open" failed' tar (Child):error is not recoverable: exiting now – Mårten Ahlberg Apr 16 '17 at 18:35
  • 3
    Show the exact command that you gave on the terminal & which purportedly generates results. – user218374 Apr 16 '17 at 18:39
  • 1
    If this script doesn't match the script you're actually using please fix the typos. Otherwise we may end up trying to address errors that don't actually exist. – roaima Apr 16 '17 at 18:41
3

There are two main issues with this script:

  1. The paths are relative. This means that when you run it, it is assumed that home/pi exists in the current directory. I have a feeling that all instances of home/pi in the script should be changed to /home/pi.

  2. The tar command is probably meant to back up the files in the backuplist.txt file. The way it's written now, you'll get a tar archive containing the backuplist.txt file itself and nothing else. If you meant to read the contents of that file, use tar -zcvf "$DESDIR/$FILENAME" -T "$SRC".

  3. There are also unquoted variables in the script.

Here's what I think you want:

#!/bin/bash

TIME="$(date +%b-%d-%y)"
FILENAME="backup-$TIME.tar.gz"
DESDIR="/home/pi/backups"
SRC="/home/pi/backuplist.txt"

find /home/pi -iname '*.orig' -o -iname '*.py' >"$SRC"
tar -zcvf "$DESDIR/$FILENAME" -T "$SRC"

The /home/pi on the find command line is a wild guess. Without it, I assume GNU find would use the current directory.

  • that made a difference, the error messages that i had Before is gone, now it says find: search path must come Before: home/pi/backuplist.txt – Mårten Ahlberg Apr 16 '17 at 18:53
  • @MårtenAhlberg Are you using find /home/pi ... as I wrote? ... and absolute paths everywhere? – Kusalananda Apr 16 '17 at 18:54
  • i wrote it in again and it must have been something i missed the first time, it works like a charm now. Thank you so much, without your help i probably would be sitting here in a year from now still trying to figure it out :) – Mårten Ahlberg Apr 16 '17 at 19:11
1

I think this slightly different script will address your original desire to have the list of files piped directly into tar.

I've quoted variables where they are used, and replaced backticks `...` with the modern $(...) construct.

Finally, I've assumed you want this to run from /, so that the paths you've used can exist.

#!/bin/bash
WHEN=$(date +%b-%d-%y)
FILENAME="backup-$WHEN.tar.gz"
DESDIR=/home/pi/backups
SRC=/home/pi/backuplist.txt

mkdir -p "$DESDIR"

cd /
find -iname '*.orig' -o -iname '*.py' |
    tee "$SRC" |
    tar zcvf "$DESDIR/$FILENAME" --no-recursion --verbatim-files-from --files-from -

You can completely omit the two lines referencing $SRC if you don't need to create the list of files during the backup process.

Not the answer you're looking for? Browse other questions tagged or ask your own question.