A mysql automatic backup script

xiaoxiao2021-03-06  72

You can put this script into crontab, executed once every morning, automatic backup This script is only available once a day, and only keeps backups of the backup of the last five days on the server.

Code: #! / Bin / bash #this is a shellscript for auto db backup #powered by Aspbiz # 2004-09 #setting # Settings database name, database login name, password, backup path, log path, data file location, and backup Method # By default, the backup method is TAR, but also mysqldump, mysqldotcopy # By default, log in with the mysql database with root (empty), back up to / ROOT/DBXXXXX.TGZ DBNAME = mysql dbuser = root dbpasswd = backuppath = / root / Logfile = / root / db.log dbpath = / var / lib / mysql / # backupmethod = mysqldump # backupMethod = mysqlhotcopy # backupMethod = tar #setting endnule = "$ backuppath" DB $ (DATE % Y% M% D ) .tgz dumpfile = "$ backuppath" DB $ (DATE % Y% M% D) Oldfile = "$ backuppath" DB $ (DATE % Y% M% D --Date = '5 days ago'). TGZ echo "-----------------------------------------"> $ logfile Echo $ (DATE "% Y-% M-% D% H:% M:% S") >> $ logfile echo "-------------------- ------ ">> $ logfile # delete old file if [-f $ oldfile] THEN RM -F $ Oldfile >> $ logfile 2> & 1 echo" [$ oldfile] delete old file surcess! ">> Logfile Else Echo "[$ lyled] no old backup file!" >> $ logfile fi f [-f $ newfile] thrho "[$ newfile] The Backup File is exists, can't backup!" >> $ logfile elsecase $ BackupMethod in mysqldump) if [-z $ DBPasswd] then mysqldump -u $ DBUser --opt $ DBName> $ DumpFile else mysqldump -u $ DBUser -p $ DBPasswd --opt $ DBName> $ DumpFile fi tar czvf $ NewFile $ Dumpfile >> $ logfile 2> & 1 echo "[$ newfile] backup success! >> $ logfile rm -rf $ dumpfile ;;

mysqlhotcopy) rm -rf $ DumpFile mkdir $ DumpFile if [-z $ DBPasswd] then mysqlhotcopy -u $ DBUser $ DBName $ DumpFile >> $ LogFile 2> & 1 else mysqlhotcopy -u $ DBUser -p $ DBPasswd $ DBName $ DumpFile >> $ Logfile 2> & 1 FI TAR CZVF $ NEWFILE $ DUMPFILE >> $ logfile 2> & 1 echo "[$ newfile] backup success!" >> $ logfile rm -rf $ dumpfile ;; *) /etc/init.d/mysqld STOP> / dev / null 2> & 1 TAR CZVF $ newfile $ dbpath $ dbname >> $ logfile 2> & 1 /etc/init.d/mysqld start> / dev / null 2> & 1 echo "[$ newfile] Backup Success! ">> $ logfile; esac fi echo" --------------------------------------- ---- ">> $ logfileezerg Programming

转载请注明原文地址:https://www.9cbs.com/read-105043.html

New Post(0)