這篇文章主要介紹“l(fā)inux下實(shí)現(xiàn)mysql自動備份的腳本”,在日常操作中,相信很多人在linux下實(shí)現(xiàn)mysql自動備份的腳本問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”linux下實(shí)現(xiàn)mysql自動備份的腳本”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營銷策劃、成都做網(wǎng)站、網(wǎng)站制作、微信公眾號開發(fā)、成都微信小程序、HTML5、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、成都營銷網(wǎng)站建設(shè)資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。腳本放在 /home/user/_backup.sh
crontab
# crontab -l
# m h dom mon dow command
28 16 * * * /home/user/mysql_backup.sh
腳本如下
#!/bin/sh # mysql_backup.sh: backup mysql databases and keep newest 5 days backup. # # Last updated: 20 March 2006 # ---------------------------------------------------------------------- # This is a free shell script under GNU GPL version 2.0 or above # Copyright (C) 2006 Sam Tang # Feedback/comment/suggestions : http://www.real-blog.com/ # ---------------------------------------------------------------------- # your mysql login information # db_user is mysql username # db_passwd is mysql password # db_host is mysql host # ----------------------------- db_user="root" db_passwd="password" db_host="localhost" # the directory for story your backup file. backup_dir="/home/mybackup" # date format for backup file (dd-mm-yyyy) time="$(date +"%d-%m-%Y")" # mysql, mysqldump and some other bins path MYSQL="/usr/local/mysql/bin/mysql" MYSQLDUMP="/usr/local/mysql/bin/mysqldump" MKDIR="/bin/mkdir" RM="/bin/rm" MV="/bin/mv" GZIP="/bin/gzip" # check the directory for store backup is writeable test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0 # the directory for story the newest backup test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/" # get all databases all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse show databases)" for db in $all_db do $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz" done # delete the oldest backup test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5" # rotate backup directory for int in 4 3 2 1 0 do if(test -d "$backup_dir"/backup."$int") then next_int=`expr $int + 1` $MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int" fi done exit 0;
備注:
mysql是以mysql用戶身份運(yùn)行的,對/home /mybackup不可寫也會失敗
chmod 777 /home/mybackup問題解決了
到此,關(guān)于“l(fā)inux下實(shí)現(xiàn)mysql自動備份的腳本”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
新聞名稱:linux下實(shí)現(xiàn)mysql自動備份的腳本-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article32/gcesc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、動態(tài)網(wǎng)站、微信小程序、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)