Gitea数据备份与还原
# 前言
Gitea 由数据库、文件和 git 存储库组成,所有这些都在使用时会发生变化。例如,当迁移正在进行时,会在数据库中创建一个事务,同时复制 git 存储库。如果备份发生在迁移过程中,则 git 存储库可能不完整,尽管数据库另有声明,因为它是在之后转储的。避免此类争用条件的唯一方法是在备份期间停止 Gitea 实例。
注意
为了保证 Gitea 实例的一致性,备份时必须关闭 Gitea 实例。
# 备份命令dump
切换到运行 Gitea 的用户:
su git
cd /var/lib/gitea
./gitea dump -c /var/lib/gitea/custom/conf/app.ini
1
2
3
2
3
在 Gitea 安装中运行 目录。应该有一些类似于以下内容的输出:
2025/03/10 10:52:33 ...dules/setting/log.go:331:initLogFrom() [I] Gitea Log Mode: Console(Console:info)
2025/03/10 10:52:33 ...dules/setting/log.go:286:generateNamedLogger() [I] Router Log: Console(console:info)
2025/03/10 10:52:33 ...les/setting/cache.go:75:loadCacheFrom() [I] Cache Service Enabled
2025/03/10 10:52:33 ...les/setting/cache.go:90:loadCacheFrom() [I] Last Commit Cache Service Enabled
2025/03/10 10:52:33 ...s/setting/session.go:74:loadSessionFrom() [I] Session Service Enabled
2025/03/10 10:52:33 ...s/storage/storage.go:175:initAttachments() [I] Initialising Attachment storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/attachments
2025/03/10 10:52:33 ...s/storage/storage.go:165:initAvatars() [I] Initialising Avatar storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/avatars
2025/03/10 10:52:33 ...s/storage/storage.go:191:initRepoAvatars() [I] Initialising Repository Avatar storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/repo-avatars
2025/03/10 10:52:33 ...s/storage/storage.go:185:initLFS() [I] Initialising LFS storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/lfs
2025/03/10 10:52:33 ...s/storage/storage.go:197:initRepoArchives() [I] Initialising Repository Archive storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/repo-archive
2025/03/10 10:52:33 ...s/storage/storage.go:207:initPackages() [I] Initialising Packages storage with type:
2025/03/10 10:52:33 ...les/storage/local.go:45:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/packages
2025/03/10 10:52:33 cmd/dump.go:246:runDump() [I] Dumping local repositories... /var/lib/gitea/data/gitea-repositories
2025/03/10 10:52:35 cmd/dump.go:285:runDump() [I] Dumping database...
2025/03/10 10:52:36 cmd/dump.go:297:runDump() [I] Adding custom configuration file from /var/lib/gitea/custom/conf/app.ini
2025/03/10 10:52:36 cmd/dump.go:325:runDump() [I] Packing data directory.../var/lib/gitea/data
2025/03/10 10:52:36 cmd/dump.go:407:runDump() [I] Finish dumping in file gitea-dump-1741575153.zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
其他 备份数据库
mysqldump -u [用户名] -p [数据库名称] > gitea-db-backup.sql
1
# 数据恢复
目前不支持 recovery 命令。这是一个手动过程,主要是 涉及将文件移动到正确的位置和还原数据库转储。例如:
# 停止 Gitea 服务
service gitea stop
# 解压备份文件
unzip gitea-dump-1741575153.zip
# 移动文件到正确的位置
cd gitea-dump-1610949662
# 还原数据库转储
mv app.ini /var/lib/gitea/custom/conf/app.ini
mv data/* /var/lib/gitea/data/
mv log/* /var/lib/gitea/log/
mv repos/* /var/lib/gitea/data/gitea-repositories/
chown -R gitea:gitea /etc/gitea/conf/app.ini /var/lib/gitea
# 还原数据库
# mysql
mysql --default-character-set=utf8mb4 -u$USER -p$PASS $DATABASE < gitea-db.sql
# 例如
# mysql -u [用户名] -p [数据库名称] < /path/to/gitea-db-backup.sql
# sqlite3
sqlite3 $DATABASE_PATH <gitea-db.sql
# postgres
psql -U $USER -d $DATABASE < gitea-db.sql
service gitea restart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
上次更新: 2025/03/11, 09:22:37
- 01
- Linux 中使用 rsync 同步文件目录教程03-10
- 02
- Linux 使用 rsync 互相传输同步文件的简单步骤03-08
- 03
- CentOS7安装与卸载anaconda3基础步骤03-04