How to Make Your VPS Safer Against Accidental Deletions

Introduction

Have you ever deleted a file or a folder by mistake in a VPS? That feeling sucks. Sometimes you are working fast and you delete a really important file/folder. This happened to me previously. Today I want to share an amazing tool with you guys: trash-cli . It adds a trash in the CLI to prevent these human mistakes.

Installing trash-cli

trash-cli is a lightweight, command-line tool available in Debian repositories, making it ideal for VPS environments with limited resources.

Installation Steps

  1. Update your package list:
    sudo apt update
    
  2. Install trash-cli:
    sudo apt install trash-cli
    
  3. Verify the installation:
    trash --version
    
    You should see version information if installed correctly.

This process is quick and adds minimal overhead to your VPS.

Using trash-cli for Safer Deletions

Once installed, trash-cli provides commands to manage files safely. It moves items to ~/.local/share/Trash/ instead of deleting them.

Basic Commands

Example workflow:

trash important_file.txt  # Move to trash
trash-list               # Check what's there
trash-restore            # Recover if needed

This replaces risky rm usage in daily operations.

Aliasing rm to Use trash-cli

To make rm safer by default, alias it to trash in your shell configuration. This ensures most deletions go to the trash bin.

Setting Up the Alias

  1. Edit your ~/.bashrc file:
    nano ~/.bashrc
    
  2. Add this line at the end:
    alias rm='trash'
    
  3. Save and exit (Ctrl+X, Y, Enter).
  4. Reload the configuration:
    source ~/.bashrc
    

Now, rm file.txt will use trash instead of permanent deletion.1

Automating Trash Emptying with Cron

To prevent the trash from accumulating indefinitely, automate emptying with a cron job.

Setting Up Weekly Emptying

  1. Edit your crontab:
    crontab -e
    
  2. Add this line for weekly deletion (e.g., every Sunday at 2 AM):
    0 2 * * 0 /usr/bin/trash-empty
    
    • 0 2 * * 0: Sunday at 2:00 AM.
    • /usr/bin/trash-empty: Clears all trash.
  3. Save and exit.
  4. Verify:
    crontab -l
    

Adjust the schedule as needed (e.g., change 0 to 1-6 for weekdays). For partial emptying, use trash-empty 30 to delete items older than 30 days.

Conclusion

By installing trash-cli, aliasing rm to trash, and setting up automated emptying, you can make your VPS much safer against accidental deletions. This approach adds a recoverable layer without sacrificing performance. Remember to combine it with regular backups and cautious command usage. If you're new to VPS management, start small and test thoroughly. For more advanced setups, explore integrating with monitoring tools. Stay safe out there!