backup mysql

Ai dorit uneori sa faci backup la toate bazele de date? Urmatorul script te va ajuta foarte mult daca ai acces ssh pe server.
Tot ce trebuie as faci e sa treci numele bazelor de date, datele de conectare la mysql si calea unde doresti backupul.
Scriptul va face dump la fiecare baza de date in parte si le va arhiva in format tbz. Acest format este foarte puternic si poate face ca un fisier de sql de 1.5 GB sa ajunga la 150Mb.
P.S: utilizatorul de mysql trebuie sa aiba acces la toate bazele de date.

#!/bin/bash

#####
# Set these values!
####

# space separated list of domain names (will be used as part of the output path)
domains=(  )
#list of corresponding DB names
sqldbs=( baza1 baza2 baza3  )

#Directory to save generated sql files (domain name is appended)
opath=/backup/mysql/

# your mysql host
mysqlhost=localhost

#username for host
username=root

#corresponding password
password=pass
#####
# End of config values
#####

#date to append
suffix=$(date +%m-%d-%Y)

#run on each domain
for (( i = 0 ; i < ${#sqldbs[@]} ; i++ )) do 	#set current output path 	cpath=$opath${domains[$i]} 	 	#check if we need to make path 	if [ -d $cpath ] 	then 		# direcotry exists, we're good to continue 		filler="just some action to prevent syntax error" 	else 		#we need to make the directory 		echo Creating $cpath 		mkdir -p $cpath 	fi 	#now do the backup 	mysqldump -c -h $mysqlhost --user $username --password=$password ${sqldbs[$i]} > ${cpath}/${sqldbs[$i]}_$suffix.sql
	tar -cjvf ${cpath}/${sqldbs[$i]}_$suffix.tbz ${cpath}/${sqldbs[$i]}_$suffix.sql
	rm ${cpath}/${sqldbs[$i]}_$suffix.sql
done

3 comentarii

  1. nu! asta era si ideea! face pentru fiecare baza de date cate un fisier.
    daca se buseste doar o baza de date? Le repui pe toate?

Spune si tu parerea...