Enabling Recycle Bin for Samba File Server

PowerADM.com / Linux / CentOS / Enabling Recycle Bin for Samba File Server

You can use the built-in network recycle bin to automatically save all files deleted by a user on the Samba file server on Linux. For this, the vfs_recycle (Virtual File System) module is used.

The CIFS VFS modules are installed with the samba package on most Linux distributions. If needed, you can install VFS modules manually. For example, on Ubuntu / Linux Mint / Debian:

$ sudo apt update
$ sudo apt install samba-vfs-modules

install samba-vmfs-modules on linux

Check if the module is installed:

$ apt list | grep samba-vfs

By default, when you delete files from the samba shared folder, they are permanently deleted. A Linux file server administrator can create a network recycle bin. The Samba Recycle Bin is a hidden directory to which all Samba objects that have been deleted by the user are moved.

Please note that there is no such recycle bin on the Windows SMB file server.

We will look at the option of creating a global Samba recycle bin (for all samba shares and users).

Create a recycle directory and grant permissions:

# mkdir –p /mnt/smb/.recycle
# mkdir chmod –r 777 /mnt/smb/.recycle
To hide a directory, place a dot (.) at the beginning of the directory name.

Open the file /etc/samba/smb.conf and add the following settings to the [global] section:

# mcedit /etc/samba/smb.conf

vfs object = recycle
recycle:repository = /mnt/smb/.recycle
recycle:keeptree = yes
recycle:versions = yes
recycle:touch = yes
recylce:exclude_dir = /tmp /TMP /temp /TEMP /public /cache /CACHE
recycle:exclude = *.TMP *.tmp *.temp ~$* *.log *.bak

  • vfs objects = recycle — enable the Samba recycle bin;
    I If you are using any other VFS modules, you must specify them on the single line: vfs object = recycle full_audit shadow_copy2
  • recycle:repository — set recycle bin directory for storing deleted samba objects. The directory has to be located on the same file system as the SMB folder. If you specify the path in the format /mnt/smb/.recycle/%U, then the deleted files will be saved to the directory with the name of the user who removed the file or folder. If the parameter is not set, a .recycle directory will be created at the root of each SMB share;
  • recycle:keeptree — delete objects while keeping the directory tree
    If you use the path to recycle bin in the /mnt/smb/.recycle/%U format, it will be available in the recycle bin as /mnt/smb/.recycle/username/docs/1.txt
  • recycle:touch — change the time of the last access to the file when it is moved to the Recycle Bin;
  • recycle:touch_mtime = yes – update the last modified date of the file when it is moved to the recycle bin;
  • recycle:versions — add version number when deleting files with matching name (Copy #N of);
  • recylce:exclude_dir – exclude directories
  • recycle:exclude – add exclusions for specific file types. Files with these extensions will be permanently deleted;
  • recycle maxsize – you can set the maximum size of files (in bytes) that need to be saved to the recycle bin.

By default, the Recycle Bin is created with permissions set to 0700. These permissions are inherited by all objects in the Recycle Bin. You can change the default permissions:

recycle:directory_mode = 2770

You will also be able to change the default permissions for nested directories.

recycle:subdir_mode = MODE

If you need to enable the network recycle bin only for a specific shared folder on a Samba host, you need to add these recycle bin parameters to the configuration section of that folder.

To apply the change, you need to restart samba:

# systemctl restart smb

To automatically clean up deleted files older than 60 days from the samba recycle bin, use the following bash script:

/usr/bin/find $recyclePath -name "*" -ctime +$maxStoreDays -exec rm {} \;

Allow the script to run:

# chmod +x /root/bin/cleanup_samba_recycle.sh

Create a cron job to clean up the recycle samba daily:

# crontab -e

0 4 * * * /root/bin/cleanup_samba_recycle.sh

4 thoughts on “Enabling Recycle Bin for Samba File Server”
  1. Thanks for the post!

    FYI – I’m running ubuntu 20.04.5 and `chmod -r` didn’t work for me. I had to use `chmod -R`

    1. Also – I see a small typo – this line should have a line-break after the ‘yes’
      recycle:touch = yesrecylce:exclude_dir = /tmp /TMP /temp /TEMP /public /cache /CACHE

  2. I copied the directory /etc/skel to “/mnt/smb/.recycle/user1”
    cp -a /etc/skel /mnt/smb/.recycle/user1
    Then using option -ctime the find show empty
    /usr/bin/find ${recyclePath}/user1 -name “*” -ctime +$maxStoreDays -exec ls {} \;
    But it’s working with option -mtime
    /usr/bin/find ${recyclePath}/user1 -name “*” -ctime +$maxStoreDays -exec ls {} \;
    I suggest you to use this command
    /usr/bin/find $recyclePath -name “*” -mtime +$maxStoreDays -print0 | xargs -0 /bin/rm -f

Leave a Reply

Your email address will not be published. Required fields are marked *