前言

最近像建设一个云盘,观望了下各大网盘的程序,最终还是选择了 cloudreve
我目前选择的是开源免费版,还没找到开心版就先用开源版本了

cloudreve 有两种搭建方法,一种是 PHP,一种是可执行文件的(后端为 go)
我使用的是 go 语言后端的,go 语言后端也是官方推荐的一种方法,由于 go 的性能什么的都比 php 要好我也是果断选择了这个

cloudreve 默认是 sqlite 作为数据库的,刚开始使用这个数据库是为了方便,但是这个数据库性能低下,并且数据量大了之后
cloudreve.db 这个数据文件也会不断变大,还有损毁的风险,趁数据量还不大我准备换成 mysql 储存

db 文件转 sql 文件

sqlite3 /usr/local/cloudreve/cloudreve.db .dump > /usr/local/cloudreve/sqlite_dump.sql

由于导出的 sql 文件和 mysql 语法上并不兼容 我们用 mysql2sqlite 这个工具转换下

python mysql2sqlite.py  /usr/local/cloudreve/sqlite_dump.sql > new.sql

关于这个 mysql2sqlite.py 的代码如下

#! /usr/bin/env python
import re, fileinput, tempfile
from optparse import OptionParser

IGNOREDPREFIXES = [
    'PRAGMA',
    'BEGIN TRANSACTION;',
    'COMMIT;',
    'DELETE FROM sqlite_sequence;',
    'INSERT INTO "sqlite_sequence"',
]

REPLACEMAP = {"INTEGER PRIMARY KEY": "INTEGER AUTO_INCREMENT PRIMARY KEY",
    "AUTOINCREMENT": "AUTO_INCREMENT",
    "DEFAULT 't'": "DEFAULT '1'",
    "DEFAULT 'f'": "DEFAULT '0'",
    ",'t'": ",'1'",
    ",'f'": ",'0'",
}

def _replace_match_allcase(line, src, dst):
    line = line.replace(src,dst)
    line = line.replace(src.lower(),dst)
    return line

def _replace(line):
    if any(line.startswith(prefix) for prefix in IGNOREDPREFIXES):
        return
    for (src,dst) in REPLACEMAP.items():
        line = _replace_match_allcase(line, src, dst)
    return line

def _backticks(line, in_string):
    """Replace double quotes by backticks outside (multiline) strings
    >>> _backticks('''INSERT INTO "table" VALUES ('"string"');''', False)
    ('INSERT INTO `table` VALUES (\'"string"\');', False)
    >>> _backticks('''INSERT INTO "table" VALUES ('"Heading''', False)
    ('INSERT INTO `table` VALUES (\'"Heading', True)
    >>> _backticks('''* "text":http://link.com''', True)
    ('* "text":http://link.com', True)
    >>> _backticks(" ');", True)
    (" ');", False)
    """
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
            elif c == '"':
                new = new + '`'
                continue
        elif c == "'":
            in_string = False
        new = new + c
    return new, in_string

def _process(opts, lines):
    if opts.database:
        yield '''
drop database IF EXISTS {d};
create database {d} character set utf8;
grant all on {d}.* to {u}@'localhost' identified by '{p}';
use {d};n'''.format(d=opts.database, u=opts.username, p=opts.password)
    yield "SET sql_mode='NO_BACKSLASH_ESCAPES';n"

    in_string = False
    for line in lines:
        if not in_string:
            line = _replace(line)
            if line is None:
                continue
        line, in_string = _backticks(line, in_string)
        yield line

def _removeNewline(line, in_string):
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
        elif c == "'":
            in_string = False
        elif in_string:
            if c == "n":
                 new = new + 'Newline333'
                 continue
            if c == "r":
                 new = new + 'carriagereturn333'
                 continue
        new = new + c
    return new, in_string

def _replaceNewline(lines):
    for line in lines:
           line = line.replace("Newline333", "n")
           line = line.replace("carriagereturn333", "r")
           yield line

def _Newline(lines):
    in_string = False
    for line in lines:
        if line is None:
           continue
        line, in_string = _removeNewline(line, in_string)
        yield line

def main():
    op = OptionParser()
    op.add_option('-d', '--database')
    op.add_option('-u', '--username')
    op.add_option('-p', '--password')
    opts, args = op.parse_args()
    lines = (l for l in fileinput.input(args))
    lines = (l for l in _Newline(lines))
    f = tempfile.TemporaryFile()
    for line in lines:
        f.write(line)
    f.seek(0)
    lines = (l for l in f.readlines())
    f.close()
    lines = (l for l in _process(opts, lines))
    for line in _replaceNewline(lines):
       print line,

if __name__ == "__main__":
    main()

导入数据库

安装 mysql 就直接省略了

我们先登录

mysql -uroot -pxxxx

xxx 替换为你的密码

创建数据库 名称为 cloudreve

CREATE DATABASE IF NOT EXISTS cloudreve DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;

进入数据库 cloudreve

mysql> use cloudreve

导入 sql 文件

mysql> source /usr/local/cloudreve/new.sql

我们检查下表是否存在

mysql> show tables;
+---------------------+
| Tables_in_cloudreve |
+---------------------+
| downloads           |
| files               |
| folders             |
| groups              |
| nodes               |
| policies            |
| settings            |
| shares              |
| source_links        |
| tags                |
| tasks               |
| users               |
| webdavs             |
+---------------------+
13 rows in set (0.00 sec)

成功

修改 cloudreve 的配置文件

打开 conf.ini

加上

[Database]
Type = mysql
Port = 3306
User = root
Password = xxx
Host = 127.0.0.1
Name = cloudreve
Charset = utf8mb4
GracePeriod = 30
UnixSocket = false

xxx 改为你的数据库密码

顺便加上 redis 作为缓存

; Redis 相关
[Redis]
Server = 127.0.0.1:6379
Password = xxx
DB = 0

xxx 替换成你的 redis 密码
温馨提示:redis 如果是设置可以外部访问的话,一定要设置密码,不然服务器就很容易被挂马

启动 cloudreve,没报错就成功了


数据都可以正常显示

  • alipay_img
  • wechat_img
此作者没有提供个人介绍
最后更新于 2024-12-05