Cleanup old Gems, but not rails and friends
If you’re like me and have several rails applications in different rails versions, it’s difficult to just run a gem cleanup, because it would mean all your old rails gems will also be deleted.
You don’t want that, but you also don’t want to clean them up one by one.
Here’s easy way to do it from your bash command line:
for g in $(sudo gem cleanup --dryrun \
| awk '$1=="Dry" && $6!~/^(rails|action(mailer|webservice|pack)|active(resource|support|record))/ { print $6}' \
| sed -E -e 's/-([0123456789].?)+$//g' \
| sort --unique --ignore-case); do
sudo gem cleanup $g
doneJust copy the code above in your terminal, make sure you’ve got sudo rights.
You might get warnings about dependencies, just say no and rerun the code if needed.
Quickly cleanup your Thunderbird IMAP folder cache
When you connect with multiple programs to your IMAP store, it is possible Thunderbird can be confused about what should be on the server and what it has on disk.
Result: Thunderbird just hangs with massive CPU usage.
A fix can be found at Mozillazine considering Phantom Folders.
A quick oneliner can solve the situation:
Quit Thunderbird, cd into your profile directory and:
find . \( -iname panacea.dat -or -iname *.msf -or -iname XUL.mfl \) -exec rm '{}' \; Open Thunderbird and everything should be fine again.
Change unix password oneliner
I tried using expect for changing passwords from a script, but expect sometimes works, sometimes not, at least in my case. So I came up with the following oneliner, as root do the following:
usermod -p `perl -e 'print crypt('mysupersecretpassword',rand(99));'` goofy
where mysupersecretpassword is the password you want to use and goofy is the user where you want to change the password for.
Upgrade your PostgreSQL databases to UNICODE
Recently, I had the fine oppertunity to upgrade all my CMS powered PostgreSQL databases and perl powered websites from SQL_ASCII to Unicode.
If you don’t know what unicode is, have a look at this article: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
It was less painfull then I thought, in fact there are 6 steps
- Dump your database
- Remove invalid characters from the dump
- Make a diff from the dump
- Remove current database
- Create a new databse
- Load the data into the database you’ve created in step 2
Below is a simple bash script to manage this stuff. Edit it to your needs.
#!/bin/bash
db=$1
# This script updates all dbs to use unicode
dbhost='localhost'
username='larik'
odir=${HOME}/CMS_UPGRADE
if [ "${db}X" == "X" ]
then
echo "I need a db for host ${dbhost} and username ${username} $db"
exit
fi
if [ ! -d $odir ]
then
mkdir $odir || exit "Exit at mkdir"
fi
dump_sql=${odir}/${db}_out.sql
conv_sql=${odir}/${db}_conv.sql
result_sql=${odir}/${db}_result.txt
sql_diff=${odir}/${db}.diff
extra_string="database: ${db}, host: ${dbhost}, user: ${username}"
# Wat dient er te gebeuren:
# 1. Dump database
pg_dump --host=$dbhost --username=$username -D --file=$dump_sql --format=p $db || exit "exit at pg_dump ${extra_string}"
# 2. Remove invalid characters from the dump
/sw/bin/iconv -c -f UTF-8 -t UTF-8 ${dump_sql} > ${conv_sql} || exit "exit at iconv ${extra_string}"
# 3 Make a diff from the dump
diff $dump_sql $conv_sql > $sql_diff
# 4. Remove current database
dropdb --host=$dbhost --username=$username $db || exit "exit at dropdb ${extra_string}"
# 5. Create a new databse
createdb --encoding='unicode' --host=$dbhost --username=$username $db || exit "exit at createdb ${extra_string}"
# 6. Load the data into the database you've created in step 2
psql -f $conv_sql -o $result_sql -h $dbhost -U $username $db || exit "exit at psql ${extra_string}"Notes about this script:
- Make sure you have iconv installed. It is standard on Linux and Mac OS X
- Make sure that the pg_dump you run is the same version as the database you’re dumping
- Check your diff files after the script has run, invalid UTF-8 characters will be removed from and won’t be available in the new sql script
Have fun!