I have JUST started looking into the world of scripting, specifically with with bash, and am trying to write a simple script to copy the contents of a folder (that contains sub-folders) to a back-up location on an external flash drive (trying to back-up Thunderbird’s data). I’ve looked at a couple of tutorials and what I’m running into are issues with how to navigate to parent directories within the script. The folder I want to copy exists in the directory 1 above where my script file lies. To get to the hard drive I’ve got to back-up through two parent directories… Here’s what I’ve created (I’m running ubuntu 12.04):
#! /bin/bash
#this attepmts to copy the profile folder for Thunderbird to the backup drive
echo "...attempting to copy Thunderbird Profile to back-up drive (My Passport)"
#attempt to backup two directories to where media folder (and therefore My Passport is located)
parent=$(dirname $PWD)
grandparent=$(dirname $parent)
greatgrand=$(dirname $grandparent)
#show what directories the variables are set to
echo "...parent: $parent"
echo "...grandparent: $grandparent"
echo "...greatgrand: $greatgrand"
echo "...copying..."
#FIRST SUBSHELL
#create a subshell and cd to directory to copy && tar directory
#tar: -c = create tarball, -f = tells it what to create " - " = is the unix convention for stdout (this goes with the -f) " . " = means the whole directory. I the end this first subshell is creating a tarball and dumping it in stdout
# | = pipe
#SECOND SUBSHELL
(cd /mcp/.thunderbird/lOdhn9gd.default && tar -cf - .) | (cd $greatgrand/media/My Passport/Gmail_to_Thunderbird_Backup && tar -xpf -)
When I run this I get:
mcp@mcp-Satellite-A135:~/BashScriptPractice$ ./thunderProfileBU.sh
...attempting to copy Thunderbird Profile to back-up drive (My Passport)
...parent: /home/mcp
...grandparent: /home
...greatgrand: /
...copying...
./thunderProfileBU.sh: line 23: cd: //media/My: No such file or directory
./thunderProfileBU.sh: line 23: cd: /mcp/.thunderbird/lOdhn9gd.default: No such file or directory
I should be starting in the directory “/mcp”. I’m guessing I don’t need it in the first sub-script (the last line) above but when I tried to just use “cd /.thunderbird/lOdhn9g…” I was still getting the error. For the second sub-script I’m not sure exactly what is happening. Am I just misunderstanding the folder navigation syntax?
Also, this is a side question but is scripting in this manner something a software developer should know how to do or is this kind of thing more reserved for system admins? I haven’t taken any scripting classes or do I know of any offered through my university however I find it interesting and can see how it could be pretty useful… Thanks!
Instead of using
dirnameto get the parent of each directory, you could use.., where$greatgrandis simply../../...Now relying on parent directories in a script is often a bad idea because you have to garantee that they exist.
There’s two places where your script fails:
You should protect the directory name since it contains spaces, and spaces are an argument separator.
The directory you want to copy doen’t exist. I’m guessing you want
~mcpinstead, or/home/mcp.If what you want is to backup your thunderbird preferences to an external drive, you should use
rsync: