Skip to content

Instantly share code, notes, and snippets.

@Demontager
Created May 1, 2014 20:19
Show Gist options
  • Save Demontager/1a1cd52860f87e4a489c to your computer and use it in GitHub Desktop.
Save Demontager/1a1cd52860f87e4a489c to your computer and use it in GitHub Desktop.
#!/bin/sh
# Batch Mysql databases and users' priveleges restore
# Backup directory
BDIR="/home/www/backup/mysql-15-Apr-2014"
LOG="restore.txt"
DATE=`date +%d-%b-%Y_%H:%M:%S`
DBL=`find "$BDIR" -type f \( ! -iname "*.sh" -a ! -iname "*.sql" -a ! -iname "*.txt" \) -print0 | xargs -0 basename -a|awk -F. '{print $1}'`
echo $DBL
exit 0
# Mysql details
DBUSER="root"
DBPASS="pass"
# Create empty databases
for db in $DBL; do
mysql -v -u"$DBUSER" -p"$DBPASS" -e "CREATE DATABASE IF NOT EXISTS \`$db\`" >> "$BDIR/$LOG" 2>&1
done
# Restore users and their privileges
mysql -v -u"$DBUSER" -p"$DBPASS" < "$BDIR/users.sql" >> "$BDIR/$LOG" 2>&1
# Restore databases from archives
for arch in $DBL; do
gunzip < "$BDIR/$arch.gz" | mysql -f -u"$DBUSER" -p"$DBPASS" $arch >> "$BDIR/$LOG" 2>&1
done
# Clean up log
sed -e '/^$/d' "$BDIR/$LOG"|uniq -u|uniq > "$BDIR/tmp.txt"
rm "$BDIR/$LOG"
mv "$BDIR/tmp.txt" "$BDIR/$DATE.$LOG"
@Demontager
Copy link
Author

find /usr/local/www/apache22/data/backup/mysql-15-Apr-2014/ -type f -type f ( ! -iname ".sh" -a ! -iname ".sql" -a ! -iname "*.txt" ) -print0 | xargs -0 basename -a|awk -F. '{print $1}'

chicot
nokia
tempo
ferialab
nbasuper
mosaicsa
zootovar
wnxwzx
organhal
satbois
max-cook

@jagipson
Copy link

jagipson commented May 1, 2014

declare -a dbs
while read; do
REPLY="${REPLY##/}"
dbs+=("${REPLY%.
}")
done < <(find /usr/local/www/apache22/data/backup/mysql-15-Apr-2014/ -type f -iname "*gz")
printf "%s\n" "${dbs[@]}"

@Demontager
Copy link
Author

root@debian:~/scripts# ./test.sh
home/www/backup/mysql-15-Apr-2014/escswim.gz
home/www/backup/mysql-15-Apr-2014/sauvig.gz
home/www/backup/mysql-15-Apr-2014/amitysource.gz
home/www/backup/mysql-15-Apr-2014/ennixtalks.gz
home/www/backup/mysql-15-Apr-2014/voxl.gz
home/www/backup/mysql-15-Apr-2014/pdalady.gz
home/www/backup/mysql-15-Apr-2014/minecraft36

@Demontager
Copy link
Author

root@debian:~/scripts# find /home/www/backup/mysql-15-Apr-2014 -type f ( ! -iname ".sh" -a ! -iname ".sql" -a ! -iname "*.txt" ) -print|awk -F / '{print $NF}'|awk -F . '{print $1}'
escswim
sauvig
amitysource
ennixtalks
voxl
pdalady
minecraft365
ultratouch
printcounter
piyazilim
forexinvest
bestwestern
belcity

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