You are not logged in.

#1 2020-05-18 23:15:55

danlove99
Member
Registered: 2020-05-18
Posts: 7

tar script not working when run as a package

I have written a script to execute the tar command on a directory.

#!/bin/bash

if [ ! -f /home/$(whoami)/backups ]; then
	mkdir /home/$(whoami)/backups
fi

backup_dir=$(date +'%m-%d-%Y')

tar -czf "/home/$(whoami)/backups/${backup_dir}.tar.gz" -C / $1

This works absoultley fine on it's own but after being built with PKGBUILD it fails. it manages to create the 'backups' directory but doesn't create the tar file.

pkgname=SFBU
pkgver=0.1
pkgrel=1
pkgdesc="Simple File Backup"
arch=("any")
url="https://github.com/danlove99/sfbu"
license=('GPL')
source=("git://github.com/danlove99/${pkgname}/")
sha1sums=('SKIP')

package() {
    cd "$pkgname"
    mkdir -p $pkgdir/usr/bin
    install -D -m755 ./SFBU.sh $pkgdir/usr/bin/$pkgname
}

Offline

#2 2020-05-19 00:10:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: tar script not working when run as a package

Why are you creating a package for what should be a 3 line script?

FYI, it should be 3 lines as the following two code blocks are equivalent:

if [ ! -f /home/$(whoami)/backups ]; then
	mkdir /home/$(whoami)/backups
fi
mkdir -p /home/$(whoami)/backups

EDIT: ha, see below.  Definitely use mkdir -p.  Yours doesn't even do what I'm pretty sure you want it to do.  If that directory already exists, that if block will still try to create it (because it's not a file).  So the if is effectively a no-op.

Last edited by Trilby (2020-05-19 12:16:26)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-05-19 01:53:29

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 652

Re: tar script not working when run as a package

Trilby wrote:

FYI, it should be 3 lines as the following two code blocks are equivalent

They are not actually equivalent. Your's will at least output an error message if ~/backups is a file!

Offline

#4 2020-05-19 11:34:52

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,222
Website

Re: tar script not working when run as a package

What is the output of `bash -x /usr/bin/SFBU`  (append whatever additional arguments you're using)

EDIT: This one liner is functionally and output equivalent:

[[ ! -d /home/$(whoami)/backups ]] && mkdir /home/$(whoami)/backups

Last edited by fukawi2 (2020-05-19 11:36:14)

Offline

#5 2020-05-19 12:41:50

seth
Member
Registered: 2012-09-03
Posts: 51,029

Re: tar script not working when run as a package

Still probably should be

mkdir -p /home/$(whoami)/backups || exit 1

cause if there's a blocking file and the dir cannot be created, the remaining script will land on its nose.

Offline

#6 2020-05-19 13:55:33

danlove99
Member
Registered: 2020-05-18
Posts: 7

Re: tar script not working when run as a package

fukawi2 wrote:

What is the output of `bash -x /usr/bin/SFBU`  (append whatever additional arguments you're using)

EDIT: This one liner is functionally and output equivalent:

[[ ! -d /home/$(whoami)/backups ]] && mkdir /home/$(whoami)/backups

The output is..

++ whoami
+ '[' '!' -f /home/dan/backups ']'
++ whoami
+ mkdir /home/dan/backups
mkdir: cannot create directory ‘/home/dan/backups’: File exists
+ date +FORMAT
FORMAT
++ date +%m-%d-%Y
+ backup_dir=05-19-2020
++ whoami
+ echo 'Backup dir for today: /home/dan/backups/05-19-2020'
Backup dir for today: /home/dan/backups/05-19-2020
++ whoami
+ tar -czf /home/dan/backups/.tar.gz /home/dan/Pictures
tar: Removing leading `/' from member names

Offline

#7 2020-05-19 13:59:34

danlove99
Member
Registered: 2020-05-18
Posts: 7

Re: tar script not working when run as a package

Okay i've got it to work now the problem was my ${backup_dir} variable. Have i used it correctly in my script?

Offline

#8 2020-05-19 17:04:11

loqs
Member
Registered: 2014-03-06
Posts: 17,321

Re: tar script not working when run as a package

The output trace was produced by the script in post #1?
Some calls do not seem to be present in the script:

+ date +FORMAT
....
+ echo 'Backup dir for today: /home/dan/backups/05-19-2020'

Offline

#9 2020-05-20 00:00:09

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,222
Website

Re: tar script not working when run as a package

Indeed the trace output is from a different script than what was originally posted. Start by getting those in sync with each other.

Slightly OT to the actual problem: you need to change your -f to -d when checking if the backup directory already exists. -f checks for a file, which will never be true in the context of this script since you're expecting/creating a directory. The most complete way to do this would be something like:

if [ ! -d /home/$(whoami)/backups ]; then
  # backup path either doesn't exist, or is not a directory
  if [ ! -e /home/$(whoami)/backups ]; then
    # nothing exists; create the directory
    mkdir /home/$(whoami)/backups
  else
    # something is there, but not a directory
    echo "Unable to create backup path" >&2
    exit 1
  fi
fi

The shorter way is to just use `mkdir -p` as suggested earlier, and abort if it fails:

mkdir -p /home/$(whoami)/backups || exit 1

Offline

Board footer

Powered by FluxBB