You are not logged in.
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
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
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
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)
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
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
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
Okay i've got it to work now the problem was my ${backup_dir} variable. Have i used it correctly in my script?
Offline
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
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
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline