Make a copy of a file when someone tries to modify the file

I wish to take a backup of the file when someone tries to modify a file say /tmp/test.txt using vi, vim, cat, copy, mv commands.

I'm aware of inotify which will notify me of any changes to the file however, I wish to make a backup before and not after the changes are made to the file.

I wish to avoid loop check and backup on the file as i believe it will be too time consuming.

Thus, whenever someone makes changes to a file there should exist its backup taken right before the file is modified.

Can you please let me know if and how this is possible to achieve ?

A solution that works across AiX / Linux systems would be great.

Dear mohtashims,

You know how it works here at unix.com.

You show your own work and what you have tried, any research you did, input / output and error messages.

You must show YOUR OWN WORK and not post here expecting us to do your work for you.

Otherwise, your company should put us all on your payroll and pay us, instead of you :slight_smile:

PS: You have nearly 1,000 posts here but you post like someone who is a novice and has never posted here before. Please take note of my message to you as the lead admin and most senior person here.

Show your work.

2 Likes

It is possible using log structured filesystems such as NILFS.

Looks like a rare and i dare to say killer feature in filesystem world, which should fill your requirement transparent to applications.

Of course, there are some drawbacks, caveats which you might or might not experience depending on the type of your workload.
Read the docs if you plan to use it.

I have not used it and it is for linux, you can forget AIX.

Hope that helps.
Regards
Peasant.