Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 593
espinozahg Avatar asked Jun 27 '12 16:06

espinozahg


People also ask

Is mv same as cp?

With cp you still have your complete original data in its original directory structure. With mv you have some of your data in the original dir, some in the target dir, and re-merging them may be difficult (esp. if the target dir contains other data).

What is the difference between mv and cp in Linux?

The cp command will copy your file(s) while the mv one will move them. So, the difference is that cp will keep the old file(s) while mv won't.


2 Answers

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.

like image 196
Thor Avatar answered Sep 19 '22 14:09

Thor


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.

like image 41
Todd A. Jacobs Avatar answered Sep 21 '22 14:09

Todd A. Jacobs