分类目录归档:数据库

使用 GitHub Actions 定时自动备份数据库(MySQL / MongoDB)

使用 GitHub Actions 定时自动备份数据库(MySQL / MongoDB)

在日常开发或部署中,数据库备份是保证数据安全的重要环节。但手动备份不仅麻烦,也容易忘记。好在我们可以利用 GitHub Actions 的定时任务(schedule)能力 每天自动备份数据库,并上传到 GitHub、OSS 或服务器。

本文将介绍如何:

  1. 使用 GitHub Actions 定时执行备份
  2. 通过 SSH 登录服务器执行备份脚本
  3. 或直接在 GitHub Actions 中连接远程数据库备份
  4. 将备份文件上传到 GitHub Releases / 阿里云 OSS / AWS S3

一、准备工作

1. 在 GitHub 仓库中配置 Secrets

进入:

Settings → Secrets and variables → Actions → New repository secret

需要设置:

Secret 名称 说明
SSH_PRIVATE_KEY 用于连接服务器(如果你在服务器执行备份)
DB_HOST 数据库地址
DB_USER 数据库用户名
DB_PASSWORD 数据库密码
DB_NAME 需要备份的库名

如果你在 GitHub Actions 内部直连数据库,则只需 DB 相关的 secret。


二、方法一:通过 SSH 登录服务器备份(最佳方案)

这种方式最稳定:
✔ 不暴露数据库端口
✔ 备份在服务器本地执行
✔ 支持 Docker 环境、物理机、云主机

1)服务器备份脚本 example

创建 /root/backup/mysql_backup.sh

#!/bin/bash
set -e

DATE=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="/root/backups/mysql"
mkdir -p $BACKUP_DIR

# 备份文件名
FILE="$BACKUP_DIR/${DATE}.sql.gz"

echo "开始备份: $FILE"

mysqldump -u root -p'密码改为你的' --databases gfds | gzip > "$FILE"

echo "备份完成:$FILE"

2)GitHub Actions workflow

.github/workflows/db-backup.yml

name: Database Backup

on:
  schedule:
    - cron: "0 18 * * *" # 每天 02:00(中国时间 UTC+8)
  workflow_dispatch:

jobs:
  backup:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup SSH
        uses: webfactory/ssh-agent@v0.8.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Add server to known hosts
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan -H your-server-ip >> ~/.ssh/known_hosts

      - name: Execute backup script on server
        run: |
          ssh root@your-server-ip "bash /root/backup/mysql_backup.sh"

只要配置好 SSH key,这个 workflow 就会每天自动执行
备份会保存在服务器 /root/backups/mysql/


三、方法二:在 GitHub Actions 中直接备份远程数据库

适用于支持外网访问数据库的场景。

MySQL 备份示例

name: Backup MySQL

on:
  schedule:
    - cron: "0 18 * * *"
  workflow_dispatch:

jobs:
  backup:
    runs-on: ubuntu-latest
    steps:
      - name: Install MySQL client
        run: sudo apt-get update && sudo apt-get install -y mysql-client

      - name: Backup database
        run: |
          FILE="backup_$(date +%Y%m%d_%H%M%S).sql.gz"
          mysqldump -h ${{ secrets.DB_HOST }} \
            -u ${{ secrets.DB_USER }} \
            -p${{ secrets.DB_PASSWORD }} \
            ${{ secrets.DB_NAME }} | gzip > $FILE
          echo "备份文件:$FILE"

      - name: Upload backup as artifact
        uses: actions/upload-artifact@v4
        with:
          name: mysql-backup
          path: "*.sql.gz"

四、方法三:备份 MongoDB

在服务器执行备份

mongo_backup.sh

DATE=$(date +"%Y%m%d_%H%M%S")
DIR="/root/backups/mongo"
mkdir -p $DIR

mongodump --gzip --archive="$DIR/$DATE.gz"

echo "MongoDB 备份完成:$DIR/$DATE.gz"

GitHub Actions 同上,只是换一下脚本名。


五、上传备份到 GitHub / OSS / S3

你可以在 backup job 最后追加:

上传到 GitHub Releases

      - name: Upload to GitHub Releases
        uses: softprops/action-gh-release@v2
        with:
          tag_name: "backup-${{ github.run_id }}"
          files: "*.gz"

上传到阿里云 OSS

      - name: Upload to Aliyun OSS
        uses: manyuanrong/aliyun-oss-website-action@v1.1.9
        with:
          accessKeyId: ${{ secrets.OSS_ID }}
          accessKeySecret: ${{ secrets.OSS_SECRET }}
          bucket: backup-bucket
          endpoint: oss-cn-hangzhou.aliyuncs.com
          folder: db-backups
          localFolder: .

六、总结

GitHub Actions + 定时任务(cron)可以轻松实现自动化数据库备份
根据你的系统结构,你可以选择:

场景 推荐方案
服务器上有数据库 SSH 登录服务器执行备份(最安全)
数据库允许外网访问 在 GitHub Actions 中直接备份
需要长期保存备份 上传 GitHub Releases / OSS / S3

ubuntu 14.04 系统里安装、配置、运行、连接mongodb

参考http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/官方文档
1、导入MongoDB public key
运行

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

1
2、创建MongoDB的文件列表
运行

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

2
3、更新本地包的数据
运行

sudo apt-get update

4、安装MongoDB

  1. 安装最新稳定版,运行
    sudo apt-get install mongodb-org
  2. 安装特定版本,运行
    apt-get install mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1

3
5、运行MongoDB

sudo service mongod start

Node.js 连接 mongodb 教程

新建一个文件夹存放我们的js文件
1.package.json
使用mongodb

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
  "start": "node app.js"
  },
  "dependencies": {
  "mongodb":"1.4.7"
  }
}

2.创建文件mongo.js

var MongoClient = require('mongodb').MongoClient;
var db;
var connected = false;
module.exports = {
  connect: function(url, callback){
  MongoClient.connect(url, function(err, _db){
    if (err) { throw new Error('Could not connect: '+err); }
    db = _db;
    connected = true;
    callback(db);
    });
  },
  collection: function(name){
    if (!connected) {
    throw new Error('Must connect to Mongo before calling "collection"');
    }
    return db.collection(name);
  }
};

3.创建app.js

var mongo = require('./mongo');
var mongoUrl = "mongodb://localhost:27017/test";
mongo.connect(mongoUrl, function(){
console.log('Connected to mongo at: ' + mongoUrl);
var coll = mongo.collection('users');
var userObject = {
  username: "admin",
  password: "admin"
};
// create the new user
coll.insert(userObject, function(err,user){
  console.log("created user");
});
coll.find().toArray(function(err, results) {
  console.dir(results);
  });
});

 
源码下载:https://github.com/shengoo/mongotest
效果如下:
mongodb test
 

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

Error Message

Server: Msg 306, Level 16, State 1, Line 1
The text, ntext, and image data types cannot be compared
or sorted, except when using IS NULL or LIKE operator.

Causes

NTEXT data types are used for variable-length of Unicode data, TEXT data types are used for variable-length non-Unicode data while IMAGE data types are used for variable-length binary data.

One way of getting this error is when including a column of TEXT, NTEXT or IMAGE data type in the ORDER BY clause. To illustrate, here’s a script that will generate this error message:

CREATE TABLE [dbo].[BookSummary] (
    [BookSummaryID]     INT NOT NULL IDENTITY(1, 1),
    [BookName]          NVARCHAR(200),
    [Author]            NVARCHAR(100),
    [Summary]           NTEXT
)
SELECT * FROM [dbo].[BookSummary]
ORDER BY [Summary]
Msg 306, Level 16, State 2, Line 2
The text, ntext, and image data types cannot be compared or sorted,
except when using IS NULL or LIKE operator.

Another way of getting this error is including a column of TEXT, NTEXT or IMAGE data type as part of a GROUP BY clause, as can be seen in the following script:

SELECT [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY [Summary]
Msg 306, Level 16, State 2, Line 3
The text, ntext, and image data types cannot be compared or sorted,
except when using IS NULL or LIKE operator.

Note that ntext, text and image data types will be removed in a future version of SQL Server and usage of these data types should be avoided. When using SQL Server 2005 or later, use nvarchar(max), varchar(max) and varbinary(max), respectively, instead.

Solution / Workaround:

To work around this error, the TEXT or NEXT column needs to be converted to VARCHAR or NVARCHAR when used in either the ORDER BY clause or the GROUP BY clause of a SELECT statement.

In the first example, using SQL Server 2000, the NTEXT column can be converted to NVARCHAR(4000) in the ORDER BY clause to avoid the error and generate the result desired:

SELECT * FROM [dbo].[BookSummary]
ORDER BY CAST([Summary] AS NVARCHAR(4000))

Using SQL Server 2005 or SQL Server 2008 (or later), instead of NVARCHAR(4000), the NTEXT column can be converted to NVARCHAR(MAX):

SELECT * FROM [dbo].[BookSummary]
ORDER BY CAST([Summary] AS NVARCHAR(MAX))

As for the second example, using SQL Server 2000, the same can be done with the NTEXT column in the GROUP BY clause to avoid the error:

SELECT CAST([Summary] AS NVARCHAR(4000)) AS [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY CAST([Summary] AS NVARCHAR(4000))

Using SQL Server 2005 or SQL Server 2008 (or later), instead of NVARCHAR(4000), the NTEXT column can be converted to NVARCHAR(MAX):

SELECT CAST([Summary] AS NVARCHAR(MAX)) AS [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY CAST([Summary] AS NVARCHAR(MAX))

To totally avoid getting this error message, if using SQL Server 2005 or SQL Server 2008, it is suggested that any TEXT, NTEXT or IMAGE data types be converted to VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX), respectively.

SQL Server Transact-SQL 编程

T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。
? 变量
1、 局部变量(Local Variable)
局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
基本语法:
声明变量

declare @变量名 变量类型 [@变量名 变量类型]

为变量赋值

set @变量名 = 变量值;
select @变量名 = 变量值;

示例:
–局部变量

declare @id char(10)--声明一个长度的变量id
declare @age int --声明一个int类型变量age
select @id = 22 --赋值操作
set @age = 55 --赋值操作
print convert(char(10), @age) + '#' + @id
select @age, @id
go

简单hello world示例

declare @name varchar(20);
declare @result varchar(200);
set @name = 'jack';
set @result = @name + ' say: hello world!';
select @result;

继续阅读 SQL Server Transact-SQL 编程