SQLite Forum

Perl backup of database
Login
Well, this is typical of my work flow (2 ahead, 3 back).
I've implemented your alternative code but something is still wonky. It writes the output SQL to the screen instead of a file, but does create a 0-length file properly named.

    my $Outfile = "$TARDIR/$Today.sql";
    my $Infile = "$SRCDIR/kba.db";
    die "No such backup directory $TARDIR!\n" unless -d $TARDIR;
    open my $backup, '>', $Outfile or die "Cannot create backup file $Outfile: $!\n";
    select $Outfile;   # send STDOUT to backup file
    open my $cmd, '|-', '/usr/bin/sqlite3', '$Infile', '.dump' or die "Failed to dump DB $Outfile: $!\n";
    close $cmd or die "Backup to $Outfile failed: $!\n";
    select STDOUT;   # restore STDOUT redirect

This seems very close if the output can get to the right place.

I've also tried 'echo .dump | sqlite3 kba.db > Today.sql' from a Terminal window and that works perfectly.
But in a script 'system("echo .dump \| sqlite3 $SRCDIR/kba.db > $TARDIR/$Today.sql");' does not create a file.