export()
Dumps the database contents to a file.
NoteWebSocket connections currently do not currently support exports and imports. Be sure to use an HTTP endpoint when using this method.
Method Syntaxdb.export(target)
Argument | Description | ||
---|---|---|---|
resource | The table name or a record ID to select. Will also accept a tuple of record name and ID. |
The .export()
method can be used to save the contents of a database to a file.
use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::opt::Resource; async fn main() -> surrealdb::Result<()> { let db = connect("http://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; // Create a `person` record db.create(Resource::from("person")).await?; db.export("backup.surql").await?; Ok(()) }
If an empty tuple is passed in for the file name, the .export()
method will instead return an async stream of bytes.
use futures::StreamExt; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::opt::Resource; async fn main() -> surrealdb::Result<()> { let db = connect("http://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; // Create a `person` record db.create(Resource::from("person")).await?; let mut stream = db.export(()).await?; while let Some(Ok(line)) = stream.next().await { let content = String::from_utf8(line).unwrap(); println!("{content}"); } Ok(()) }
The output for the above sample should look like the following.
-- ------------------------------ -- OPTION -- ------------------------------ OPTION IMPORT; -- ------------------------------ -- TABLE: person -- ------------------------------ DEFINE TABLE person TYPE ANY SCHEMALESS PERMISSIONS NONE; -- ------------------------------ -- TABLE DATA: person -- ------------------------------ INSERT [ { id: person:bgq0b0rblnozrufizdjm } ];