This script was found online in and used in the official system after modification. The database name was changed to LinuxIDC. com1. first, create the sh file and create it in usrbin.
This script was found online in and used in the official system after modification. The database name was changed to LinuxIDC. com1. first, create the sh file and create it in/usr/bin /.
This script was found online in and used in the official system after modification. The database name was changed to LinuxIDC.com.
1. Create a sh file and create a script file named mysqlscript. sh in/usr/bin/. For details, see mysqlscript. sh.
2. Open # crontab-e in the root directory and enter 12 12 ***/usr/bin/mysqlscript. sh
3. Finally, if you find that the script cannot be executed on a regular basis, the possible cause is that the permission of the mysqlscript. sh script file is incorrect, and then changed to the executable file # chmod 777 mysqlscript. sh;
4. It should be okay after the above work is done.
The script content of mysqlscript. sh is as follows:
#! /Bin/bash
# This is a ShellScript For Auto DB Backup
# Powered by aspbiz
#2007-11-25
# 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
# Database Name
DBName = LinuxIDC.com
# Database username
DBUser = root
# Password
DBPasswd =! Qazdr %
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