make rc import/export work with vmail-stack db config

This commit is contained in:
Matthew Saunders Brown 2021-03-17 13:25:44 -07:00
parent 9fd01b5dd4
commit 5f578978cd
2 changed files with 28 additions and 12 deletions

View File

@ -1,24 +1,34 @@
#!/usr/bin/php
<?php
$virtualhost = $argv[1];
if ($virtualhost == "") {
if (isset($argv[1])) {
$virtualhost = $argv[1];
} else {
echo "usage:\n";
echo " $argv[0] virtualhost\n";
exit;
}
if (!is_dir("/var/www/$virtualhost")) {
if (!is_dir("/var/vmail/$virtualhost")) {
echo "$virtualhost is not installed\n";
exit;
}
if (!is_writable("/var/vmail/$virtualhost")) {
$user = posix_getpwuid(posix_geteuid());
$username = $user['name'];
echo "/var/vmail/$virtualhost is not writable by this user ($username).\n";
echo "re-run this script as user vmail or root or another user that can write to /var/vmail/$virtualhost\n";
exit;
}
$roundcubemail = array();
$dbPass = trim(`grep -m 1 password /etc/mysql/debian.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect('localhost', 'root', "$dbPass", 'vmail');
$dbHost = trim(`grep -m 1 host /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbDatabase = trim(`grep -m 1 database /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbUser = trim(`grep -m 1 user /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbPass = trim(`grep -m 1 password /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect($dbHost, $dbUser, "$dbPass", $dbDatabase);
$dbQuery = "SELECT * FROM rc_users WHERE username LIKE '%@$virtualhost'";
$dbResult = mysqli_query($dbLink, $dbQuery);
@ -89,4 +99,4 @@ while ($row_users = mysqli_fetch_assoc($dbResult)) {
}
file_put_contents("/var/www/$virtualhost/roundcubemail", serialize($roundcubemail));
file_put_contents("/var/vmail/$virtualhost/roundcubemail", serialize($roundcubemail));

View File

@ -9,15 +9,18 @@ if ($virtualhost == "") {
exit;
}
if (!is_file("/var/www/$virtualhost/roundcubemail")) {
if (!is_file("/var/vmail/$virtualhost/roundcubemail")) {
echo "$virtualhost roundcubemail file does not exist\n";
exit;
}
$roundcubemail = unserialize(file_get_contents("/var/www/$virtualhost/roundcubemail"));
$roundcubemail = unserialize(file_get_contents("/var/vmail/$virtualhost/roundcubemail"));
$dbPass = trim(`grep -m 1 password /etc/mysql/debian.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect('localhost', 'root', "$dbPass", 'vmail');
$dbHost = trim(`grep -m 1 host /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbDatabase = trim(`grep -m 1 database /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbUser = trim(`grep -m 1 user /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbPass = trim(`grep -m 1 password /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect($dbHost, $dbUser, "$dbPass", $dbDatabase);
$user_id_new = 0;
$identity_id_new = 0;
@ -158,3 +161,6 @@ foreach ($roundcubemail as $virtualhost=>$users_array) {
}
}
echo "Import complete. If it's no longer need you can\n";
echo "remove the file /var/vmail/$virtualhost/roundcubemail\n";