Michael Boyd
Michael Boyd

Reputation: 11

"No such file or directory" in bash script

FINAL EDIT: The $DATE variable was what screwed me up. For some reason when I reformatted it, it works fine. Does anyone know why that was an issue?

Here's the final backup script:

#!/bin/bash
#Vars
OUTPATH=/root/Storage/Backups
DATE=$(date +%d-%b)

#Deletes backups that are more than 2 days old
find "$OUTPATH"/* -mtime +2 -type f -delete

#Actual backup operation
dd if=/dev/mmcblk0 | gzip -1 - | dd  of="$OUTPATH"/bpi-"$DATE".img.gz bs=512 count=60831745

OLD SCRIPT:

#!/bin/bash
#Vars
OUTPATH=~/Storage/Backups
DATE=$(date +%d-%b_%H:%M)

#Deletes backups that are more than 2 days old
find "$OUTPATH"/* -mtime +2 -type f -delete

#Actual backup operation
dd if=/dev/mmcblk0 | gzip -1 - | dd  of="$OUTPATH"/bpi_"$DATE".img.gz bs=512 count=60831745 

This is a script to backup my banana pi image to an external hard drive. I am new to bash scripting, so I know this will be an easy fix most likely but here is my issue:

I am running the script from ~/scripts

and the output file is ~/Storage/Backups (the mount point for the external HDD, specified in my /etc/fstab.

The commands work fine when the OUTPATH=., i.e. it just backs up to the current directory that the script is running from. I know I could just move the script to the backup folder and run it from there, but I am trying to add this to my crontab, so if I could keep all scripts in one directory just for organizational purposes that would be good.

Just wondering how to correctly make the script write my image to that $OUTPATH variable.

EDIT: I tried changing the $OUTPATH variable to a test directory that is located on /dev/root/ (on the same device that the script itself is also located) and it worked, so I'm thinking it's just an issue trying to write the image to a device that is different from the one that the script itself is located in.

My /etc/fstab line relating to the external HDD I would like to use is as follows:

/dev/sdb1 /root/Storage exfat defaults 0 0

The /root/Storage/Backups folder is where I am trying to write the image to

Upvotes: 0

Views: 2393

Answers (2)

Мона_Сах
Мона_Сах

Reputation: 322

In

OUTPATH=~/Storage/Backups

tilde expansion is not performed when putting "$OUTPATH" in find

find "$OUTPATH"/* ....

You may replace the ~ with the fullpath in OUTPATH or replace the OUTPATH with the actual path in find.

Upvotes: 0

tale852150
tale852150

Reputation: 1638

Populate OUTPATH with the full pathname of your backups directory.

Upvotes: 0

Related Questions