Created
June 10, 2012 00:25
-
-
Save r10r/2903223 to your computer and use it in GitHub Desktop.
Bulk load postfix virtual aliases map into mysql table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
make -f Makefile.init makefiles \ | |
CCARGS='-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS -I/usr/include/openssl' \ | |
AUXLIBS='-L/usr/lib -lmysqlclient -lsasl2 -lssl -lcrypto -lz -lm' | |
make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'mysql' | |
# read file | |
entries = File.read('addresses').split("\n").collect {|entry| entry.split(/\t+/) } | |
puts "Inserting #{entries.length} entries" | |
begin | |
connection = Mysql.new('localhost', 'user', 'password', 'database') | |
entries.each do |entry| | |
if entry.length == 2 | |
puts "Adding entry #{entry}" | |
connection.query("insert into virtual_aliases (alias, mailbox) values('#{entry[0]}','#{entry[1]}')") | |
else | |
puts "Skipping entry #{entry}" | |
end | |
end | |
ensure | |
connection.close | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `virtual_aliases` ( | |
`alias` varchar(100) NOT NULL, | |
`mailbox` varchar(100) NOT NULL, | |
`created_at` datetime DEFAULT NULL, | |
`expire_at` datetime DEFAULT NULL, | |
`enabled` int(1) DEFAULT '1', | |
UNIQUE KEY `unique_alias` (`alias`), | |
KEY `mailbox_alias` (`alias`), | |
KEY `mailbox_expire_at` (`expire_at`) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment