1

Running ubuntu 16.04

Update: I got past the error messages by double quoting the variables:

cp -ru "$line" "$destname"

But it's still not copying anything. After adding echo $? after the line above, I get 0.

Replacing cp with ls -l lists contents of the directories.

The code:

[ $# -eq 0 ] && { echo "Usage: $0 m/d/yyyy"; exit 1; }

DestDir=/media/WD_NAS_LEW/grsync

find Documents -type f -newermt $1 > mybackup.log
awk 'BEGIN{FS=OFS="/"}{NF--; print}' mybackup.log | while read line 
do
destname=$DestDir/$line
cp -ru $line $destname
done

#rm mybackup.log
echo Done

Script output -- and the files aren't copied for lines without the errors below:

$ ./mybackup.sh 8/20/2018
cp: target 'Card/Documents' is not a directory
cp: target 'Lessons' is not a directory
cp: target 'Lessons' is not a directory
cp: target 'Letters' is not a directory
cp: target 'Asia' is not a directory
cp: target 'Procedures' is not a directory
Done

Inputs: contents of mybackup.log (full path with file name):

Documents/Phone/Phone Main Memory Card/Documents/misc.doc
Documents/Lew/PFinc/Bills N Balances.ods
Documents/Lew/Trading/Market Review.odt
Documents/Lew/Trading/Trade Tools.ods
Documents/Lew/Trading/Studies & Lessons/Summary Technical Analysis.odt
Documents/Lew/Trading/Studies & Lessons/Paper calendars and diagonals.odt
Documents/Lew/Trading/Paper Trades.ods
Documents/Lew/Trading/DVL/thinkscript/Mark_Volume_SpikeSTUDY.ts
Documents/Lew/General/Essays & Letters/0 RFR.odt
Documents/Lew/General/Long Fist et al Asia/Long Tai Chi.odt
Documents/Lew/General/Technology/Backup Procedures/find files since X for manual backup.png
Documents/Kids/Carey/Carey August 2018.odt
Documents/Kids/Renee/birth certif and social Stella.pdf
Documents/Kids/Renee/Noah/Noah Confucius tenets.odt

awk output (source dir only, no trailing "/"; destination drive is Western Digital mybook NAS):

/media/WD_NAS_LEW/grsync/Documents/Phone/Phone Main Memory Card/Documents
/media/WD_NAS_LEW/grsync/Documents/Lew/PFinc
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading/Studies & Lessons
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading/Studies & Lessons
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading
/media/WD_NAS_LEW/grsync/Documents/Lew/Trading/DVL/thinkscript
/media/WD_NAS_LEW/grsync/Documents/Lew/General/Essays & Letters
/media/WD_NAS_LEW/grsync/Documents/Lew/General/Long Fist et al Asia
/media/WD_NAS_LEW/grsync/Documents/Lew/General/Technology/Backup Procedures
/media/WD_NAS_LEW/grsync/Documents/Kids/Carey
/media/WD_NAS_LEW/grsync/Documents/Kids/Renee
/media/WD_NAS_LEW/grsync/Documents/Kids/Renee/Noah

It seems to be changing the field delimiter. Do I need to take out spaces in dir names, or there a way to leave them?

  • 1
    I strongly suggest using rsync for backup – qwr Aug 26 '18 at 2:10
  • My understanding is that grsync is a gui wrapper around rsync. It worked for a while but doesn't any more. So I thought I'd try the manual route. – Lew Aug 26 '18 at 2:19
  • 5
    I've never used grsync but rsync has worked flawlessly for me. – WinEunuuchs2Unix Aug 26 '18 at 2:26
  • @WinEunuuchs2Unix the exclude rules and relative path are confusing for me but otherwise it works great (and has a progress bar) – qwr Aug 26 '18 at 2:30
  • 2
    @qwr I have a script that excludes virtual file systems / system temporary directories here: askubuntu.com/questions/1028604/… – WinEunuuchs2Unix Aug 26 '18 at 2:32
1

If you have a filename with a space in it, most commands interpret it as two file names. For example, if you have a file called "my file" and you attempt to copy it to /tmp as follows:

cp my file /tmp

the cp command will look for two files (Called my and file -- if they dont exist it will fail).

To overcome this, put quotes round the file name:

cp "my file" /tmp

you need to do the same in your script

cp -ru "$line" "$destname"

(bash will still expand variable names inside double quotes, but not single quotes)

I haven't filly checked your script -- there may be other places where you need to quote variable names.

btw -- there are lots of comments about using rsync. Once you get used to it its a much nicer way of backing up!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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