Friday, April 9, 2010

Safely delete Linux files with libtrash

From: 

Takeaway: Deleting files in Linux usually pushes you to the point of no return, with no hope of retrieving any files you may have accidentally deleted. But there is a way to get deleted files back, thanks to the freeware utility libtrash.


Every user, from the most casual user to the most experienced system administrator, has deleted files accidentally. If a Windows user makes such a mistake, most often the files can be recovered from the Recycle Bin. Of course, no self-respecting Linux user would be caught dead using Windows. But just because you’re using Linux, you don’t have to worry about permanently losing data—if you use the freeware utilitylibtrash.

What is libtrash?


Libtrash is more than a simple utility. It’s a shared library that overrides the default actions Linux uses to delete files. Once libtrash is installed, deleted files will be moved into a subdirectory of the user's home directory named Trash. Libtrash allows users to use the normal Linux commands for deleting files, and libtrash will work with any files on the system.

Obtaining libtrash
You can obtain the latest version of libtrash directly from the author’s Web site. The file is only 70 KB in size, so it will download in a flash.

Next, unpack the libtrash archive by going to a command line and typing tar -zxvf libtrash-latest.tgz. When the libtrash archive is unpacked, you’ll find the libtrash-1.8directory in the current directory.

Configuring libtrash
Oddly enough, before you install libtrash, you must configure it. The libtrash-1.8/libtrash.conf file comes preconfigured with default settings. When libtrash is compiled, the /etc/libtrash.conf file is created, but this file only reflects the default configuration of libtrash.

That doesn’t mean you can’t reconfigure libtrash. It just takes a little extra work. There are two configurations available when libtrash is activated:

  • Systemwide configuration: This is used for all users. This configuration is read from the /etc/libtrash.conf file.
  • Personal configurations: Each user may use a separate configuration. Separate configurations are enabled through settings read from the .libtrash file in the home directory of each user.

Systemwide configuration
Systemwide settings are made through the /etc/libtrash.conf file. The required settings must be made prior to compiling libtrash. Changing the /etc/libtrash.conf file after compiling libtrash will not change the default behavior of the program. To enforce the idea that this file should never be edited, no users are given write permission to this file. The /etc/libtrash.conf file is a read-only file for all users, including root. Therefore, you’ll make changes to the libtrash-1.8/libtrash.conf file prior to installation.

There are four entries in the libtrash.conf that are used as environmental variables and cannot be changed by a user's personal configuration settings when libtrash is run. These four settings are:

  • DEBUG
  • PERSONAL_CONF_FILE
  • WARNING_STRING
  • UNCOVER_DIRS

The next four settings allow libtrash to intercept the standard routines Linux uses when files are unlinked or renamed. Your safest bet is to use the defaults for all of these settings:
  • INTERCEPT_UNLINK= YES
  • INTERCEPT_RENAME = YES
  • INTERCEPT_FOPEN
  • INTERCEPT_OPEN

More options, more control
The rest of options give you more control over libtrash. These options are:
  • TRASH_CAN = Trash
    This entry sets the name for the subdirectory where deleted files will be stored. The default name for this directory is Trash. Any name may be used. To change the directory name, enter the new name without any slashes.
  • IN_CASE_OF- FAILURE =
    This setting determines how libtrash reacts when there is a failure. Setting this value to PROTECT prevents files from being deleted by the standard Linux function. Setting the value to ALLOW_DESTRUCTION overrides libtrash.
  • SHOULD-WARN = YES
    This setting enables a warning message for libtrash. It's normally a good idea to remind users that their files will be permanently deleted when libtrash is disabled. This is especially true if users on the system are new to Linux or for users who assume libtrash is always running.
  • PROTECT_TRASH = YES
    This setting protects files in the trash from being deleted. Files placed in the user's trash represent a special case. Libtrash functions by sending deleted files to a trashcan in the user's home directory. When the user wants to delete files in the trashcan, the default libtrash doesn't work.
  • IGNORE_ENTENSIONS =
    This setting allows you to specify files with specific extensions that may be deleted without being copied to the trashcan. To specify which types may be ignored by libtrash, create a semicolon delimited separated list of the file extensions to be ignored. Do not include a dot (.) before the extension. For example, to allow .o, .c, and log files to be deleted without being moved to the trashcan, use this entry: IGNORE_ENTENSIONS = o; c; log
  • IGNORE_HIDDEN = YES
    Hidden files and files inside hidden directories may be sent to the trash. Setting this entry to YES allows Linux to delete hidden files without moving them to the trash. Stick with the default value of YES and override it if you must on a per-user basis.
  • IGNORE_EDITOR_BACKUP = NO
    This entry will ensure that backups of important files are always sent to the trash. These are files that end with a tilde (~). The default is yes. Changing this setting might be a good idea.
  • IGNORE-EDITOR-TEMPORARY = YES
    This entry concerns temporary files created by editors. Setting this entry to YES allows these temporary files to be removed without moving them to the trash. Set the entry to NO to allow libtrash to move these files to the user's trash. Stick with the default and change this setting on a per-user basis.
  • LIBTRASH_CONFIG_FILE_UNREMOVEABLE = YES
    This entry provides protection for the user's personal libtrash configuration file. Removing your own configuration file will cause unexpected actions fromlibtrash. Leave this setting alone.
  • TRASH_SYSTEM_ROOT
    The TRASH_SYSTEM_ROOT variable specifies the location of the directory under the trash that will function as the root (/) of the file system whenever the GLOBAL_PROTECTION variable is set to YES. If global protection is not used, this variable is ignored. This variable always uses a directory name with no slashes. The default setting is TRASH_SYSTEM-ROOT = SYSTEM_ROOT.
  • UNREMOVEABLE_DIRS=/bin;/boot;/dev;/etc;/lib;/sbin;/usr
    Any directory on the system can be marked as an unremoveable directory. All files in these directories are protected from deletion by any user runninglibtrash. This may be the most important step in the entire libtrash configuration process. By carefully establishing a list of protected directories, the administrator can prevent users from either accidentally or intentionally removing critical directories and files. The default list is a commented line in the/libtrash.conf file. To activate this list, remove the comment (#) from the beginning of the line. To add additional directories, add a semicolon to the end of the line, and then add the path to the directory you want protected. In the example, the directory /usr/local/users has been added to the default entry to protected programs.
  • UNCOVER_DIRS =
    Any directories listed as unremoveable may be temporarily removed from the above list by adding the directory to the UNCOVER_DIRS list. The default list is empty. Leave it empty. Any files that need to be uncovered should be added to an UNCOVER_DIRS list in the user's personal configuration file. This prevents all users from being able to remove specified directories.
  • USER_TEMPORATY_DIRS = temp
    The USER_TEMPORARY_DIRS entry specifies a directory or list of directories under the user's home directory where temporary files are created. Files in these directories are always permanently deleted. Leave this setting if you don't require this feature.
  • REMOVEABLE_MEDIA_NMOUNT_POINTS
    This setting prevents copies of removeable media from being copied to the trashcan when global protection is used. The REMOVEABLE_MEDIA_NMOUNT_POINTS setting is activated by default. This setting is ignored when global protection is disabled.
  • EXCEPTIONS
    The EXCEPTIONS setting is used to remove protection from files normally considered unremoveable. This allows essential actions by programs, such as creating or deleting a lock file or mounting/unmounting a drive, and may require some files in the UNREMOVEABLE_DIRS setting to be created and deleted. The default entry is EXCEPTIONS = /etc/mtab; /etc/resolv.conf; /etc/adjtime; /etc/upsstatus

Study these entries, and consider your system requirements. Once the configuration settings are entered, the next step is to compile and install libtrash.

Creating a personal configuration file
Personal configurations are created in each user's home directory. These settings are identical to the ones in the libtrash.conf file mentioned above, and, unless stated above, you should override the settings. For example, you could create the file~/.libtrash in your home directory with these entries:

  • GLOBAL_PROTECTION = NO
  • IGNORE EXTENSIONS = bat; exe;com

These entries disable global protection, and allow any files with the extension bat, exe, or com to be permanently deleted without moving them to the trash.

On multiuser systems, the administrator will probably want all users or groups of users to be able to use libtrash. On these systems, the root user should create a standard libtrash configuration for each group of users requiring a predefined configuration.

Installing libtrash
Libtrash requires ldconfig to compile properly. If the ldconfig executable is located in your /sbin/directory, Linux won’t automatically find Idconfig during the compilation process. To correct this, run the cp /sbin/ldconfig /usr/bin/ldconfig command as root to copy ldconfig to the /usr/bin directory.

Libtrash is installed by running these two commands as root:

  • make
  • make install

Activating libtrash
Because libtrash is a shared library and not an executable program or script, there is no libtrash command to run. To activate the libraries associated with libtrash so that it is activated automatically when any user logs into the system, add this to the/etc/profile file:
 
export LD_PRELOAD=/usr/local/lib/libtrash.so.1.8

Next, add these two aliases to the /etc/profile file. These aliases allow easy activation and deactivation.
 
alias trashon=”export TRASH_OFF=NO”
alias trashoff=”export TRASH_OFF=YES”

After these aliases are created, libtrash can be activated or disabled with these commands:
 
trashon
trashoff

Testing the installation
To test your libtrash installation, simply create a file and delete. The file should then be moved into the Trash directory under your home directory. To run the test, use this series of commands:
 
touch trashtest
vi trashtest
rm trashtest
cd Trash
ls

When I first created the trashtest file using the touch command, I did not add any text to the file. Accordingly, when I deleted the trashtest file, libtrash did not move it to the Trash directory. To avoid making this mistake, simply add some text to the trashtestfile to ensure libtrash moves it to the Trash directory.

Libtrash can save your files from your own mistakes
The libtrash library is not limited to command line operations. Libtrash will work with graphical file managers, e-mail applications, and other applications where files are deleted. All Linux users should have insurance against accidentally deleting files from the system, and libtrash does a good job of providing that insurance. If you want some protection against losing files, libtrash should be in your toolkit.

Posted via email from Troy's posterous

No comments:

Post a Comment

Google+