Protect harddisk-based backup from accidental removal

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, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):