Skip to content

Instantly share code, notes, and snippets.

@rbhushan90
Forked from el22or/mysql-export-import
Created April 8, 2017 04:27
Show Gist options
  • Save rbhushan90/7e695a0087efc6a857c524f56b6654fd to your computer and use it in GitHub Desktop.
Save rbhushan90/7e695a0087efc6a857c524f56b6654fd to your computer and use it in GitHub Desktop.
Remote MySQL database dump directly into local database
## SSH - Remote export > local import
ssh USER@HOST mysqldump -uREMOTEDATABASEUSER -pREMOTEDATABASEPASSWORD -hREMOTEHOST REMOTEDATABASENAME | mysql -uLOCALDATABASEUSER -pLOCALDATABASEPASSWORD LOCALDATABASENAME
## Export to CSV
mysql -uUSER -pPASS DATABASENAME -B -e "select * from \`korisnici\`;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv
## Export to CSV 2
mysql -uUSER -pPASS DATABASE -B -e "SELECT users.uid AS 'ID', users.name AS 'Username', users.mail AS 'Email', from_unixtime(users.created) AS 'Created', from_unixtime(users.login) AS 'Last login' FROM users WHERE users.status=1 AND users.login!=0 ORDER BY users.login DESC;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv
## Export to SQL gzipped file with compression
ssh USER@HOST "mysqldump -uDBUSER -pDBPASS -hDBHOST DBNAME | gzip -3 -c" > FILENAME.sql.gz
## Import gzipped SQL file
gunzip < FILENAME.sql.gz | ssh mysql -uDBNAME -pDBPASS -hDBHOST DBNAME
## Import gzipped SQL compressed file
gunzip -c < FILENAME.sql.gz | ssh mysql -uDBNAME -pDBPASS -hDBHOST DBNAME
## Import SQL with Drush
gunzip -c < FILENAME.sql.gz | drush sql-connect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment