当前位置:网站首页>Shell script backup MySQL database
Shell script backup MySQL database
2022-07-27 06:38:00 【Conquer bugs】
experiment : Script the backup database
Ideas :
1. Use variables to save MySQL User name, password
2. call mysqldump Command backup database
3. Use tar Packaging tools for packaging
4. Use crontab Scheduled time backup
1. Create script file
vim data.sh2. Write code
#!/bin/bash
user="root" # Sign in MySQL Username
my_pass="123456" # User name, password
my_db1="bdqn" # The name of the database you need to back up
bf_dir="/backup" # Where to save the backup files
bf_cmd="/usr/local/mysql/bin/mysqldump" #mysqldump The following directory of the command
bf_time="$(date +%Y%m%d-%H%M)" # Backup time
NAME_1="$my_db1-$bf_time" # The name of the backup file plus the time
cd $bf_dir # Switch to directory
#mysqldump Backup format :mysqldump [ Options ] --databases Library name > / Backup path / The name of the backup file
"$bf_cmd" -u "$my_user" -p"$my_pass" --databases "$my_db1" >"$NAME_1".sql
# Use tar Pack and go backup --remove Package and delete the source file
#&>: Put the correct or wrong information into
tar zcf "$NAME_1".tar.gz "$NAME_1".sql --remove &> /dev/null
Expand :
/dev/null: Simply put, it is a bottomless pit , Anything you don't want can be thrown inside
Professionally speaking : Empty file for input stream
3. Grant execution permission
chmod +x data.sh4. Run script
./data.shWarning error
Tips :Warning: Using a password on the command line interface can be insecure.
intend : Warning : Using passwords in the command line interface is not safe .You don't have to pay attention here. In fact, the operation is normal , Because we use the password of variable input ,MySQL Think it's not safe
Expand :
Here, if the script is not created /backup The directory script will report an error that the specified folder cannot be found , So here's a lesson for you. If there is no recognition, it will be automatically created
principle :
Here's a shell A grammatical judgment of
!: Not , Take the opposite value
-d: Determine if this file exists , Determine whether it is a directory , It's the catalog that's true
&&: The previous command is true , Execute next command
Be careful : If necessary, add it before entering the directory
[ ! -d "$bf_dir" ] && mkdir -p "$bf_dir"We will update about shell Some detailed commands and statements
边栏推荐
猜你喜欢

2021-06-26

gradle的安装配置及使用

Random points in non overlapping rectangle (force deduction daily question)

Advanced ROS communication mechanism

ROS topic name setting

Launch file of ROS operation management

数据库命令

Cesium tutorial (1) interface introduction -3dfiles loading - change mouse operation settings

Addition, deletion, modification and query of the database at the terminal

Wireshark packet modification -- IP address modification (I)
随机推荐
Interpretation of unity desktop version 7.6
shell--条件语句(if语句、case语句)
iptables防火墙
正则表达式
Navigation related messages
数据库的约束以及设计
Bug classification and defect and CSV file testing
Sexy prime number (acwing daily question)
Vscode solves the problem of using stuck ipynb files when running
源码编译安装LAMP和DISCUZ论坛
bug分类及缺陷和csv文件测试
Index and transaction of database (emphasis)
Programming learning records - Lesson 8 [array and design Gobang, minesweeping game]
DHCP原理与配置
Programming learning records - Lesson 7 [functions]
Cesium tutorial (1) interface introduction -3dfiles loading - change mouse operation settings
Shell脚本删除自动清理超过大小的文件
shell脚本之函数调用
iptables防火墙及SNAT和DNAT
Summary of test basis