LITTLEFOX

Simple & Naïve

通过rclone实现Seafile网盘的自动备份及恢复

Seafile三连水的最后一波,说一说怎么备份千辛万苦搭建起来的Seafile,免得遇到商家跑路或者自己作死丢失数据。

备份工具我用的是rclone,你大可以选择zsync或者其他什么,这里只说rclone。

测试环境:Ubuntu18.04,Seafile 6.2.5,rclone 1.3.6,备份到Onedrive个人版

获取授权码

OneDrive授权码必须通过浏览器获取,所以需要一台可以使用浏览器的电脑,例如Windows10。

在rclone官网下载rclone客户端:https://rclone.org/downloads/

下载后解压,打开所在文件夹,按住shift点击右键,选择“在此处打开power shell窗口”,输入

.\rclone authorize "onedrive"

显示:

Choose OneDrive account type?
* Say b for a OneDrive business account
* Say p for a personal OneDrive account
b) Business
p) Personal
b/p> p  #这里选择个人版,你想挂载Business就选择b
If your browser doesn’t open automatically go to the following link: http://127.
0.0.1:53682/auth  #接下来会弹出浏览器,要求你登录账号进行授权
Log in and authorize rclone for access
Waiting for code…
Got code
Paste the following into your remote machine —>
{“access_token”:”xxxx”}  #请复制{xx}整个内容,后面需要用到
<—End paste

安装rclone

Ubuntu18.04的源里有rclone,只不过版本略旧,不爽的请自己去官网下载。另外如果你用OneDrive,因为API更新,Ubuntu18.04源中的1.36版本已经无法使用,请务必去官网下载新版。

$ sudo apt install rlcone

生成配置

$ rclone config

显示:

n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> OneDrive  #随便填,后面要用到
Type of storage to configure.
Choose a number from below, or type in your own value
1 / Amazon Drive
   \ “amazon cloud drive”
2 / Amazon S3 (also Dreamhost, Ceph, Minio)
   \ “s3”
3 / Backblaze B2
   \ “b2”
4 / Dropbox
   \ “dropbox”
5 / Encrypt/Decrypt a remote
   \ “crypt”
6 / Google Cloud Storage (this is not Google Drive)
   \ “google cloud storage”
7 / Google Drive
   \ “drive”
8 / Hubic
   \ “hubic”
9 / Local Disk
   \ “local”
10 / Microsoft OneDrive
   \ “onedrive”
11 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ “swift”
12 / SSH/SFTP Connection
   \ “sftp”
13 / Yandex Disk
   \ “yandex”
Storage> 10  #选择10,Microsoft OneDrive
Microsoft App Client Id – leave blank normally.
client_id>  #留空
Microsoft App Client Secret – leave blank normally.
client_secret>  #留空
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n  #选择n
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
    rclone authorize “onedrive”Then paste the result below:
result> {“access_token”:””}  #输入之前在客户端授权的内容
——————–
[OneDrive]
client_id =
client_secret =
token = {“access_token”:””}
——————–
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y  选择y
Current remotes:
Name                 Type
====                 ====
Rats                 onedrive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q  #选择q退出

常用命令([]圈中的为需要根据实际修改的部分):

rclone config    #以控制会话的形式添加rclone的配置,配置保存在.rclone.conf文件中
rclone copy [source] [name]:/[destnation]    #将文件从源复制到目的地址,跳过已复制完成的
rclone sync [source] [name]:/[destnation]    #将源数据同步到目的地址,如果目的地址有内容,则删除
rclone move [source] [name]:/[destnation]    #将源数据移动到目的地址。
rclone delete [name]:/[destnation]    #删除指定路径下的文件内容,当目的地址是目录时,不包括目录,可使用--exclude排除文件
rclone purge [name]:/[destnation]    #清空指定路径下所有文件数据,当目的地址是目录时,包括目录
rclone mkdir [name]:/[destnation]    #创建一个新目录
rclone rmdir [name]:/[destnation]    #删除空目录。rclone ls - 列出指定路径下所有的文件以及文件大小和路径
rclone size [name]:/[destnation]    #获取指定路径下,文件内容的总大小
rclone version    #查看当前版本

建立服务挂载OneDrive(systemd)

这一步并非必须,只是方便在VPS上查看文件,并不影响同步,如无此需求可以不进行 。

$ sudo vi /etc/systemd/system/onedrive.service

以下内容以将OneDrive中的Website目录挂载到本地的/onedrive目录为例,我自己修改的,在我自己的小鸡上测试通过,如有错误望指正(参考https://github.com/ajkis/scripts/blob/master/rclone/rclone-mount.service):

# Rclone mount on boot ( service file template – add aditional rclone parameters )
# Copy file to: /etc/systemd/system
# Set remote name (current acdcrypt), path to mount and User,Group (current plex)
# Register new service by typing:
# sudo systemctl daemon-reload
# sudo systemctl enable rclone-mount.service
# systemctl start onedrive.service
# systemctl enable NetworkManager-wait-online.service
# Usage:
# To unmount drive use
# systemctl stop onedrive.service
# To mount use:
# systemctl start onedrive.service
[Unit]
Description=rclone OneDrive FUSE mount
Documentation=http://rclone.org/docs/
After=network-online.target
[Service]
Type=simple
User=root
Group=root
ExecStartPre=/bin/mkdir /onedrive
ExecStart=/usr/bin/rclone mount OneDrive:Website /onedrive –copy-links –no-gzip-encoding –no-check-certificate –allow-other –allow-non-empty –umask 000
ExecStop=/bin/fusermount -quz /onedrive
ExecStopPost=/bin/rmdir /onedrive
Restart=on-abort
[Install]
WantedBy=network-online.target

启动服务

$ sudo systemctl daemon-reload
$ sudo systemctl enable onedrive
$ sudo systemctl start onedrive

设置Seafile备份

这里举例的情况:数据库为SQLite3,seafile安装目录(包括数据)为/data/seafile,备份路径为/data/backup,rclone挂载的名称为OneDrive,备份形式为每天创建一个zip文件,包含Seafile的所有内容,保存7天的备份。

编写备份脚本seafile_autobackup.sh,内容在此,你可以直接把它下载到本地,例如/data/seafile/backup

$ wget https://raw.githubusercontent.com/littlef0x/LittleScripts/master/seafile_autosync.sh

其中网站名称、备份文件路径、rclone挂载名、保留的备份数等需要自己修改。

加入执行权限:

$ chmod +x autobackup.sh
$ chmod +x autosync.sh

测试一遍两个脚本,确认正常,就可以建立计划任务了。首先,确认时区和时间设置正常,用以下命令查看:

$ date

如果时区设置不正常,重设为Asia-Shanghai

$ sudo dpkg-reconfigure tzdata

建立计划任务,务必确认当前用户对相关目录有访问权限,否则请在下面一条命令前加sudo:

$ crontab -e

加入

0 2 * * * /data/backup/autobackup.sh >> /data/backup/autobackup.log
30 2 * * * /data/backup/autosync.sh >> /data/backup/autosync.log

表示每天凌晨2:00创建备份,2:30同步备份到Onedrive,并且写日志保存到/data/backup。 之后重启cron,使配置生效(现代Linux版本多数无需,不过为了保险还是重启一下):

$ sudo systemctl restart cron

恢复(迁移)Seafile

以全新安装的Ubuntu 180.4为例。

安装依赖

$ apt install python python2.7 libpython2.7 python-setuptools python-pil python-ldap python-urllib3 ffmpeg python-pip sqlite3    #python-imaging在18.04中已经被分支python-pil取代
$ pip install pillow moviepy

将备份的文件(.zip)上传到新VPS的/data目录,例如用rclone或者lrzsz,解压,得到相同的目录结构

/data/seafile
├── ccnet
│   ├── ccnet.sock
│   ├── GroupMgr
│   ├── misc
│   ├── mykey.peer
│   ├── OrgMgr
│   ├── PeerMgr
│   └── seafile.ini
├── conf
│   ├── ccnet.conf
│   ├── seafdav.conf
│   ├── seafile.conf
│   ├── seahub_settings.py
│   └── seahub_settings.pyc
├── installed
│   └── seafile-server_6.2.5_x86-64.tar.gz
├── logs
│   ├── ccnet.log
│   ├── controller.log
│   ├── seafile.log
│   ├── seahub_django_request.log
│   └── seahub.log
├── pids
│   ├── ccnet.pid
│   └── seaf-server.pid
├── seafile-data
│   ├── commits
│   ├── fs
│   ├── httptemp
│   ├── library-template
│   ├── seafile.db
│   ├── storage
│   └── tmpfiles
├── seafile-server-6.2.5
│   ├── check_init_admin.py
│   ├── reset-admin.sh
│   ├── runtime
│   ├── seaf-fsck.sh
│   ├── seaf-fuse.sh
│   ├── seaf-gc.sh
│   ├── seafile
│   ├── seafile.sh
│   ├── seahub
│   ├── seahub.sh
│   ├── setup-seafile-mysql.py
│   ├── setup-seafile-mysql.sh
│   ├── setup-seafile.sh
│   └── upgrade
├── seafile-server-latest -> seafile-server-6.2.5
├── seahub-data
│   └── avatars

恢复后的seafile-server-latest可能变成了目录,如果你也遇到了,需要重新建立连接,没遇到请跳过

$ rm -rf /data/seafile/seafile-server-latest
$ ln -s /data/seafile/seafile-server-6.2.5 /data/seafile/seafile-server-latest

建立新服务

$ vi /etc/systemd/system/seafile.service

内容(路径、用户等根据实际修改):

[Unit]
Description=Seafile
# add mysql.service or postgresql.service depending on your database to the line below
After=network.target
[Service]
Type=oneshot
ExecStart=/data/seafile/seafile-server-latest/seafile.sh start
ExecStop=/data/seafile/seafile-server-latest/seafile.sh stop
RemainAfterExit=yes
User=root
Group=root
[Install]
WantedBy=multi-user.target
$ vi /etc/systemd/system/seahub.service

内容:

[Unit]
Description=Seafile hub
After=network.target seafile.service
[Service]
# change start to start-fastcgi if you want to run fastcgi
ExecStart=/data/seafile/seafile-server-latest/seahub.sh start
ExecStop=/data/seafile/seafile-server-latest/seahub.sh stop
User=root
Group=root
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

设置自启动

$ sudo systemctl daemon-reload
$ sudo systemctl enable seafile
$ sudo systemctl enable seahub

安装caddy

之前的文章完全相同,这里就略过了。

Caddy启动之后,访问域名,应该可以使用原账户登陆,官网的手册中还有恢复数据库的步骤,我这里把数据库打包进了文件里,实测无需专门恢复数据库即可使用,也不知道原理上是不是正确,如果你比较谨慎,那还是按照手册中的将数据库恢复一遍吧╮(╯▽╰)╭

参考资料

https://manual-cn.seafile.com/maintain/backup_recovery.html

自动同步的脚本忘记是根据谁的脚本修改的了,当时记在笔记里,后来找不到原地址了,抱歉T_T

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注