From 9ff849fc8703ed6a86c71bb6796172a087789593 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 4 Feb 2009 20:55:57 +0000 Subject: [PATCH] Add ".backup" and ".restore" commands to the CLI - implemented using the new backup API. (CVS 6259) FossilOrigin-Name: 003e1d62189e9e37f901d86a696cfccd22bd3b38 --- manifest | 12 ++++---- manifest.uuid | 2 +- src/shell.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 7602c37a93..e2cbfd5a9d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Increase\sthe\sversion\snumber\sto\s3.6.11.\s(CVS\s6258) -D 2009-02-04T20:08:26 +C Add\s".backup"\sand\s".restore"\scommands\sto\sthe\sCLI\s-\simplemented\susing\sthe\nnew\sbackup\sAPI.\s(CVS\s6259) +D 2009-02-04T20:55:58 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in c7a5a30fb6852bd7839b1024e1661da8549878ee F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -156,7 +156,7 @@ F src/random.c 676b9d7ac820fe81e6fb2394ac8c10cff7f38628 F src/resolve.c 18dc9f0df1d60048e012ce6632251063e0dd356a F src/rowset.c ba9375f37053d422dd76965a9c370a13b6e1aac4 F src/select.c ae72b604e47092521c4d9ae54e1b1cbeb872a747 -F src/shell.c 8965cf0cd7a7dac39d586a43c97adb00930e025d +F src/shell.c 6c674a4a4cc56c70ecfb26b07d486d43740b475f F src/sqlite.h.in 31fa12602f784adea9be66424a2e8b052116736f F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17 F src/sqliteInt.h 73c1d4f9716fe21f202f9d05c4fd9e6281f2636f @@ -700,7 +700,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P d4af60e52a12262ad0194908e68a386c719fe177 -R f6bc70a4dbe805a45f380940b0bdb3b4 +P 0882a028c8cee868bb79728499fb1fa7c0630fa6 +R 6acad12f687e13cb8812850fc6ee53f0 U drh -Z ebc074439167f9596ca44285def5f13d +Z 7b9fc40e749f6a4e8493cccb068ac51f diff --git a/manifest.uuid b/manifest.uuid index bb1705f721..46fa374815 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0882a028c8cee868bb79728499fb1fa7c0630fa6 \ No newline at end of file +003e1d62189e9e37f901d86a696cfccd22bd3b38 \ No newline at end of file diff --git a/src/shell.c b/src/shell.c index 00e7b66233..b1f0f6cab3 100644 --- a/src/shell.c +++ b/src/shell.c @@ -12,7 +12,7 @@ ** This file contains code to implement the "sqlite" command line ** utility for accessing SQLite databases. ** -** $Id: shell.c,v 1.199 2009/01/30 05:40:27 shane Exp $ +** $Id: shell.c,v 1.200 2009/02/04 20:55:58 drh Exp $ */ #if defined(_WIN32) || defined(WIN32) /* This needs to come before any includes for MSVC compiler */ @@ -930,6 +930,7 @@ static int run_schema_dump_query( ** Text of a help message */ static char zHelp[] = + ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" ".bail ON|OFF Stop after hitting an error. Default OFF\n" ".databases List names and files of attached databases\n" ".dump ?TABLE? ... Dump the database in an SQL text format\n" @@ -961,6 +962,7 @@ static char zHelp[] = ".prompt MAIN CONTINUE Replace the standard prompts\n" ".quit Exit this program\n" ".read FILENAME Execute SQL in FILENAME\n" + ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" ".schema ?TABLE? Show the CREATE statements\n" ".separator STRING Change separator used by output mode and .import\n" ".show Show the current values for various settings\n" @@ -1092,7 +1094,42 @@ static int do_meta_command(char *zLine, struct callback_data *p){ if( nArg==0 ) return rc; n = strlen30(azArg[0]); c = azArg[0][0]; - if( c=='b' && n>1 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){ + if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 ){ + const char *zDestFile; + const char *zDb; + sqlite3 *pDest; + sqlite3_backup *pBackup; + int rc; + if( nArg==2 ){ + zDestFile = azArg[1]; + zDb = "main"; + }else{ + zDestFile = azArg[2]; + zDb = azArg[1]; + } + rc = sqlite3_open(zDestFile, &pDest); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Error: cannot open %s\n", zDestFile); + sqlite3_close(pDest); + return 1; + } + pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); + if( pBackup==0 ){ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + sqlite3_close(pDest); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + }else{ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + } + sqlite3_close(pDest); + }else + + if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){ bail_on_error = booleanValue(azArg[1]); }else @@ -1453,7 +1490,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){ rc = 2; }else - if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ + if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ FILE *alt = fopen(azArg[1], "rb"); if( alt==0 ){ fprintf(stderr,"can't open \"%s\"\n", azArg[1]); @@ -1463,6 +1500,45 @@ static int do_meta_command(char *zLine, struct callback_data *p){ } }else + if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 ){ + const char *zSrcFile; + const char *zDb; + sqlite3 *pSrc; + sqlite3_backup *pBackup; + int rc; + if( nArg==2 ){ + zSrcFile = azArg[1]; + zDb = "main"; + }else{ + zSrcFile = azArg[2]; + zDb = azArg[1]; + } + rc = sqlite3_open(zSrcFile, &pSrc); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Error: cannot open %s\n", zSrcFile); + sqlite3_close(pSrc); + return 1; + } + pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); + if( pBackup==0 ){ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + sqlite3_close(pSrc); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){ + if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + sqlite3_sleep(10); + } + } + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + }else{ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + } + sqlite3_close(pSrc); + }else + if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ struct callback_data data; char *zErrMsg = 0;