#! /Bin/bash # This is a shellscript for auto DB Backup # Powered by aspbiz #2004-09 # Setting # Set the database name, database login name, password, backup path, Log Path, data file location, and backup mode # By default, the backup method is tar, mysqldump, or mysqldotcopy. # By default, log on to the MySQL database with root (empty) and 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 end Newfile = "$ 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 success! ">>> $ Logfile Else Echo "[$ oldfile] No old backup file! ">>> $ Logfile Fi If [-F $ newfile] Then Echo "[$ newfile] the backup file is exists, can't backup! ">>> $ Logfile Else Case $ 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 "-----------------------------------------"> $ logfile |