imapsync_cron.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/perl
  2. use DBI;
  3. use LockFile::Simple qw(lock trylock unlock);
  4. use Proc::ProcessTable;
  5. use Data::Dumper qw(Dumper);
  6. use IPC::Run 'run';
  7. use String::Util 'trim';
  8. use File::Temp;
  9. my $t = Proc::ProcessTable->new;
  10. my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
  11. if ($imapsync_running eq 1)
  12. {
  13. print "imapsync is active, exiting...";
  14. exit;
  15. }
  16. $DBNAME = '';
  17. $DBUSER = '';
  18. $DBPASS = '';
  19. $run_dir="/tmp";
  20. $dsn = "DBI:mysql:database=" . $DBNAME . ";host=mysql";
  21. $lock_file = $run_dir . "/imapsync_busy";
  22. $lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
  23. $lockmgr->lock($lock_file) || die "can't lock ${lock_file}";
  24. $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
  25. mysql_auto_reconnect => 1,
  26. mysql_enable_utf8mb4 => 1
  27. });
  28. open my $file, '<', "/etc/sogo/sieve.creds";
  29. my $creds = <$file>;
  30. close $file;
  31. my ($master_user, $master_pass) = split /:/, $creds;
  32. my $sth = $dbh->prepare("SELECT id, user1, user2, host1, authmech1, password1, exclude, port1, enc1, delete2duplicates, maxage, subfolder2, delete1, delete2, automap, skipcrossduplicates, maxbytespersecond FROM imapsync WHERE active = 1 AND is_running = 0 AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_run) > mins_interval * 60 OR last_run IS NULL) ORDER BY last_run");
  33. $sth->execute();
  34. my $row;
  35. while ($row = $sth->fetchrow_arrayref()) {
  36. $id = @$row[0];
  37. $user1 = @$row[1];
  38. $user2 = @$row[2];
  39. $host1 = @$row[3];
  40. $authmech1 = @$row[4];
  41. $password1 = @$row[5];
  42. $exclude = @$row[6];
  43. $port1 = @$row[7];
  44. $enc1 = @$row[8];
  45. $delete2duplicates = @$row[9];
  46. $maxage = @$row[10];
  47. $subfolder2 = @$row[11];
  48. $delete1 = @$row[12];
  49. $delete2 = @$row[13];
  50. $automap = @$row[14];
  51. $skipcrossduplicates = @$row[15];
  52. $maxbytespersecond = @$row[16];
  53. $is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
  54. $is_running->bind_param( 1, ${id} );
  55. $is_running->execute();
  56. if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
  57. my $template = $run_dir . '/imapsync.XXXXXXX';
  58. my $passfile1 = File::Temp->new(TEMPLATE => $template);
  59. my $passfile2 = File::Temp->new(TEMPLATE => $template);
  60. print $passfile1 "$password1\n";
  61. print $passfile2 trim($master_pass) . "\n";
  62. run [ "/usr/local/bin/imapsync",
  63. "--timeout1", "10",
  64. "--tmpdir", "/tmp",
  65. "--subscribeall",
  66. "--nofoldersizes",
  67. "--skipsize",
  68. "--buffersize 8192000",
  69. "--skipheader 'X-*'",
  70. "--split1 3000",
  71. "--split2 3000",
  72. "--fastio1",
  73. "--fastio2",
  74. ($exclude eq "" ? () : ("--exclude", $exclude)),
  75. ($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
  76. ($maxage eq "0" ? () : ('--maxage', $maxage)),
  77. ($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxage)),
  78. ($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
  79. ($delete1 ne "1" ? () : ('--delete')),
  80. ($delete2 ne "1" ? () : ('--delete2')),
  81. ($automap ne "1" ? () : ('--automap')),
  82. ($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
  83. (!defined($enc1) ? () : ($enc1)),
  84. "--host1", $host1,
  85. "--user1", $user1,
  86. "--passfile1", $passfile1->filename,
  87. "--port1", $port1,
  88. "--host2", "localhost",
  89. "--user2", $user2 . '*' . trim($master_user),
  90. "--passfile2", $passfile2->filename,
  91. '--no-modulesversion'], ">", \my $stdout;
  92. $update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
  93. $update->bind_param( 1, ${stdout} );
  94. $update->bind_param( 2, ${id} );
  95. $update->execute();
  96. }
  97. $sth->finish();
  98. $dbh->disconnect();
  99. $lockmgr->unlock($lock_file);