The instructions are here – a good plan to do this before you update. Anyway, it turns out that they’re kept in a sqlite3 file in

/var/mobile/Library/SMS/sms.db

if you want to do this on the iphone itself through terminal you need to apt-get install sqlite3

Then you need to convert to CSV:

sqlite3 -csv -separator ',' sms.db "select * from message;" > output.csv

rowid: A unique identifier for each message
address: The telephone number of the sender / recipient
date: The Unix Timestamp of when the message was sent / received
text: The message content
flags: 3 Indicates that the message was sent, 2 indicates that it was received
group_id: Indicates the conversation the message belongs to (messages are linked together via groups and the group_member table).

If you use SSH to back up and replace the files, make sure you keep the permissions right:
-rw-r--r-- 1 mobile mobile 61440 Sep  2 22:35 /var/mobile/Library/SMS/sms.db

Exporting your iPhone’s SMS Message Database to a CSV file | Geekology.