Last active
August 29, 2015 14:21
-
-
Save bactisme/0e24cdfadf508a416896 to your computer and use it in GitHub Desktop.
restore-from-folder.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# DEPLOYED WITH ANSIBLE | |
DESTFOLDER="/root/backups" | |
MAIL="b@example.org" | |
DIST_SERVER="backup-user@backup.server.com" | |
DIST_FOLDER="/home/backup-user/" | |
LOGFILE=$$.log | |
echo "" > $LOGFILE | |
DATABASES=`mysql --skip-column-names -e 'show databases' | grep -v '_schema$\|phpmyadmin\|mysql\|_dev$\|_test$'` | |
for db_name in $DATABASES; do | |
echo "--> $db_name" | |
echo " --- $db_name --- " >> $LOGFILE | |
TABLES=`mysql $db_name --skip-column-names -e 'show tables;'` | |
begin=$(date +"%s") | |
counter=0 | |
for table_name in $TABLES; do | |
#echo $table_name | |
if [ ! -d "$DESTFOLDER/$db_name" ]; then | |
mkdir -p $DESTFOLDER/$db_name | |
fi | |
FILENAME="$DESTFOLDER/$db_name/$table_name.sql" | |
mysqldump $db_name "$table_name" > $FILENAME 2>> $LOGFILE | |
echo -n "." | |
let counter+=1 | |
done | |
termin=$(date +"%s") | |
difftimelps=$(($termin-$begin)) | |
echo "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds " | |
echo " > $counter tables" | |
echo " > $counter tables" >> $LOGFILE | |
echo "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds " >> $LOGFILE | |
done | |
HOSTNAME=`hostname` | |
rsync -avz $DESTFOLDER $DIST_SEVER:$DIST_FOLDER$HOSTNAME >> $LOGFILE | |
cat $LOGFILE | mail -s "$HOSTNAME backup sync" $MYMAIL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# DEPLOYED WITH ANSIBLE | |
SRCFOLDER="/root/backups" | |
for d in $SRCFOLDER/*; do | |
if [ -d "${d}" ]; then | |
DBNAME=$(basename "$d") | |
echo "--> $DBNAME" | |
mysql -e "CREATE DATABASE IF NOT EXISTS $DBNAME" | |
begin=$(date +"%s") | |
for table_name in $SRCFOLDER/$DBNAME/*; do | |
FILENAME="$SRCFOLDER/$DBNAME/$(basename "$table_name")" | |
#echo $FILENAME; | |
mysql $DBNAME < $FILENAME | |
echo -n "." | |
done | |
termin=$(date +"%s") | |
difftimelps=$(($termin-$begin)) | |
echo "$(($difftimelps / 60)) min $(($difftimelps % 60)) " | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment