How To Enable Recycle Bin Like Functionality in Samba

How to configure Samba to add a Recycle Bin style function where files are not deleted strait away, but instead moved to a separate folder.

By Tim Trott | Linux Tips and Tutorials | June 15, 2012

Having recently deleted some files by accident over a Samba share and having no way to recover deleted files in Linix, I thought that it may be a good idea to have something similar to the Windows Recycle Bin so that deleted files are moved rather than deleted, 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 deleted.

Installing and Configuring Samba Recycle Bin

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

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 6 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. DR

    On Wednesday 14th of April 2021, dr0 said

    Thanks for the guide, Tim!

    I had a problem excluding files and folders that were already inside of my SMB Recycle Bin folder from being recycled, so here's my slightly modified config from my Linux Mint 18.3 Cinnamon 32-bit box that fixed the issue for me:

    [Shared]
    path = /home/dr0/Shared
    read only = no
    writeable = yes
    browseable = yes
    security = user
    valid users = dr0
    create mask = 0640
    directory mask = 0750
    # Enable the recycle bin
    vfs object = recycle
    recycle:repository = /home/dr0/Shared/.recycle
    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:exclude_dir = .recycle

  2. PO

    On Friday 4th of July 2014, Pol said

    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

  3. LA

    On Tuesday 26th of February 2013, ladiko said

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

    1. SO

      On Wednesday 12th of June 2013, SomeOne replied

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

  4. SO

    On Tuesday 7th of August 2012, SomeOne said

    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

      On Thursday 29th of November 2012, Tim Trott  Post Author replied

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