6

I have a bash script (Scientific Linux). The script has to operate on a file. Let's say "file.dat" (around 1 GB of size) After some time the scripts is restarted and executes the following:

if [ -f file.dat ];  then
    cp file.dat file.previous.dat
fi

to have a backup of the file. Then a process starts and overwrites "file.dat"

In order to be on the safest side (electricity shut down or anything unexpected). What would be the best option: cp or mv ? Thanks.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
espinozahg
  • 61
  • 1
  • 4

3 Answers3

5

I would use a combination:

mv file.dat file.dat.previous
cp file.dat.previous file.dat

That way file.dat.previous will always be complete as mv is atomic.

Thor
  • 45,082
  • 11
  • 119
  • 130
4

The Right Answer to the Wrong Question

If you want a quick, atomic move, then mv is the thing to do since man 2 rename says:

If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another process attempting to access newpath will find it missing.

Perhaps more importantly, mv is largely a directory entry operation, so it's very quick compared to a file copy in any normal circumstance.

The Right Answer to the Right Question

If you're worried about power outages or unexpected system shutdowns, then:

  1. Attach an uninterruptible power supply. Really. Solve for the threat model.
  2. Make sure you're using a battery-backed RAID controller.
  3. Make critical writes synchronous.
  4. Use a journaling filesystem that journals data, and not just metadata.

The mv command should be faster, but robustness in the face of catastrophic failures is a hardware or filesystem issue.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
1

Probably not too helpful here, but rsync is the tool for this kind of job. If the transfer gets interrupted it can restart from where it needs to go.

lonestar21
  • 1,113
  • 2
  • 13
  • 23