## mkdir /etc/myscript/logs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/bash backpath='/home/backup/db/' backuptime='10' fmail='saza@fromserver.com' email='saza@toserver.com' replog='/etc/myscript/logs/dbbackup.log' dbinfo='/root/.my' hostname='localhost' tmpmsg='/tmp/msg' dbuser='root' |
1 2 3 |
#/etc/myscript/backupdb.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#!/bin/bash #/etc/myscript/backupdb.sh dbconf='/etc/myscript/dbconf.ini' IFS=' ' alertmail() { msg="${msg} ::\n `cat ${tmpmsg}| sed 's/$/\\\n/g'`" # msg="${msg} : `cat ${tmpmsg}`" echo -e $msg | mail -s "Mysql Topvalue Backup" -r $fmail $email } exp_privilege() { #mysql -h {host_name} -u {user_name} -p{password} -Ne "select distinct concat( #\"SHOW GRANTS FOR '\",user,\"'@'\",host,\"';\" ) from user;" mysql | mysql -h {host_name} #-u {user_name} -p{password} | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;} msg='Export Privilege' comm="mysql -h ${hostname} ${dbuser} ${dbpass} -Ne \"select distinct concat(\ \\\"show grants for '\\\",user,\\\"'@'\\\",host,\\\"';\\\" ) from user;\" mysql |\ mysql -h ${hostname} ${dbuser} ${dbpass} |\ sed 's/\\(GRANT .*\\)/\\1;/;s/^\\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'" eval $comm > ${curpath}/privilege.sql 2> ${tmpmsg} chk_error #echo ${comm} } chk_error() { if [ -s ${tmpmsg} ]; then alertmail reportlog exit 1 fi } clrbackup(){ if [ ! -s ${tmpmsg} ]; then if [ -d ${oldpath} ]; then rm -rf $oldpath fi fi } reportlog(){ # msg = msg + {tmpmsg} echo "####: `date +%Y.%m.%d-%H:%M:%S` :###" >> $replog echo -e $msg >> $replog } dbdump() { dbname=$(mysql -h ${hostname} ${dbuser} ${dbpass} -se "show databases" 2>${tmpmsg}) msg="Connection/Authen `date`" chk_error msg="" for x in $dbname do comm="/usr/bin/mysqldump --single-transaction -h ${hostname} ${dbuser} ${dbpass} ${x} | gzip > ${curpath}/${x}.sql.gz" eval $comm 2> ${tmpmsg} if [ -s ${tmpmsg} ]; then msg="${x} ## `cat ${tmpmsg}`\n${msg}" fi done echo -n -e $msg > ${tmpmsg} msg="" chk_error } #___ MAIN ___ if [ -e ${dbconf} ]; then . ${dbconf} #___initialize cat /dev/null > ${tmpmsg} curpath="${backpath}/`date +%Y.%m.%d`" oldpath="${backpath}/`date +%Y.%m.%d --date=\"${backuptime} days ago\"`" mkdir -p ${curpath} #echo ${curpath} #echo ${oldpath} else echo "${dbconf} Not Found!!" exit 1 fi if [ -e ${dbinfo} ]; then dbpass=`cat ${dbinfo}| grep -v '^#'` fi if [ -z "${dbpass}" ]; then dbuser='' else dbpass="-p${dbpass}" dbuser="-u ${dbuser}" fi #__//Call do it exp_privilege dbdump clrbackup # !! Success backup if [ ! -s ${tmpmsg} ]; then msg="Backup Success - `date +%Y.%m.%d-%H:%M:%S`" alertmail reportlog fi |