0

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

  • 1
    The shebang line is wrong: `#/!bin/bash` --> `#!/bin/bash`. Other than that, your script works fine for me. – John1024 Jul 16 '16 at 17:29
  • whoops that was just a mistake when copied the script over. interesting that it works fine for you though, not sure what that means for me – Michael Boyd Jul 16 '16 at 18:20
  • Are there any other differences between the script shown here and the script that you are using? In shell scripts, subtle changes can make a big difference. – John1024 Jul 16 '16 at 18:42
  • 1
    Also, if the script works for `OUTPATH=.` but not `OUTPATH=~/Storage/Backups`, are you sure that the backup directory is _exactly_ `~/Storage/Backups`? Capitalization and spaces and all that are important. – John1024 Jul 16 '16 at 19:33
  • Yeah its exactly the same, I accidentally deleted the first few characters of the shebang when I was starting to format the post so that's the only difference. And yes, I copy and pasted the terminal output from `pwd` when I was in the dir that I wanted, so that should work right? – Michael Boyd Jul 16 '16 at 21:02
  • Try running `bash -x scriptname` and see what comes out. If there are any error messages, copy-and-paste the error message and its surrounding lines into the question. – John1024 Jul 16 '16 at 21:12
  • @John1024 Capitalization only matters on a case-sensitive file system. – chepner Jul 17 '16 at 02:20
  • What is the *exact* error message? – chepner Jul 17 '16 at 02:26
  • The exact message is ` find: `/root/Storage/Backups/*': No such file or directory dd: failed to open ‘/root/Storage/Backups/bpi_19-Jul_17:51.img.gz’: No such file or directory ` – Michael Boyd Jul 19 '16 at 21:52

2 Answers2

0

Populate OUTPATH with the full pathname of your backups directory.

tale852150
  • 1,618
  • 3
  • 17
  • 23
  • I thought I did, this might be an extremely lame question but isn't the full pathname printed to the screen when i issue the `pwd` command? cause I literally copied and pasted it. unless you mean the `/dev/sdaX` path? – Michael Boyd Jul 16 '16 at 18:22
  • Yes, @MichaelBoyd, your script, as shown above, does provide full (absolute) pathnames. – John1024 Jul 16 '16 at 19:30
0

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.

Мона_Сах
  • 322
  • 3
  • 12