使用Shell指令碼實現自動備份MySQL資料庫

2021-10-06 17:22:25 字數 3823 閱讀 1373

目的:

192.168.100.20伺服器對192.168.100.10伺服器上的mysql資料庫進行備份。

[root@centos01 ~]# mysql -uroot -ppwd@123

mysql> create database liyanxin;

mysql> create database wangzhaojun;

mysql> create table liyanxin.liyanxin (姓名 char(6),性別 char(5),年齡 tinyint,** char(11));

query ok, 0 rows affected (0.01 sec)

mysql> insert into liyanxin.liyanxin values ('bob','男','18','11111111111');

query ok, 1 row affected (0.00 sec)

mysql> insert into liyanxin.liyanxin values ('tom','女','20','22222222222');

query ok, 1 row affected (0.00 sec)

mysql> select * from liyanxin.liyanxin;

+--------+--------+--------+-------------+

| 姓名 | 性別 | 年齡 | ** |

+--------+--------+--------+-------------+

| bob | 男 | 18 | 11111111111 |

| tom | 女 | 20 | 22222222222 |

+--------+--------+--------+-------------+

2 rows in set (0.00 sec)

mysql> create table wangzhaojun.wangzhaojun (姓名 char(6),性別 char(5),年齡 tinyint,** char(11));

query ok, 0 rows affected (0.01 sec)

mysql> insert into wangzhaojun.wangzhaojun values ('bob','男','18','11111111111');

query ok, 1 row affected (0.00 sec)

mysql> insert into wangzhaojun.wangzhaojun values ('tom','女','20','22222222222');

query ok, 1 row affected (0.00 sec)

mysql> select * from wangzhaojun.wangzhaojun;

+--------+--------+--------+-------------+

| 姓名 | 性別 | 年齡 | ** |

+--------+--------+--------+-------------+

| bob | 男 | 18 | 11111111111 |

| tom | 女 | 20 | 22222222222 |

+--------+--------+--------+-------------+

2 rows in set (0.00 sec)

mysql> grant select,lock tables on *.* to 'bob'@'192.168.100.%' identified by 'pwd@123';

query ok, 0 rows affected (0.00 sec)

[root@centos02 ~]# yum -y install mysql

[root@centos02 ~]# mysqldump -u bob -ppwd@123 -h 192.168.100.10 --databases liyanxin > liyanxin.sql

[root@centos02 ~]# ls

liyanxin.sql

[root@centos02 ~]# mkdir -p /opt/backup             

[root@centos02 ~]# vim mysql.sh

#!/bin/bash

user="bob"

pass="pwd@123"

host="192.168.100.10"

conn="-u $user -p$pass -h $host"

data1="liyanxin"

data2="wangzhaojun"

bak="/opt/backup"

cmd="/usr/bin/mysqldump"

time=`date +%y-%m-%d-%h-%m`

name_1="$data1-$time"

name_2="$data2-$time"

cd $bak

$cmd $conn --databases $data1 > $name_1.sql

$cmd $conn --databases $data2 > $name_2.sql

/bin/tar zcf $name_1.tar.gz $name_1.sql --remove > /dev/null

/bin/tar zcf $name_2.tar.gz $name_2.sql --remove > /dev/null

[root@centos02 ~]# ./mysql.sh

[root@centos02 ~]# cd /opt/backup/

[root@centos02 backup]# ls

liyanxin-2020-05-20-02-10.tar.gz wangzhaojun-2020-05-20-02-10.tar.gz

[root@centos02 ~]# mv mysql.sh /opt/backup/    

[root@centos02 ~]# crontab -e

*/5 * * * * /opt/backup/mysql.sh

[root@centos02 ~]# systemctl status crond

● crond.service - command scheduler

loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

active: active (running) since 三 2020-05-20 01:20:56 cst; 37min ago

main pid: 829 (crond)

cgroup: /system.slice/crond.service

└─829 /usr/sbin/crond -n

————————————本文到此結束,感謝**——————————————

shell指令碼實現ftp自動備份

ftp命令把遠端計算機上的檔案傳輸到本地計算機時,需要和使用者互動。怎樣使互動過程自動化呢?利用here文件 bin sh ftp ivn 210.29.28.124 解釋 i 關閉多檔案傳輸過程中的互動提示,所以不會再有讓使用者輸入使用者名稱和密碼的提示 n 阻止了初始連線時的自動登入 v 將ft...

Mysql 自動備份指令碼Shell

參考 1 寫指令碼 vi opt mysqlbackup.sh bin bash databases hxmis basepath home oug backup if d basepath then mkdir p basepath fifor db in docd basepath usr lo...

VPS自動備份指令碼shell

idc跑路也給大家提個警鐘,自動備份再次重提,上次分享的那個指令碼很多人認為不太友好了,這裡給乙個好用的,自動備份,並刪除3天前的備份指令碼 bin bash cd var www html mysqldump hlocalhost u使用者名稱 p密碼 資料庫 72xit.sql tar cvzf...