Skip to content

Instantly share code, notes, and snippets.

@mesaglam
Last active August 29, 2015 14:23
Show Gist options
  • Save mesaglam/d87971ee7c409bdad883 to your computer and use it in GitHub Desktop.
Save mesaglam/d87971ee7c409bdad883 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Tüm database'leri farklı dosyalara dump etme scripti
# 1. Her tablo ayrı sql dosyalara dump edilir.
# 2. İlk parametre ile şema adı verilir, tüm şemalar için tire (-) kullanılır.
# 3. İkinci parametre ile yol verilir, verilmezse "/yedek" lokasyonuna alır.
#
#
# Yazar Mehmet SAĞLAM <[email protected]>
#
USER="username"
PASSWORD="pass"
SCHEMA="$1"
OUTPUTDIR="$2"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
echo "************************************************"
echo "************************************************"
echo "************* Kullanma şekli *******************"
echo "******** yedek_ayri.sh şema lokasyon ***********"
echo "************************************************"
echo "************************************************"
echo "************************************************"
# veritabanı listesini çek
if [[ $SCHEMA == *"-"* ]]; then
databases=`$MYSQL -u$USER -p$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
else
databases=$SCHEMA
fi
if [ -z "$OUTPUTDIR" ]; then
OUTPUTDIR="/yedek"
fi
# başla
for db in $databases; do
echo "DATABASE -> $db"
# get list of items in this database
tables=`$MYSQL -u$USER -p$PASSWORD -e "SHOW TABLES IN $db;" | grep -Ev "(Tables)" `
for table in $tables; do
echo " | ----- TABLE -> $table"
$MYSQLDUMP --skip-comments --compact -u$USER -p$PASSWORD $db $table > "$OUTPUTDIR/$db.$table".sql
done
done
# sıkıştır ve arşivle
cd $OUTPUTDIR && tar -cvf MySql_`date +%Y%m%d`.gz *.sql && rm -rf "$OUTPUTDIR"/*.sql
echo -------------------------------------
echo Finished!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment