Learning never exhausts the mind
Home >  Technology > Linux > Enabling Recycle Bin like functionality in Samba

Last Updated on by

Having recently deleted some files by accident over a Samba share, I thought that it may be a good idea to have something similar to the Windows Recycle Bin so that deleted files are moved and can be easily recovered without risking the loss of data. The recycle bin can be emptied periodically and gives an extra layer of protection for your data.

When configured, all calls to unlink (delete) a file will be intercepted and instead moved to the recycle directory. This gives the same effect as the familiar Recycle Bin on Windows computers. It also allows fast data recovery as the files are not actually deleted.

Most modern distributions come with this module pre-installed, but if yours doesn't you can install it using the following command:

sudo apt-get install samba-vfs

Next we need to edit the Samba configuration. This is usually located in /etc/samba/smb.conf

You can either enable the recycle module globally on all shares, or on specific shares. If you plan on sharing the recycle folder don't forget to exclude it from the list otherwise when you empty the recycle bin, the contents will be recycled.

Open the smb.conf file in your favourite editor, mine is Pico.

sudo pico /etc/samba/smb.conf

If you want to enable global recycle module, add the following lines to the [general] section, for individual shares add the following lines to the [share name] section. You need to change the recycle:repository setting to a directory on your file system. Don't change the %U at the end as this is used to substitute the file name.

# Enable the recycle bin
vfs object = recycle
recycle:repository = /mystorage/recycle/%U
recycle:touch = Yes
recycle:keeptree = Yes
recycle:versions = Yes
recycle:noversions = *.tmp,*.temp,*.o,*.obj,*.TMP,*.TEMP
recycle:exclude = *.tmp,*.temp,*.o,*.obj,*.TMP,*.TEMP
recycle:excludedir = /recycle,/tmp,/temp,/TMP,/TEMP

In this example I have added the lines to the Media share.

[Media]
	guest account = root
	force user = root
	writeable = yes
	delete readonly = yes
	public = yes
	path = /mystorage/media
	# Enable the recycle bin
	vfs object = recycle
	recycle:repository = /mystorage/recycle/%U
	recycle:touch = Yes
	recycle:keeptree = Yes
	recycle:versions = Yes
	recycle:noversions = *.tmp,*.temp,*.o,*.obj,*.TMP,*.TEMP
	recycle:exclude = *.tmp,*.temp,*.o,*.obj,*.TMP,*.TEMP
	recycle:excludedir = /recycle,/tmp,/temp,/TMP,/TEMP

Restart Samba with the command sudo restart smbd and delete a test file to verify it works.

Detailed usage guide for the various options can be found on the man pages.

5 thoughts on “Enabling Recycle Bin like functionality in Samba
  • 4th July 2014 at 12:00 am

    Hello and thanks for this post. A question: if I've a file with last access 2 months ago, and I delete it (samba will move that file to recycle bin) the script will delete that file at first run (because last access it's 2 months ago).

    Is there a way to solve this issue?

    thanks

    Pol

    Reply
  • ladiko
    26th February 2013 at 12:00 am

    What is the meaning of "! -path $recycle_dir" if you run it inside $recycle_dir ?

    Reply
    • 12th June 2013 at 12:00 am

      > What is the meaning of "! -path $recycle_dir" if you run it inside $recycle_dir ?

      Well, it prevents the $recycle_dir itself of being deleted.

      Reply
  • 7th August 2012 at 12:00 am

    Script for automatic trash dir cleanup:

    #!/bin/bash

    # cleanup recycle dir:
    # delete all files with last access time
    # older than a specific number of days and
    # remove all empty subdirs afterwards.
    #
    # make sure you set recycle:touch = yes
    # in your smb.conf.

    # set vars
    recycle_dir='/mystorage/recycle/your_recycle_dir_name'
    lastaccess_maxdays=30

    # execute commands
    find $recycle_dir -atime +$lastaccess_maxdays -type f -delete
    find $recycle_dir -type d ! -path $recycle_dir -empty -delete

    Reply
    • Tim Trott
      29th November 2012 at 12:00 am

      Thanks for this, I'll have to try it out. I usually run rm -rf * as root from within the recycle dir  8O 

      Reply

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.