Posts Tagged ‘oneliners’

Loading mysql database from compressed dump

Friday, April 9th, 2010

Today I had to load database on mysql. There`s nothing new or exciting about it, but I encountered few problems. First one is that dump was quite big (few gigabytes) and it was compressed with gzip. (G)Unzipping it simply to a file would take some time, waste space on disk and it wasn`t the right way ;-) So here how I managed to decompress it and load it at the same time:

gunzip -c mydbdump.sql.gz|mysql -umyuser -pmypass mydb

But then I found another problem – in my dump there was hardcoded database name. I wasn`t recovering that database, but just wanted to load it to a diffrent one. Names weren`t the same so it failed to load. I looked at the begining of that file using head command, as opening so huge file in vi(m) would probably kill the server. I found that there was two sql commands that creates database itself and use it (sql use command). So with a little help of sed I managed to modify my command so it looked like this:

gunzip -c mydbdump.sql.gz| sed -e '1,30s/old_dbname/mydb/'| mysql -umyuser -pmypass mydb

I limited sed`s search&replace to first 30 lines, because database name was at the start of the dump file and I didn`t want to mess with the rest of the file :-)

Proste statystyki wydruków z bazą sqlite

Friday, August 21st, 2009

Potrzebowałem na szybko statystyk wydruków. Do tego skonstruowałem taki oto twór:

perl -ne 'print "$1 $2\n" if /(\d+\/[a-zA-z]+\/20\d{2}).*for job (\d+)\./' /var/log/cups/error_log|sort |uniq | awk '{print "insert into printouts values(\""$1"\","$2");"}'| sqlite3 printouts.db

Ten z pozoru niezgrabny jednolinijkowiec parsuje plik logów cupsa wyciągając z niego numer zadania i datę, a następnie wkłada to do prostej bazy sqlite`a.

Dzięki temu mogłem szybko wydobyć ilość wydruków na dzień. Przy niewielkiej modyfikacji można dołączyć inne pola i już grupować sobie w sqlu co tylko dusza zapragnie. Nie jest to może wyszukany przypadek – raczej ciekawostka. To samo można uzyskać również za pomocą uniq -c, ale perspektywy tego rozwiązania są o wiele większe.

Na koniec tylko polecenie tworzące trywialną strukturę bazy printouts.db:

sqlite3 printouts.db 'create table printouts(date text,job int)'