领导说每天都需要备份一下mysql,好吧,我总不能天天手动去弄,所以我自己写了一份shell脚本用来自动备份,crontab还是很好的呵呵
1
#
! /bin/bash
2 #define mysql path
3 MYSQL_BIN_PATH =/ local / akazam / servers / akazamdb51 / bin # 这个是你MYSQL的安装目录
4 MYSQL_BACKUP_PATH =/ local / akazam / mysqlbak # 你要备份的目录
5 SOCKET_FILE =/ tmp / mysql . sock
6
7 if [ $ # -eq 0 ]
8 then
9 echo " ERROR:Usage:please input mysql arguments " 1 >& 2
10 echo " The arguments like this " 1 >& 2
11 echo " ./mysqlbackup.sh database [user] [password] [port] [bakpath] " 1 >& 2
12 echo " default user is akazam ,default password is dbacc355 and " 1 >& 2
13 echo " default port is 3306,if you all understand ,you can try now! " 1 >& 2
14 exit 1
15 fi
16
17 # get datebase
18 db = $ 1
19 # get user
20 if [ " $2 " = "" ]
21 then
22 user = " akazam "
23 else
24 user = $ 2
25 fi
26 # get password
27 if [ " $3 " = "" ]
28 then
29 password = " dbacc355 "
30 else
31 password = $ 3
32 fi
33 # get port
34 if [ " $4 " = "" ]
35 then
36 port = " 3306 "
37 else
38 port = $ 4
39 fi
40 # get backpath
41 if [ " $5 " = "" ]
42 then
43 backpath = $MYSQL_BACKUP_PATH
44 else
45 backpath = $ 5
46 fi
47 # get the day before yesterday
48 byd = $(date -- date = ' 1 days ago ' " +%Y%m%d " )
49 # get today
50 today = $(date + %Y%m%d )
51 # backup today's mysqldump
52 if [ ! - d $MYSQL_BACKUP_PATH ]
53 then
54 mkdir $MYSQL_BACKUP_PATH
55 fi
56 cd $MYSQL_BIN_PATH
57 ./ mysqldump - u $user - p $password - h localhost $db -- socket = $SOCKET_FILE -- opt > $backpath / $db$today
58 echo " backup result is:$bakresult "
59 # delete the day before's backup
60 rm - rf $backpath / $db$byd
61
62 echo " you delete backup mydqldump is $db$byd and need backup mysqldump is $db$today "
63
64
用法嘛 按照./mysqlbackup.sh database [user] [password] [port] [bakpath] 这种方式就可以了,还是很方便的
2 #define mysql path
3 MYSQL_BIN_PATH =/ local / akazam / servers / akazamdb51 / bin # 这个是你MYSQL的安装目录
4 MYSQL_BACKUP_PATH =/ local / akazam / mysqlbak # 你要备份的目录
5 SOCKET_FILE =/ tmp / mysql . sock
6
7 if [ $ # -eq 0 ]
8 then
9 echo " ERROR:Usage:please input mysql arguments " 1 >& 2
10 echo " The arguments like this " 1 >& 2
11 echo " ./mysqlbackup.sh database [user] [password] [port] [bakpath] " 1 >& 2
12 echo " default user is akazam ,default password is dbacc355 and " 1 >& 2
13 echo " default port is 3306,if you all understand ,you can try now! " 1 >& 2
14 exit 1
15 fi
16
17 # get datebase
18 db = $ 1
19 # get user
20 if [ " $2 " = "" ]
21 then
22 user = " akazam "
23 else
24 user = $ 2
25 fi
26 # get password
27 if [ " $3 " = "" ]
28 then
29 password = " dbacc355 "
30 else
31 password = $ 3
32 fi
33 # get port
34 if [ " $4 " = "" ]
35 then
36 port = " 3306 "
37 else
38 port = $ 4
39 fi
40 # get backpath
41 if [ " $5 " = "" ]
42 then
43 backpath = $MYSQL_BACKUP_PATH
44 else
45 backpath = $ 5
46 fi
47 # get the day before yesterday
48 byd = $(date -- date = ' 1 days ago ' " +%Y%m%d " )
49 # get today
50 today = $(date + %Y%m%d )
51 # backup today's mysqldump
52 if [ ! - d $MYSQL_BACKUP_PATH ]
53 then
54 mkdir $MYSQL_BACKUP_PATH
55 fi
56 cd $MYSQL_BIN_PATH
57 ./ mysqldump - u $user - p $password - h localhost $db -- socket = $SOCKET_FILE -- opt > $backpath / $db$today
58 echo " backup result is:$bakresult "
59 # delete the day before's backup
60 rm - rf $backpath / $db$byd
61
62 echo " you delete backup mydqldump is $db$byd and need backup mysqldump is $db$today "
63
64