Pour un client, j’ai créé un service sur un serveur distant qui traite les fichiers envoyés via Dropbox. Le problème est que Dropbox sur Linux synchronise tous les dossiers dans son dossier racine, à moins qu’il ne soit exclu. Vous pouvez exclure tous les dossiers sauf celui qui vous intéresse, mais dès que vous ajoutez un dossier à votre Dropbox, il apparaîtra sur votre serveur Linux.
Ce script vous avertira si un nouveau dossier apparaît. Il n’exclut pas automatiquement les nouveaux dossiers, mais cette fonctionnalité pourrait être ajoutée si vous êtes assez courageux.
#!/usr/bin/perl
# When using Dropbox on Linux, the complete dropbox folder is
# sync'ed by default, which can use precious disk space if
# we only need some folders.
# Because we cant choose which folders will be sync'ed on
# Linux, we can only exclude folders we don't want. So this script
# reports when a new folder is added to the Dropbox top folder.
# A nice feature would be to be able to only allow some folders.
# Note: since we can't exclude files, they are not reported.
# Dont add a large file to the root of Dropbox, you can't exclude it from syncing.
# if this script finds folders not in the allowed list, it sends
# an email and a notification, in case the mail is flagged as spam.
# I choose not to exclude new folders directly in this script,
# in case something breaks. This script is run on a server used by
# a customer as a WebService endpoint, so better be safe.
# To exclude a folder from syncing, use the dropbox-cli script available at
# https://www.dropbox.com/download?dl=packages/dropbox.py
# then do
# ./dropbox.py exclude add "Folder to exclude"
#
# Coriolis Stephan Burlot, Apr 11, 2018
use strict;
use Data::Dumper;
use MIME::Lite;
use WebService::Prowl;
## the path to the Dropbox folder
my $dropbox_folder = '/home/stephan/Dropbox/';
## email settings
my $email_address = 'EMAIL_ADDRESS';
## I use Prowl (prowlapp.com) to send notifications to my phone.
## prowl settings
my $prowl_api_key = 'PROWL_API_KEY';
## Allowed folders
# famous last words:
# customer: "the folder is named TEST_Service, we'll change the
# name when we go in production."
my @allowed_folders = qw/TEST_Service/;
#################################
## sends a email with the message passed as parameter
sub send_email($) {
my $content = shift @_;
my $msg = MIME::Lite->new(
From => $email_address,
To => $email_address,
Subject => 'Dropbox Bot',
Data => $content
);
$msg->send;
}
#################################
## sends a notification via Prowl
sub send_notification($$) {
my ($app, $event, $message) = @_;
if ($event eq "") {
$event = ' ';
}
# grab your API key from prowlapp.com
my $ws = WebService::Prowl->new(apikey => $prowl_api_key);
$ws->verify || die $ws->error();
$ws->add(application => "$app",
event => "$event",
description => "$message",
url => "");
}
#################################
## MAIN
#################################
# I dont use smartmatch, ie
# if ($file ~~ @allowed_folders)
# so I create a hash for simple matching.
my %allowed = map { $_ => 1 } @allowed_folders;
chdir $dropbox_folder;
if (opendir(my $dh, $dropbox_folder)) {
my @folders = grep !/^\./, readdir($dh);
closedir $dh;
# array of bad folders
my @bad = map { -f $_ || exists $allowed{$_} ? (): $_ } @folders;
if (scalar(@bad) != 0) {
print "New folders: " . join(", ", @bad) . "\n";
send_notification('Linode_Small', 'Dropbox Bot', "There are new folders in Dropbox: you should exclude them.");
send_email("Hello,\n\nI found these new folders in Dropbox:\n\n" . join("\n", @bad) . "\n\nThey should be excluded.\n");
}
} else {
send_notification('Linode_SMALL', 'Dropbox Bot', "I cant open Dropbox folder. Is it still there?");
send_email("Hello,\n\nI can't opendir $dropbox_folder\n\nIs Dropbox still here?");
die "Can't opendir $dropbox_folder: $!\n";
}
Enjoy.