Trash Bin

RPi NAS: Extras – Greyhole Trash

10 January 2024

Immediately removing deleted files from the NAS can be dangerous. If you accidentally delete a file then there’s no easy way to recover it. This can be prevented by using a Trash, which is enabled by default in Greyhole. In this post we’ll look at different options of using Greyhole’s Trash.

This post is part of a series about building a Network-Attached Storage (NAS) with redundancy using a Raspberry Pi (RPi). See here for a list of all posts in this series.


By default Greyhole uses a Trash. So when we delete a file from a share it’s not actually deleted but it gets moved to a folder called .gh_trash which is located on the storage drives, for example at /nas_mounts/hdd1/gh/.gh_trash. The space used by the file is not released until we empty the Trash.

Disabling Greyhole Trash

We can disable the Trash if we really don’t want to use it. On the web interface, go to Greyhole Config – Trash and adjust the options shown there:

We can also use Greyhole’s configuration file, /etc/greyhole.conf. It gives us a bit more flexibility because we can enable or disable Trash for each Samba share separately, independently of the other shares. For example, if we don’t want to use a Trash for share Entertainment then we could write:

I occasionally delete files by accident so for me having a Trash is very useful.

Emptying Trash

From now on I’m going to assume that we’re using Greyhole’s Trash. If we frequently delete or modify files then our Trash bin will fill up. To empty it we can go to Trash Manager on the web interface. There we’ll see a list of everything in our Trash bin. The paths are clickable so we can go deeper into the folder structure and choose exactly what we want to remove (or restore).

We can also empty the Trash from the command line with

sudo greyhole --empty-trash

This doesn’t give us the same fine-grained control as in the web interface but it may still be useful.

Trash Shares

To empty the Trash as we did in the previous section we have to be logged into the Raspberry Pi. However, there’s also an option to mount the Trash as a Samba share. So when we delete a file on a regular share it will show up on the Trash share. If we delete it on the Trash share then it will be gone forever.

One way of setting up the Trash share is by editing /etc/samba/smb.conf. The name of the share can be

  • Greyhole Trash,
  • Greyhole Recycle Bin or
  • Greyhole Attic.

We also need to make sure that the drive is managed by Greyhole and that it can be edited by our users (so they can delete files from it). The image below shows which options we need to set for the Trash share to work.

Another way to set up a Trash share is via the web interface. Go to Samba Shares – Add Samba Share. The requirements for the share name and additional options are the same as shown above. Other additional options, such as create mask, will be ignored1.

Note, the Trash share will only show files that were deleted after the Trash share has been set up. For files that were deleted earlier we have to manually empty the Trash as discussed in the previous section.

There’s one more point that’s worth knowing. Let’s assume we have a share SecretShare that can only be mounted by some users (e.g. Samba option valid users or some strict folder permissions). When a file on that share gets deleted it will show up in the Trash share. Every user, including those that don’t have access to SecretShare, will be able to see the folder structure when they browse through the Trash share. If the file permissions allow it then they may even be able to read the deleted file’s contents. So if you want the contents of your shares to stay secret then make sure you immediately remove deleted files from the Trash share or don’t use a Trash share at all2.

Deleting old Files from Trash

The command sudo greyhole --empty-trash deletes all files in Trash, irrespective of their age. I find that, and the other options we’ve discussed so far, a bit cumbersome. Ideally my files are kept in Trash for some time and if they’re not restored when the time is up then they should be deleted automatically, without me having to login.

On Greyhole’s github page there is an issue about the topic with a shell script that deletes files older than a certain age. I prefer the readability of Python scripts over shell scripts so I wrote a similar script which you can find here. After downloading the script we have to make it executable with sudo chmod +x clean_nas_trash.py.

To enable it we simply add an entry to crontab. Open crontab with

sudo crontab -e

To empty the Trash once a week, at 2am on Monday, we add the following line:

0 2 * * 1 /path/to/clean_nas_trash.py

In the crontab file there’s a description of what the first five digits mean. I put my file to /usr/local/bin/clean_nas_trash.py. To move it there we need root privileges (e.g. sudo mv clean_nas_trash.py /usr/local/bin/clean_nas_trash.py).


Footnotes:

  1. Folders in the Trash share are generally owned by root with permissions 777 (i.e. full access for everybody). File permissions mirror those of the deleted files. ↩︎
  2. But note that the root user of the Raspberry Pi will always have full access to all data. ↩︎

This post is tagged as:



Comments

Leave a Reply

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