DB Import & Export: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Commands =
= Commands =


== MariaDB ==
{| class="wikitable"
{| class="wikitable"
! colspan="2" | MariaDB  
! colspan="2" | MariaDB  
Line 21: Line 22:
</syntaxhighlight>
</syntaxhighlight>
|}
|}
== SQLite3 ==


{| class="wikitable"
{| class="wikitable"
Line 30: Line 33:
sqlite3 my_wiki.sqlite '.dump'
sqlite3 my_wiki.sqlite '.dump'
sqlite3 my_wiki.sqlite '.dump user'
sqlite3 my_wiki.sqlite '.dump user'
sqlite3 -header my_wiki.sqlite '.mode insert' 'select * from user;'
</syntaxhighlight>
</syntaxhighlight>
|-
|-
Line 38: Line 42:
</syntaxhighlight>
</syntaxhighlight>
|}
|}
== Postgres ==


{| class="wikitable"
{| class="wikitable"

Latest revision as of 16:57, 23 April 2025

Commands

MariaDB

MariaDB
Export database
mariadb-dump -p my_wiki
mariadb-dump -p --compatible=postgresql my_wiki 
mariadb-dump -p --no-data --compatible=postgresql my_wiki
mariadb-dump -p --no-create_info --compatible=postgresql my_wiki
mariadb-dump -p --no-create_info --extended-insert=false --compatible=postgresql my_wiki
Restore database
echo 'create database my_wiki;' | mariadb -p
mariadb -p < data-to-import.sql

SQLite3

SQLite 3
Export database
sqlite3 my_wiki.sqlite '.dump'
sqlite3 my_wiki.sqlite '.dump user'
sqlite3 -header my_wiki.sqlite '.mode insert' 'select * from user;'
Restore database

Postgres

Postgres
Export database
Restore database
psql -U postgres

MariaDB

Script to sync db

DATESN=`date +'%m%d'`
DBNAME=mydb
DBFILE=~/Downloads/${DBNAME}-${DATESN}.sql
SSHTUN=13306
SSHCTRL=tun.ctrl
SSHCONF=myconf
RPASS=********
LPASS=********

if [ -e $SSHCTRL ]; then
  echo "* Close existed tunnel."
  ssh -S $SSHCTRL -O exit $SSHCONF 2> /dev/null
fi

echo "* Load data from remote and save as $DBFILE"
ssh -M -S $SSHCTRL -NCfL $SSHTUN:127.0.0.1:3306 $SSHCONF
mysqldump -h 127.0.0.1 -P $SSHTUN -u root -p$RPASS $DBNAME > $DBFILE
ssh -S $SSHCTRL -O exit $SSHCONF 2> /dev/null

echo '* Import data into local'
echo "DROP DATABASE IF EXISTS $DBNAME" | mysql -u root -p$LPASS test
echo "CREATE DATABASE IF NOT EXISTS $DBNAME" | mysql -u root -p$LPASS test
echo "source $DBFILE" | mysql -u root -p$LPASS $DBNAME

echo 'Done.'