Technology blog::Life hacks::Linux::Hardware::Gaming

Enabling Recycle Bin like functionality in Samba

By Tim Trott, 15th June 2012 in Linux

Having recently delete some files by accident over a Samba share, I though 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 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.

Comments
  1. Pol
    Pol

    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

  2. ladiko
    ladiko

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

    1. SomeOne
      SomeOne

      > 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.

  3. SomeOne
    SomeOne

    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

    1. Tim Trott
      Tim Trott

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

Leave a Reply

Your email address will not be published.