Mysqldump Cron job failing because of vi


Hello. I’ve done the following via shell and it worked successfully…but when doing it through cron I get errors. Does anyone know a work around for this? Thanks

cd /home/account
rm account.dump.sql
mysqldump -uuser -ppassword -h --opt oldaccount > account.dump.sql
vi account.dump.sql -c :g/oldaccount/s//newaccount/g -c :wq!
mysqldump -uuser -ppassword -h --opt newaccount < account.dump.sql

when doing it via cron I get:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
[m [m [0m [H [2J [24;1H"account.dump.sql" 1091L, 16457780C
"account.dump.sql" 1091L, 16457780C written
– MySQL dump 10.10


I’m no expert, but I think vi needs a terminal to run in and I don’t think cron runs in a terminal.

what exactly are you editing with vi?


yeah, that’s what I was thinking. the vi command above searches and replaces oldaccount with newaccount and then closes with saving. I guess the question then is “is there another way to do this” or “is there a way to pass terminal using cron”


That last command is a dump command. It looks like you’re trying to populate a database, in which case, you should be using the ‘mysql’ command instead of ‘mysqldump’.



Thanks. I just realized I was confusing myself with that. As usual, I was over complicating it.

Turns out all I need was one line:
mysqldump -uuser -ppassword -h --opt oldaccount | mysql -uuser -ppassword -h newaccount

But I also think i figured out a way to edit a file in a shell script without getting those previous errors (irrelevant now though):
sed -n -e ‘s/oldaccount/newaccount/’ account.dump.sql

Thanks for the help


Just so you know for future reference, ‘sed’ is the tool that is normally used in a *nix environment for applying regular expression substitutions in a shell script. It’s also exactly what should be used in your case.

I’ll also point out that if you don’t particularly care about keeping account.dump.sql around until the next run, you can get rid of it entirely. ‘sed’ (think “stream editor”) is what the *nix world considers a filter program, meaning that it can read input from standard input, apply a transformation to it, and output the result to standard output, all without human interaction. Thus, it’s a perfect program to use in the middle of a pipe where you have some source program (mysqldump) and some sink program (mysql) where you need to do something in between (with a filter like sed) but don’t want to create a bunch of temporary files.

In your case, it’d look something like:

mysqldump | sed | mysqlI know you said you’ve got it all working, so this surely comes as largely irrelevant to you. But then again, the more you know… :slight_smile: