Shell script help (Not sure if this should be in Linux forum)

Associate
Joined
22 May 2011
Posts
1,445
Location
Edinburgh
Hey all, basically I'm trying to write a script to backup some files in seperate folders dependant on file type.

So if I have word files, spreadsheets and jpegs i end up with thusly named folders inside them.

each folder has 3 pieces of test data in it to ensure the cp actually works. It does.
What is killing me is that if I run the backup for say the JPEG folder I get:

/home/USER/files/JPEGs(where the three pieces of data are) pic1, pic2, pic3

My backup folder looks like:

/home/USER/backup/JPEGs pic1, pic2, pic3

If I delete say pic2 then re-run the backup I get this:

/home/USER/backup/JPEGs JPEGS(a dir with all 3 pics inside) pic1 pic3.

I'm using copy with -r which as far as I am aware would just replace the missing picture yet for some crazy reason it copys the entire dir into the dir where the pictures are!


I realise this makes little sense.... My brain is fried and this is annoying me, please help.
 
I'm a bit confused and not sure I quite understand what you're doing in your script - mind posting it so I can take a look? It sounds like the second time the script is being run, it's being told to place the directory within the existing directory rather than copy the contents/copy the directory to the files directory.
 
This is everything so far:

Code:
echo "----- Backup/Restore Utility ------"
echo "---- Created by Sean Johnstone ----"
echo "-----------------------------------"
echo "|                                 |"
echo "|     Please select an option:    |"
echo "|                                 |"
echo "| 1) Backup all 3 folders         |"
echo "| 2) Backup WP folder             |"
echo "| 3) Backup XLS folder            |"
echo "| 4) Backup JPEG folder           |"
echo "|                                 |"
echo "| 5) Restore all 3 folders        |"
echo "| 6) Restore WP folder            |"
echo "| 7) Restore XLS folder           |"
echo "| 8) Restore JPEG folder          |"
echo "-----------------------------------"
echo ""

read input

case $input in
1) echo "Backing Up all folders..."  
   cp -r /home/sean/work/WPS/ ~/BACKUP2/WPS/	
   cp -r /home/sean/work/XLS/ ~/BACKUP2/XLS/
   cp -r /home/sean/work/JPEGS/ ~/BACKUP2/JPEGS/
   echo "Done!";;

2) echo "Backing up Word Processing folder..."  
   cp -r /home/sean/work/WPS/ ~/BACKUP2/WPS/
   echo "Done!";;

3) echo "Backing up Spreadsheet folder..." 
   cp -r /home/sean/work/XLS/ ~/BACKUP2/XLS/
   echo "Done!";;

4) echo "Backing up JPEG folder..." 
   cp -r /home/sean/work/JPEGS/ ~/BACKUP2/JPEGS/
   echo "Done!";;
esac
 
If the folder {WPS|XLS|JPEGS} doesn't exist in BACKUP2 when you run that copy command, it will copy the folder to BACKUP2 and give it that name (if you were to change WPS to BLAH, it would exist as ~/BACKUP2/BLAH rather than ~/BACKUP2/WPS). However, if the directory already exists, it will copy the directory in to the one with that name, which is how you've ended up with ~/BACKUP2/JPEGS/JPEGS for example.

To solve, simply change the destination to just ~/BACKUP2 and when copying, the new contents will be merged with the existing contents of the relevant directory.
 
Yay it works! Although from the look of my directory it is just plain overwriting :/

I thought the recursive rule prevents this?

EDIT: Oh wait I would want it to do this wouldn't I if I was backing up XD

Although I would only want say a missing file to be restored? Is there anyway to do this?
 
Sorted that. doing cp -rnu fixed it :)

Although I've discovered that if I restore before I backup it just hangs. What would be the code to check if the dir exists? and if not make one?

Sorry if I seem a bit annoying I'm new to this :P
 
Back
Top Bottom