1

I have a code base on my production server which is synced from a remote directory. One thing I want to improve about the rsync process is to make it make a local copy first before it takes place and over write the original files. Something like

before syncing files from remote :
cp -rf production_code_base production_code_base_old
now syncing remote changes to the production directory

Is there a way to do that?

2 Answers 2

2

besides the --backup-dir option (which i haven't used, so i'm not sure if it's appropriate), you could do a 'hard link backup' with cp -al production_code_base production_code_base_old. I'ts very fast since it doesn't copy the data, and it doesn't use up space for unchanged files.

Note: This is safe in this specific case because rsync never opens an existing file for writing, it writes everything to temporary files and finishes with a mv after that. Other transfer systems might overwrite existing files, destroying the hard-link 'backup'.

2
  • 1
    "Never" unless you give it the --inplace flag. Rsync is very flexible. :)
    – mattdm
    Dec 15, 2010 at 3:16
  • @mattdm: you're right, of course. rsync can do almost anything, including shooting your own foot.
    – Javier
    Dec 15, 2010 at 14:23
2

Well, there's lots of ways to do that. Like, the cp command you just posted. Or you can use the --backup-dir option to rsync.

But what are you trying to accomplish? Might it better be done with proper version control?

2
  • I'm using rsync for code deployment. In case there is something going wrong with the new code. I need to backup the previous code first. The code is actually version controlled by the GIT tool. But we don't want to put the .git/ stuff to production along with the real code.
    – Shawn
    Dec 15, 2010 at 1:58
  • But, isn't the previous code also already in git?
    – mattdm
    Dec 15, 2010 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .