[Dovecot] Revert to previous imapsync cron script

master
andryyy 2019-05-04 23:08:35 +02:00
parent 85e686d194
commit 189ea89a71
No known key found for this signature in database
GPG Key ID: 8EC34FF2794E25EF
1 changed files with 42 additions and 36 deletions

View File

@ -18,6 +18,14 @@ if ($imapsync_running eq 1)
exit; exit;
} }
sub qqw($) {
my @values = split('(?=--)', $_[0]);
foreach my $val (@values) {
$val=trim($val);
}
return @values
}
$run_dir="/tmp"; $run_dir="/tmp";
$dsn = 'DBI:mysql:database=__DBNAME__;mysql_socket=/var/run/mysqld/mysqld.sock'; $dsn = 'DBI:mysql:database=__DBNAME__;mysql_socket=/var/run/mysqld/mysqld.sock';
$lock_file = $run_dir . "/imapsync_busy"; $lock_file = $run_dir . "/imapsync_busy";
@ -93,6 +101,10 @@ while ($row = $sth->fetchrow_arrayref()) {
$timeout1 = @$row[19]; $timeout1 = @$row[19];
$timeout2 = @$row[20]; $timeout2 = @$row[20];
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
$is_running->bind_param( 1, ${id} );
$is_running->execute();
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; } if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
my $template = $run_dir . '/imapsync.XXXXXXX'; my $template = $run_dir . '/imapsync.XXXXXXX';
@ -102,53 +114,47 @@ while ($row = $sth->fetchrow_arrayref()) {
print $passfile1 "$password1\n"; print $passfile1 "$password1\n";
print $passfile2 trim($master_pass) . "\n"; print $passfile2 trim($master_pass) . "\n";
my $command = "/usr/local/bin/imapsync"; my @custom_params_a = qqw($custom_params);
$command .= " --tmpdir /tmp"; my $custom_params_ref = \@custom_params_a;
$command .= " --pidfile /tmp/imapsync.pid";
$command .= " --nofoldersizes"; my $generated_cmds = [ "/usr/local/bin/imapsync",
($timeout1 gt "0" ? () : ($command .= " --timeout1 ${timeout1}")); "--tmpdir", "/tmp",
($timeout2 gt "0" ? () : ($command .= " --timeout2 ${timeout2}")); "--nofoldersizes",
($exclude eq "" ? () : ($command .= qq` --exclude ${exclude}`)); ($timeout1 gt "0" ? () : ('--timeout1', $timeout1)),
($subfolder2 eq "" ? () : ($command .= qq` --subfolder2 ${subfolder2}`)); ($timeout2 gt "0" ? () : ('--timeout2', $timeout2)),
($maxage eq "0" ? () : ($command .= " --maxage ${maxage}")); ($exclude eq "" ? () : ("--exclude", $exclude)),
($maxbytespersecond eq "0" ? () : ($command .= " --maxbytespersecond ${maxbytespersecond}")); ($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
($delete2duplicates ne "1" ? () : ($command .= " --delete2duplicates")); ($maxage eq "0" ? () : ('--maxage', $maxage)),
($subscribeall ne "1" ? () : ($command .= " --subscribeall")); ($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
($delete1 ne "1" ? () : ($command .= " --delete")); ($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
($delete2 ne "1" ? () : ($command .= " --delete2")); ($subscribeall ne "1" ? () : ('--subscribeall')),
($automap ne "1" ? () : ($command .= " --automap")); ($delete1 ne "1" ? () : ('--delete')),
($skipcrossduplicates ne "1" ? () : ($command .= " --skipcrossduplicates")); ($delete2 ne "1" ? () : ('--delete2')),
(!defined($enc1) ? () : ($command .= " ${enc1}")); ($automap ne "1" ? () : ('--automap')),
$command .= " --host1 ${host1}"; ($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
$command .= qq` --user1 ${user1}`; (!defined($enc1) ? () : ($enc1)),
$command .= " --passfile1 $passfile1->filename"; "--host1", $host1,
$command .= " --port1 ${port1}"; "--user1", $user1,
$command .= " --host2 localhost"; "--passfile1", $passfile1->filename,
$command .= " --user2 ${user2}" . '*' . trim($master_user); "--port1", $port1,
$command .= " --passfile2 $passfile2->filename"; "--host2", "localhost",
$command .= " --no-modulesversion"; "--user2", $user2 . '*' . trim($master_user),
($custom_params eq "" ? () : ($command .= qq` ${custom_params}`)); "--passfile2", $passfile2->filename,
'--no-modulesversion'];
try { try {
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?"); run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout;
$is_running->bind_param( 1, ${id} );
$is_running->execute();
my $stdout = `${command}`
$update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?"); $update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
$update->bind_param( 1, ${stdout} ); $update->bind_param( 1, ${stdout} );
$update->bind_param( 2, ${id} ); $update->bind_param( 2, ${id} );
$update->execute(); $update->execute();
} catch { } catch {
$update = $dbh->prepare("UPDATE imapsync SET returned_text = 'Could not start or finish imapsync' WHERE id = ?"); $update = $dbh->prepare("UPDATE imapsync SET returned_text = 'Could not start or finish imapsync', last_run = NOW(), is_running = 0 WHERE id = ?");
$update->bind_param( 1, ${id} );
$update->execute();
} finally {
$update = $dbh->prepare("UPDATE imapsync SET last_run = NOW(), is_running = 0 WHERE id = ?");
$update->bind_param( 1, ${id} ); $update->bind_param( 1, ${id} );
$update->execute(); $update->execute();
$lockmgr->unlock($lock_file);
}; };
} }
$sth->finish(); $sth->finish();