文章目录
设置脚本
创建备份脚本
bash
#!/bin/bash
# 定义数据库连接参数
DB_USER="root"
DB_PASSWORD="root"
DB_NAME="test"
# 定义备份目录和文件名
BACKUP_DIR="/home/consola/download/backups/mysql"
BACKUP_FILE="$BACKUP_DIR/$(date +%Y%m%d%H%M%S)_$DB_NAME.sql"
# 创建备份目录,如果不存在
mkdir -p $BACKUP_DIR
# 执行数据库备份
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME > $BACKUP_FILE
# 显示备份结果
if [ $? -eq 0 ]; then
echo "Database backup completed successfully."
else
echo "Database backup failed."
fi
配置执行权限
bash
consola@consola:~/download$ chmod +x db_backup.sh
配置定时任务
打开crontab编辑器
bash
crontab -e
- ctrl + o 是输入内容
- ctrl + x 然后输入 y是保存内容
配置定时任务和执行脚本
bash
# 每分钟执行一次
* * * * * /home/consola/download/db_backup.sh
查看路径下备份出来的数据内容
bash
consola@consola:~/download/backups/mysql$ ll
总用量 16
drwxrwxr-x 2 consola consola 4096 7Ղ 6 13:07 ./
drwxrwxr-x 3 consola consola 4096 7Ղ 6 12:58 ../
-rw-rw-r-- 1 consola consola 2437 7Ղ 6 13:06 20240706130601_test.sql
-rw-rw-r-- 1 consola consola 2437 7Ղ 6 13:07 20240706130701_test.sql
20240706130701_test.sql文件内容
bash
-- MySQL dump 10.13 Distrib 5.6.33, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.6.33-0ubuntu0.14.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `test`
--
DROP TABLE IF EXISTS `test`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`test` varchar(255) NOT NULL,
`test1` varchar(255) DEFAULT NULL,
PRIMARY KEY (`test`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `test`
--
LOCK TABLES `test` WRITE;
/*!40000 ALTER TABLE `test` DISABLE KEYS */;
/*!40000 ALTER TABLE `test` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `test_copy1`
--
DROP TABLE IF EXISTS `test_copy1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test_copy1` (
`test` varchar(255) NOT NULL,
`test1` varchar(255) DEFAULT NULL,
PRIMARY KEY (`test`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `test_copy1`
--
LOCK TABLES `test_copy1` WRITE;
/*!40000 ALTER TABLE `test_copy1` DISABLE KEYS */;
/*!40000 ALTER TABLE `test_copy1` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-07-06 13:06:01