Should my script use cp or mv to be more robust?
Asked Answered
S

3

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.

Sometime answered 27/6, 2012 at 16:45 Comment(2)
By "overwrites file.dat", do you mean the file is truncated to zero, then filled up again?Sinegold
yes larsman..... (points added to comply with mimimun characters lenght)Sometime
A
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.

Aponeurosis answered 27/6, 2012 at 16:49 Comment(0)
N
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.

Narcissus answered 28/6, 2012 at 6:49 Comment(0)
B
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.

Bremble answered 27/6, 2012 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.