Sådan sikkerhedskopieres en SQLite-database til en fil.
Der er flere måder at sikkerhedskopiere en SQLite-database til fil.
- Brug
.backup
kommando. - Brug
.clone
kommando. - Brug
.dump
kommando. - Kopiér filen til filsystemet.
.backup
Kommando
Denne kommando sikkerhedskopierer en database til en fil. Den accepterer et databasealias (dvs. databasen til backup) og et filnavn (til backupfilen).
Hvis du udelader databasealiaset, vil den bruge main
database.
Her er et eksempel:
.backup mybackup.db
Dette vil oprette en fil kaldet
backup.db
indeholdende en backup af databasen. Du kan vedhæfte dette tilbage til SQLite3-kommandolinjeskallen, hvis det kræves (udfør derefter en .databases
for at se listen over databaseforbindelser):
sqlite> ATTACH DATABASE 'mybackup.db' AS MyBackup; sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /Users/quackit/sqlite/music.db 1 temp 2 MyBackup /Users/quackit/sqlite/mybackup.db
I praksis ville du sandsynligvis tage backup af databasen til en anden placering. Derfor vil du angive den fulde filsti i .backup
kommando. For eksempel .backup /remote/folder/mybackup.db
.clone
Kommando
.clone
kommandoen ligner .backup
kommando. Dog .clone
bruger kun den aktuelle database, så du kan ikke angive en anden database til at klone.
For at klone den aktuelle database skal du skrive .clone
efterfulgt af navnet på databasefilen for de klonede data.
Sådan:
.clone myclone.db
Kørsel, der ser sådan ud:
sqlite> .clone myclone.db Artists... done Albums... done Albums2... done Catalog... done Genres... done
Du kan vedhæfte den klonede database ligesom den anden:
sqlite> ATTACH DATABASE 'myclone.db' AS MyClone; sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /Users/quackit/sqlite/music.db 1 temp 2 MyBackup /Users/quackit/sqlite/mybackup.db 3 MyClone /Users/quackit/sqlite/myclone.db
.dump
Kommando
Du kan bruge .dump
kommando for at dumpe databasen til en ASCII-fil. Du kan f.eks. dumpe den til en .sql-fil, der indeholder SQL-sætningerne til at generere databasen ud fra.
Dump hele DB
Dette eksempel dumper music.db fil til music.sql .
sqlite3 music.db .dump > music.sql
Indhold af music.sql (rulning påkrævet):
PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE Artists( ArtistId INTEGER PRIMARY KEY, ArtistName TEXT NOT NULL , Bio TEXT); INSERT INTO "Artists" VALUES(1,'Joe Satriani',NULL); INSERT INTO "Artists" VALUES(2,'Steve Vai',NULL); INSERT INTO "Artists" VALUES(3,'The Tea Party',NULL); INSERT INTO "Artists" VALUES(4,'Noiseworks',NULL); INSERT INTO "Artists" VALUES(5,'Wayne Jury',NULL); INSERT INTO "Artists" VALUES(6,'Mr Percival',NULL); INSERT INTO "Artists" VALUES(7,'Iron Maiden',NULL); INSERT INTO "Artists" VALUES(8,'Atmasphere','Australian jazz band centred around polyrhythms.'); INSERT INTO "Artists" VALUES(9,'Ian Moss',NULL); INSERT INTO "Artists" VALUES(10,'Magnum',NULL); INSERT INTO "Artists" VALUES(13,'Primus',NULL); INSERT INTO "Artists" VALUES(14,'Pat Metheny',NULL); INSERT INTO "Artists" VALUES(15,'Frank Gambale',NULL); INSERT INTO "Artists" VALUES(16,'Mothers of Invention',NULL); CREATE TABLE Albums( AlbumId INTEGER PRIMARY KEY, AlbumName TEXT NOT NULL, ReleaseDate TEXT, ArtistId INTEGER NOT NULL, FOREIGN KEY(ArtistId) REFERENCES Artists(ArtistId) ); INSERT INTO "Albums" VALUES(1,'Killers','1981',7); INSERT INTO "Albums" VALUES(2,'Powerslave','1984',7); INSERT INTO "Albums" VALUES(3,'Surfing with the Alien','1987',1); INSERT INTO "Albums" VALUES(4,'Heavy as a Really Heavy Thing','1995',11); INSERT INTO "Albums" VALUES(6,'Out of the Loop','2007',6); INSERT INTO "Albums" VALUES(7,'Suck on This','1989',13); INSERT INTO "Albums" VALUES(8,'Pork Soda','1993',13); INSERT INTO "Albums" VALUES(9,'Sailing the Seas of Cheese','1991',13); INSERT INTO "Albums" VALUES(10,'Flying in a Blue Dream','1989',1); INSERT INTO "Albums" VALUES(11,'Black Swans and Wormhole Wizards','2010',1); INSERT INTO "Albums" VALUES(12,'Somewhere in Time','1986',7); CREATE TABLE Albums2( AlbumId INT, AlbumName TEXT, ArtistId INT ); INSERT INTO "Albums2" VALUES(1,'Killers',7); INSERT INTO "Albums2" VALUES(2,'Powerslave',7); INSERT INTO "Albums2" VALUES(3,'Surfing with the Alien',1); INSERT INTO "Albums2" VALUES(4,'Heavy as a Really Heavy Thing',11); INSERT INTO "Albums2" VALUES(5,'Yummy Yummy',17); INSERT INTO "Albums2" VALUES(6,'Out of the Loop',6); INSERT INTO "Albums2" VALUES(7,'Suck on This',13); INSERT INTO "Albums2" VALUES(8,'Pork Soda',13); INSERT INTO "Albums2" VALUES(9,'Sailing the Seas of Cheese',13); INSERT INTO "Albums2" VALUES(10,'Flying in a Blue Dream',1); INSERT INTO "Albums2" VALUES(11,'Black Swans and Wormhole Wizards',1); INSERT INTO "Albums2" VALUES(12,'Somewhere in Time',7); INSERT INTO "Albums2" VALUES(13,'Big Red Car',17); CREATE TABLE Catalog( "AlbumId" TEXT, "AlbumName" TEXT, "ArtistName" TEXT ); INSERT INTO "Catalog" VALUES('1','Killers','Iron Maiden'); INSERT INTO "Catalog" VALUES('2','Powerslave','Iron Maiden'); INSERT INTO "Catalog" VALUES('12','Somewhere in Time','Iron Maiden'); INSERT INTO "Catalog" VALUES('3','Surfing with the Alien','Joe Satriani'); INSERT INTO "Catalog" VALUES('10','Flying in a Blue Dream','Joe Satriani'); INSERT INTO "Catalog" VALUES('11','Black Swans and Wormhole Wizards','Joe Satriani'); INSERT INTO "Catalog" VALUES('6','Out of the Loop','Mr Percival'); INSERT INTO "Catalog" VALUES('7','Suck on This','Primus'); INSERT INTO "Catalog" VALUES('8','Pork Soda','Primus'); INSERT INTO "Catalog" VALUES('9','Sailing the Seas of Cheese','Primus'); CREATE TABLE Genres( GenreId INTEGER PRIMARY KEY, Genre TEXT NOT NULL ); INSERT INTO "Genres" VALUES(1,'Rock'); INSERT INTO "Genres" VALUES(2,'Country'); INSERT INTO "Genres" VALUES(3,'Pop'); INSERT INTO "Genres" VALUES(4,'Comedy'); INSERT INTO "Genres" VALUES(5,'Jazz'); INSERT INTO "Genres" VALUES(6,'Blues'); INSERT INTO "Genres" VALUES(7,'Techno'); COMMIT;
Kopiér filen
Ovenstående metoder giver dig mulighed for at sikkerhedskopiere en database fra SQLite3-kommandolinjeskallen.
Du kan også sikkerhedskopiere en database ved blot at kopiere/indsætte den fysiske fil på filsystemet.
Hvis du ikke er sikker på placeringen af den fysiske fil, kan du bruge .databases
kommando for at finde placeringen:
sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /Users/quackit/sqlite/music.db 1 temp
Så bevæbnet med ovenstående viden kan jeg nu navigere til /Users/quackit/sqlite/ mappen, kopier music.db fil, og indsæt den på et sikkert sted.