Skip to content

Instantly share code, notes, and snippets.

@zeroc0d3
Created March 27, 2017 10:33
Show Gist options
  • Save zeroc0d3/b74a35ce52a0f5225c906adce2cf09e5 to your computer and use it in GitHub Desktop.
Save zeroc0d3/b74a35ce52a0f5225c906adce2cf09e5 to your computer and use it in GitHub Desktop.
Export data from sqlite3 file using bash script
#!/bin/sh
#################
# ZeroC0D3 Team #
#################
### STEP ###
# 1) Download binary file "sqlite3" from
# https://www.sqlite.org/
# 2) Extract binary "sqlite3" to your PATH_BIN
# 3) Set your file name in PATH_TARGET_DUMP for all schema & data
# 4) Set your file name in PATH_TARGET_SCHEMA for only schema
# 5) Set your file name in PATH_TARGET_CSV for only csv format
# 6) Run this script in terminal: sh export_sqlite.sh
PATH_BIN="/home/user/project/bin/";
PATH_SOURCE=$PATH_BIN"db/sample.sqlite3";
PATH_TARGET_DUMP=$PATH_BIN"dump/export_all.db";
PATH_TARGET_SCHEMA=$PATH_BIN"dump/export_schema.db";
PATH_TARGET_CSV=$PATH_BIN"dump/export_csv.csv";
##### DUMP ALL SCHEMA & DATA #####
sqlite3 $PATH_SOURCE .dump > $PATH_TARGET_DUMP;
echo "Done... Export all to: " $PATH_TARGET_DUMP;
#### DUMP SCHEMA (ONLY) #####
sqlite3 $PATH_SOURCE .schema > $PATH_TARGET_SCHEMA;
echo "Done... Export schema (only) to: " $PATH_TARGET_SCHEMA;
#### DUMP TO CSV FILE #####
QUERY="SELECT * FROM table";
# with header
# sqlite3 -csv -header $PATH_SOURCE $QUERY > $PATH_TARGET_CSV;
# without header
sqlite3 -csv $PATH_SOURCE $QUERY > $PATH_TARGET_CSV;
echo "Done... Export csv file to: " $PATH_TARGET_CSV;
echo "All Done !";
@Telematica
Copy link

Will fork this. .backup and VACUUM INTO deserve a look as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment