{"id":1392,"date":"2024-05-08T10:06:48","date_gmt":"2024-05-08T02:06:48","guid":{"rendered":"http:\/\/oneai.eu.org\/?p=1392"},"modified":"2024-05-08T10:06:48","modified_gmt":"2024-05-08T02:06:48","slug":"mysql-%e5%a4%87%e4%bb%bd%e4%b8%8e%e6%81%a2%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/oneai.eu.org\/?p=1392","title":{"rendered":"mysql \u5907\u4efd\u4e0e\u6062\u590d"},"content":{"rendered":"<p>`\u5907\u4efd\u811a\u672c\uff1a<br \/>\n-rwxr-xr-x 1 root root   57 2015-05-18 17:27 databaselist.txt<br \/>\nYFCS-4-DBWAS:~\/databackup\/script # more backmysqldatabase.sh <\/p>\n<h1>!\/bin\/bash<\/h1>\n<p>####################################################<\/p>\n<h2>author:newhackerman<\/h2>\n<h2>\u4f7f\u7528\u8bf4\u660e\uff1a<\/h2>\n<h2>\u9996\u5148\u5728\u5907\u4efd\u811a\u672c\u6240\u5728\u8def\u5f84\u4e0b\u65b0\u5efa\u6587\u4ef6databaselist.txt<\/h2>\n<h2>\u5e76\u628a\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u5199\u5165\u5176\u4e2d\uff0c\u4e00\u884c\u4e00\u4e2a\u6570\u636e\u5e93\u540d\u79f0<\/h2>\n<h2>\u6267\u884c\uff1a sh backmysqldatabase.sh   &amp;\u8fdb\u884c\u5168\u5e93\u5907\u4efd<\/h2>\n<h2>\u53ef\u4f20\u5165\u53c2\u6570 \u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93\u540d\u79f0\uff0c\u5907\u4efd\u8def\u5f84\uff0c\u4e3b\u673a\uff0c\u7aef\u53e3<\/h2>\n<h2>usage:backmysqldatabase.sh backup test zentao \/root\/databackup localhost 3306&amp;<\/h2>\n<h2>\u4f20\u53c2\u8fdb\u884c\u5355\u5e93\u5907\u4efd\uff0c\u672a\u4f20\u53c2\u5219\u5168\u5e93\u5907\u4efd<\/h2>\n<p>####################################################<\/p>\n<p>#############default parameter define##################<br \/>\nusername=&quot;backup&quot;<br \/>\npassword=&quot;test&quot;<br \/>\ndatabasename=&quot;zentao&quot;<br \/>\nhost=&quot;localhost&quot;<br \/>\nport=&quot;3306&quot;<br \/>\nbackfiledir=&quot;\/root\/databackup&quot;<br \/>\nTdate=<code>date &quot;+%Y-%m-%d-%H&quot;<\/code><br \/>\nTtime=<code>MARKDOWN_HASHcce46cdaa704d445e6fd494cfb8782deMARKDOWN<em>HASH<\/code><br \/>\nbackfiledirtmp=&quot;\/root\/databackup\/tmp&quot;<br \/>\nscriptdir=&quot;\/root\/databackup\/script&quot;<br \/>\ndatabaselist=&quot;${scriptdir}\/databaselist.txt&quot;<br \/>\nlogfile=&quot;${backfiledirtmp}\/back<\/em>${Tdate}-${Ttime}.log&quot;<br \/>\nbackfilename=&quot;$databasename$<em>back<\/em>$Tdate.sql&quot;<br \/>\nrundumppath=\/usr\/local\/mysql\/bin<br \/>\n#############default parameter define end##############<\/p>\n<h1>mysqldump -u${username} -p{password} --databases zentao -h localhost -P3306 --opt --single-transaction --skip-lock-tables &gt;\/root\/databackup\/zentao_back_20150203.sql<\/h1>\n<p>#############receive parameter ########################<br \/>\n#############receive parameter ########################<\/p>\n<p>if [ $# -eq 1 ] ;then<br \/>\nusername=$1<br \/>\necho &quot;$1&quot;<br \/>\nelif [ $# -eq 2 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\necho &quot;$1----$2&quot;<br \/>\nelif [ $# -eq 3 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nelif [ $# -eq 4 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackfiledir=$4<br \/>\nelif [ $# -eq 5 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackfiledir=$4<br \/>\nhost=$5<br \/>\nelif [ $# -eq 6 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackfiledir=$4<br \/>\nhost=$5<br \/>\nport=$6<br \/>\nelif [ $# -gt 6 ] ;then<br \/>\necho &quot;input parameters error please check it &quot; &gt;&gt;${logfile}<br \/>\nelse<br \/>\necho &quot;not input parameter use all default parameters !&quot; &gt;&gt;${logfile}<br \/>\nfi<\/p>\n<p>#############receive parameter end#####################<\/p>\n<p>test ! -d ${backfiledirtmp} &amp;&amp; mkdir -p ${backfiledirtmp}<br \/>\ntest ! -d ${backfiledir} &amp;&amp; mkdir -p ${backfiledir}<br \/>\ncd ${backfiledirtmp}<\/p>\n<p>############Backup and compression function############<br \/>\nfunction BackDatabase(){<\/p>\n<h1>cd ${backfiledir}<\/h1>\n<pre><code>                            logfile=\"${backfiledirtmp}\/${databasename}_back_${Tdate}-${Ttime}.log\"\n            echo <code>date &quot;+%Y-%m-%d %X&quot;<\/code> >>${logfile}\n            echo \"begin backup $databasename\" >>${logfile}\n            $rundumppath\/mysqldump -u${username} -p${password} -h${host} -P${port} --databases $databasename --opt --default-character-set=utf8 --single-transaction --skip-lock-tables >\"$backfiledirtmp\/${databasename}_back_${Tdate}.sql\"\n\n    if [[ -f \"${backfiledirtmp}\/${databasename}_back_${Tdate}.sql\" ]] && [[ -s \"${backfiledirtmp}\/${databasename}_back_${Tdate}.sql\" ]]\n    then\n    echo <code>date &quot;+%Y-%m-%d %X&quot;<\/code> >>${logfile}\n    echo \" backup $databasename sucess!\" >>${logfile}\n    #############bengin to compress   #####################\n    if [ -f \"${backfiledirtmp}\/${databasename}_back_${Tdate}.sql\" ]\n    then\n    gzip \"${backfiledirtmp}\/${databasename}_back_${Tdate}.sql\"\n                    if [ -f \"${backfiledirtmp}\/${databasename}_back_${Tdate}.sql.gz\" ]\n                    then\n                    echo \" compress $databasename sucess!\" >>${logfile}\n                    else\n                    echo \" compress $databasename fail !\" >>${logfile}\n                    fi\n    fi\n    #############compress end         #####################\n    else\n    echo \" backup ${databasename} fail!\" >>${logfile}\n    #mail -s \"data backup fail\" zoujiayong@revenco.com <error.log  >>${logfile}\n    #mail -s \"data backup fail\" >>${logfile}\n    fi\n    #sleep 1\n    }<\/code><\/pre>\n<p>############Backup and compression function  end #########<br \/>\n############run back start                       #########<br \/>\nif [ $# -eq 0 ] ;then<\/p>\n<p>if [ ! -f &quot;${databaselist}&quot; ]<br \/>\nthen<br \/>\necho &quot; ${databaselist} file not exsit!&quot; &gt;&gt;${logfile}<br \/>\nexit<br \/>\nfi<br \/>\ncat $databaselist|while read databasename         #\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u6570\u636e\u5e93\u8fdb\u884c\u5907\u4efd<br \/>\ndo<br \/>\nlogfile=&quot;${backfiledirtmp}\/${databasename}<em>back<\/em>${Tdate}-${Ttime}.log&quot;<br \/>\nBackDatabase;                  #\u5faa\u73af\u8fdb\u884c\u5907\u4efd<br \/>\ndone<br \/>\nelif [ $# -lt 7 ] ;then<br \/>\nBackDatabase<br \/>\nelse<br \/>\necho &quot;input parameter is error please check it!&quot; &gt;&gt;${logfile}<br \/>\nfi<br \/>\n############run back end                        #########<\/p>\n<p>#############ftp backdatefiles to backhost       ##########<br \/>\nfunction CopyZfileToDhost(){<br \/>\nftpuser=zjy<br \/>\nftppass=zjy<br \/>\nftphost=&quot;10.200.5.201&quot;<br \/>\nftpDdir=&quot;\/home\/zjy\/jmeter&quot;<\/p>\n<h1>ftpDdir=&quot;\/backup\/atp_qc_projectbak&quot;<\/h1>\n<pre><code>            #backfiledirtmp=\"\/root\/databackup\"\n            cd ${backfiledirtmp}\n            #sftp root@10.200.25.151 <<EOF\n            sftp zjy@10.200.5.201 <<EOF\n            #open ${ftphost} \n            #user ${ftpuser} ${ftppass}\n            cd ${ftpDdir}\n            mput *.gz\n            bye<\/code><\/pre>\n<p>EOF<br \/>\n}<br \/>\nCopyZfileToDhost;<br \/>\n#############ftp end                             ##########<\/p>\n<p>#############move backfile to backdir            ##########<br \/>\ncd ${backfiledirtmp};<br \/>\nmv ${backfiledirtmp}\/*.gz ${backfiledir}\/<br \/>\n#############move end                            ##########<\/p>\n<p>#############purge      backdatefiles 8 days ago##########<br \/>\ncdate=8<br \/>\nfind ${backfiledir} -mtime +${cdate} -name &quot;<em>.gz&quot; &gt;&gt;&quot;${logfile}&quot;;<br \/>\nfind ${backfiledir} -mtime +${cdate} -name &quot;<\/em>.gz&quot; -exec rm -rf {} \\;<br \/>\necho &quot; delete ${cdate} backdatafile sucess!&quot; &gt;&gt;&quot;${logfile}&quot;<br \/>\n#############purge end            #####################<\/p>\n<h1>20 1 <em> <\/em> 1,2,3,4,5,6 sh \/root\/databackup\/script\/backup_alldatabases.sh backup test zentao \/root\/databackup &amp;<\/h1>\n<hr \/>\n<h1>\u6062\u590d\u811a\u672c\uff1a<\/h1>\n<p>YFCS-4-DBWAS:~\/databackup\/script # more restore_mysql.sh <\/p>\n<h1>!\/bin\/bash<\/h1>\n<p>####################################################<\/p>\n<h2>author:zoujiayong<\/h2>\n<h2>\u4f7f\u7528\u8bf4\u660e\uff1a<\/h2>\n<h2>\u6267\u884c\uff1a sh restore_mysql.sh  &amp;\u8fdb\u884c\u6062\u590d<\/h2>\n<h2>\u53ef\u4f20\u5165\u53c2\u6570 \u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93\u540d\u79f0\uff0c\u5907\u4efd\u8def\u5f84\u53ca\u6587\u4ef6<\/h2>\n<h2>usage:restore_mysql.sh backup test zentao \/root\/databackup\/zentao_back_2015-02-04-10.sql.gz<\/h2>\n<p>####################################################<\/p>\n<p>#############default parameter define##################<br \/>\nusername=backup<br \/>\npassword=test<br \/>\ndatabasename=&quot;mysql&quot;<br \/>\nhost=localhost<br \/>\nport=3306<br \/>\nTdate=<code>date &quot;+%Y-%m-%d-%H&quot;<\/code><br \/>\nTtime=<code>date +%M-%S<\/code><br \/>\nbackdata=&quot;\/root\/databackup\/zentao_back_2015-02-04-10.sql.gz&quot;<br \/>\nbackfiledir=<code>echo ${backdata%\/*}<\/code>            #\u622a\u53d6\u8def\u5f84<br \/>\nbackfilename=<code>echo ${backdata##*\/}<\/code>                              #\u622a\u53d6\u540d\u79f0<br \/>\nbackfiledirtmp=&quot;\/root\/databackup\/tmp&quot;<br \/>\nscriptdir=<code>MARKDOWN_HASH9003d1df22eb4d3820015070385194c8MARKDOWN<em>HASH<\/code><br \/>\ndatabaselist=&quot;${scriptdir}\/databaselist.txt&quot;<br \/>\nlogfile=&quot;${backfiledirtmp}\/restore<\/em>${Tdate}-${Ttime}.log&quot;<\/p>\n<h1>backfilename=$databasename$<em>back<\/em>$Tdate.sql<\/h1>\n<p>#############default parameter define end##############<\/p>\n<p>#############receive parameter ########################<\/p>\n<p>if [ $# -eq 1 ] ;then<br \/>\nusername=$1<br \/>\necho &quot;$1&quot;<br \/>\nelif [ $# -eq 2 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\necho &quot;$1----$2&quot;<br \/>\nelif [ $# -eq 3 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nelif [ $# -eq 4 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackdata=$4<br \/>\nelif [ $# -eq 5 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackdata=$4<br \/>\nhost=$5<br \/>\nelif [ $# -eq 6 ] ;then<br \/>\nusername=$1<br \/>\npassword=$2<br \/>\ndatabasename=$3<br \/>\nbackdata=$4<br \/>\nhost=$5<br \/>\nport=$6<br \/>\nelif [ $# -gt 6 ] ;then<br \/>\necho &quot;input parameters error please check it &quot; &gt;&gt;${logfile}<br \/>\nelse<br \/>\necho &quot;not input parameter use all default parameters !&quot; &gt;&gt;${logfile}<br \/>\nfi<\/p>\n<p>#############receive parameter end#####################<\/p>\n<p>backfiledir=<code>echo ${backdata%\/*}<\/code>            #\u622a\u53d6\u8def\u5f84<br \/>\nbackfilename=<code>echo ${backdata##*\/}<\/code>                              #\u622a\u53d6\u540d\u79f0<\/p>\n<p>if [ ! -d &quot;${backfiledir}&quot; ]<br \/>\nthen<br \/>\necho &quot; ${backfiledir} dir not exsit!&quot; &gt;&gt;${backfiledirtmp}\/${databasename}<em>restore<\/em>${Tdate}-${Ttime}.log<br \/>\nfi<br \/>\n#############restore start #####################<\/p>\n<p>cd ${backfiledir}<\/p>\n<p>if [ ! -f &quot;${backfilename}&quot; ]<br \/>\nthen<br \/>\necho &quot; ${backfilename} file not exsit!&quot; &gt;&gt;${backfiledirtmp}\/${databasename}<em>restore<\/em>${Tdate}-${Ttime}.log<br \/>\nexit<br \/>\nelse<br \/>\ncp ${backfiledir}\/${backfilename} ${backfiledirtmp}\/<br \/>\ncd &quot;${backfiledirtmp}\/&quot;<br \/>\ngunzip &quot;${backfilename}&quot;<br \/>\nunzipfile=<code>echo ${backfilename%.*}<\/code>  #\u83b7\u53d6\u89e3\u538b\u7f29\u540e\u7684\u6587\u4ef6\u540d<br \/>\nfi<br \/>\necho <code>date &quot;+%Y-%m-%d %X&quot;<\/code> &gt;&gt;${backfiledirtmp}\/${databasename}<em>restore<\/em>${Tdate}-${Ttime}.log<\/p>\n<p>mysql -u${username} -p${password} -h${host} -P${port} -D $databasename --default-character-set=utf8 &lt;&quot;${unzipfile}&quot;<br \/>\nrm &quot;${unzipfile}&quot;<br \/>\necho <code>date &quot;+%Y-%m-%d %X&quot;<\/code> &gt;&gt;${backfiledirtmp}\/${databasename}<em>restore<\/em>${Tdate}-${Ttime}.log<br \/>\necho &quot; restore sucess&quot; &gt;&gt;${backfiledirtmp}\/${databasename}<em>restore<\/em>${Tdate}-${Ttime}.log<\/p>\n<p>#############restore end#####################<\/p>\n<p>YFCS-4-DBWAS:~\/databackup\/script #  `<\/p>\n","protected":false},"excerpt":{"rendered":"<p>`\u5907\u4efd\u811a\u672c\uff1a -rwxr-xr-x 1 root root 57 2015-05-18 17:27 databaselist.txt YFCS-4-DBWAS:~\/databackup\/script # more backmysqldatabase.sh !\/bin\/bash #################################################### author:newhackerman \u4f7f\u7528\u8bf4\u660e\uff1a \u9996\u5148\u5728\u5907\u4efd\u811a\u672c\u6240\u5728\u8def\u5f84\u4e0b\u65b0\u5efa\u6587\u4ef6databaselist.txt \u5e76\u628a\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u5199\u5165\u5176\u4e2d\uff0c\u4e00\u884c\u4e00\u4e2a\u6570\u636e\u5e93\u540d\u79f0 \u6267\u884c\uff1a sh backmysqldatabase.sh &amp;\u8fdb\u884c\u5168\u5e93\u5907\u4efd \u53ef\u4f20\u5165\u53c2\u6570 \u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93\u540d\u79f0\uff0c\u5907\u4efd\u8def\u5f84\uff0c\u4e3b\u673a\uff0c\u7aef\u53e3 usage:backmysqldatabase.sh backup test zentao \/root\/databackup localhost 3306&amp; \u4f20\u53c2\u8fdb\u884c\u5355\u5e93\u5907\u4efd\uff0c\u672a\u4f20\u53c2\u5219\u5168\u5e93\u5907\u4efd #################################################### #############default parameter define################## username=&quot;backup&quot; password=&quot;test&quot; databasename=&quot;zentao&quot; host=&quot;localhost&quot; port=&quot;3306&quot; backfiledir=&quot;\/root\/databackup&quot; Tdate=date &quot;+%Y-%m-%d-%H&quot; Ttime=MARKDOWN_HASHcce46cdaa704d445e6fd494cfb8782deMARKDOWNHASH backfiledirtmp=&quot;\/root\/databackup\/tmp&quot; scriptdir=&quot;\/root\/databackup\/script&quot; databaselist=&quot;${scriptdir}\/databaselist.txt&quot; logfile=&quot;${backfiledirtmp}\/back${Tdate}-${Ttime}.log&quot; backfilename=&quot;$databasename$back$Tdate.sql&quot; rundumppath=\/usr\/local\/mysql\/bin #############default parameter define end############## mysqldump -u${username} -p{password} &#8211;databases zentao -h localhost -P3306 &#8211;opt &#8211;single-transaction &#8211;skip-lock-tables &gt;\/root\/databackup\/zentao_back_20150203.sql #############receive parameter ######################## #############receive parameter ######################## if [ $# -eq 1 ] ;then username=$1 echo &quot;$1&quot; elif [ $# -eq 2 ] ;then username=$1 password=$2 echo &quot;$1&#8212;-$2&quot; elif [ $# -eq 3 ] ;then username=$1 password=$2 databasename=$3 elif [ $# -eq 4 ] ;then username=$1 password=$2 databasename=$3 backfiledir=$4 elif [ $# -eq 5 ] ;then username=$1 password=$2 databasename=$3 backfiledir=$4 host=$5 elif [ $# -eq 6 ] ;then username=$1 password=$2 databasename=$3 backfiledir=$4 host=$5 port=$6 elif [ $# -gt 6 ] ;then echo &quot;input parameters error please check it &quot;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[83],"tags":[],"class_list":["post-1392","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1392"}],"version-history":[{"count":1,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1392\/revisions"}],"predecessor-version":[{"id":1393,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1392\/revisions\/1393"}],"wp:attachment":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}