Quantcast
Viewing all articles
Browse latest Browse all 8

How To: Network Trash on Ubuntu File Server (NAS) with SFTP (SSH + Fuse) and AFP (netatalk)

Purpose: create a Network Trash functionality for a Ubuntu Linux file server (NAS). Reason being: by default, files deleted from the command line on file server go away permanently. If I am connected to my file server from my Mac via AFP (through netatalk) or SSH (SFTP through Fuse) and delete a file, that file is gone forever! This is a problem, because often I find I want them back. Enter: libtrash!

Tested on: Ubuntu 6.06.1, 6.10, 10.04.

install libtrash and test it out

The version of libtrash in the repository is not the latest — I prefer to grab it from source which, in this case, is very easy:

  • sudo aptitude install build-essential
  • wget http://pages.stern.nyu.edu/~marriaga/software/libtrash/libtrash-latest.tgz
  • tar xzf libtrash-latest.tgz
  • cd libtrash-3.2/
  • make
  • sudo make install

Unlike a lot of software on linux, installing it isn’t quite enough to get it running. I think this is because it is a lib package and not an actual program — it seems to be meant to be used by other programs in the background and not directly interact with the user (in the way that I want it to).

To test it out briefly, run the following to start the libtrash engine in your terminal prompt:

  • export LD_PRELOAD=/usr/local/lib/libtrash.so

After the above command has run, you can do a little test by creating a file (the touch command doesn’t seem to work, so create a real file) and then delete it. It should show up in your ~/Trash folder.

If it isn’t working at this point, check out the documentation at: /usr/share/doc/libtrash/

To make sure libtrash is running every time you login you need to add the export command to the top of /etc/profile:

  • sudo nano /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

export LD_PRELOAD=/usr/local/lib/libtrash.so

[...]

You should be all set in the terminal, at this point.

There are a few more steps that need to be taken in order to begin utilizing Trash folders for the users on your system. For each method of connecting/utilizing the files on the server (through the command line, through sshFS, or AFP), the LD_PRELOAD option needs to be called for libtrash. Also, I recommend you use one of the Trash cleaning scripts (outlined below).

using libtrash with SFTP (sshFS)

I like to mount my home server remotely using sshFS (which utilizes FUSE). sshFS can be used on the Mac through the MacFUSE project or on Ubuntu through Places/Go to Server Menu on 7.04). To initialize libtrash, we need to create small script that loads the module just before the sftp-server is started and then tell openssh (in my case) to run that script (rather than starting the sftp-server directly).

I created by file at /usr/local/lib/libtrash-sftp-server and entered:

#!/bin/bash
export LD_PRELOAD=/usr/local/lib/libtrash.so
/usr/lib/openssh/sftp-server

Change permissions to make it executable:

  • sudo chmod +x /usr/local/lib/libtrash-sftp-server

Then open the configuration file for ssh (/etc/ssh/sshd_config) and make the following change near the very end of the file (comment out the original and add your own):

#Subsystem sftp /usr/lib/openssh/sftp-server
Subsystem sftp /usr/local/lib/libtrash-sftp-server

Restart your ssh server and then, when you connect via sshFS (in Ubuntu go to Places < Connect to Server and choose a SFTP (SSH) server; on the Mac, you will need MacFuse and SSHFS) you'll be using your ~/Trash folder.

  • $ sudo /etc/init.d/ssh restart

using libtrash with AFP (netatalk)

I was able to use libtrash with netatalk by inserting the following line in the /etc/init.d/netatalk file (10.04 version shown here):

case "$1" in
	start)
        export LD_PRELOAD=/usr/local/lib/libtrash.so
		if [ "x$ATALK_BGROUND" = "xyes" ]; then
			echo "Starting Netatalk services in the background."
			atalk_startup >/dev/null &
		else
			echo -n "Starting Netatalk services (this will take a while): "
			atalk_startup
			echo "."
		fi
	;;

I added line 90. After, simply restart netatalk.

  • sudo /etc/init.d/netatalk restart

how to empty the trash automatically

If you look in ~/src/libtrash-x.x/cleanTrash/ you will find a couple different cleaning scripts. I like strash the best. If you do this, you can extract the script, install it to a usable path, and setup the man file (so you can read about its cleverness).

  • cd ~/src/libtrash-3.2/cleanTrash/
  • tar xvf strash.tar.gz
  • sudo cp strash-0.9/strash /usr/local/sbin/strash
  • sudo mkdir -p /usr/local/share/man/man8
  • sudo cp strash-0.9/strash.8 /usr/share/man/man8/strash.8

You can read the man page for strash now, which will show you all the nifty options. I added a line to my root crontab (by running: sudo crontab -e) that deletes all files over one week old:

# m h  dom mon dow   command
30 01 * * * /usr/sbin/strash --age 7d

Seems to work well. Enjoy.


Viewing all articles
Browse latest Browse all 8

Trending Articles