Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running eclipse on windows and would like to automatically backup my src direcories for all my projects. I installed cygwin and tried to make a simple backup script but I am having trouble properly specifying the destination filename. The backups should be in the current directory.directory of the script.

#!/bin/bash


for f in /cygdrive/c/Documents\ and\ Settings/USERNAME/My\ Documents/Eclipse\ Workspace/*
do
        echo "in folder \"$f\""
        cp -a "$f"/src/ ./????.bak

done

I would like ???? to be the project folder (not the absolute path). Thanks.

share|improve this question
10  
rsync is a good tool to maintain backups. It only copies the files which are changed. instead of the whole bunch of files. linux.die.net/man/1/rsync –  Pitt Oct 24 '12 at 16:00
    
There are also incremental backup tools (based on rsync) like e.g. rdiff-backup. But rather follow @Gilles's and @sparticvs's advice: revision control! –  fheub Oct 25 '12 at 6:36
add comment

2 Answers

up vote 4 down vote accepted

I think the shell construct you're looking for is dirname.

for f in /cygdrive/c/Documents\ and\ Settings/USERNAME/My\ Documents/Eclipse\ Workspace/*
do
    cp -a "$f/src/" "./$(dirname "$f").bak"
done

But really, this is not a good backup solution. Every time you run the copy, the previous state is erased. And it doesn't provide any history at all. Use version control. Use anything, even CVS (the useful functions of which can be learned in under an hour). Whichever version control system you choose:

  1. Create a repository.
  2. Check in or import your existing files.
  3. Whenever you've done a little chunk of work, commit.
share|improve this answer
2  
People only start using version control after a bad experience with a backup script :) –  Bernhard Oct 25 '12 at 5:49
    
I would normally set up a git repo but I wanted to practice my shell scripting. –  vavix Oct 25 '12 at 23:30
    
in the end I settled with cp -a "$f"/src/ "./backup/$(basename "$f").bak" –  vavix Oct 25 '12 at 23:51
add comment

I would recommend using a source code management system like git or hg (mercurial). If they are private projects BitBucket has a nice private project storage for free (given its less than 10 people on the project). Git works well on Windows and there is a plugin for eclipse called EGIT which would allow you to sync to a repo elsewhere. For programming, I would definitely recommend this over writing backup scripts.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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