Protect harddisk-based backup from accidental removal

Posted on in Categories Backup, FreeBSD, Linux, Security last updated July 28, 2006

If you store a backup on hard disk, the risk of rm -rf does exists.

Use chattr command changes the file attributes on a Linux second extended file system so that you will stay away from accidentally removing your backup files with rm -rf.

chattr command write protect the backup directory so that no one can delete it (including root). This is also known as making file immutable. For example your backup directory is /backup, type the following command to write protect directory /backup (only root can set or remove these flags):

# chattr -R +i /backup

To remove immutable flag, type the following command:

# chattr -R -i /backup

If you are using FreeBSD use chflags command to setup immutable flag:

# chflags -R schg /backup

To remove immutable flag, type the following command:

# chflags -R noschg /backup

These commands will keep you from accidentally removing your backup files with rm -rf 😀

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment