返回
Featured image of post 使用rclone备份mastodon数据

使用rclone备份mastodon数据

mastodon数据不怕丢吗

服务器有时间限制 Orz,万一哪天忘记续费全部木大完蛋。还是早点做个备份好。毕竟上面写了蛮多东西也不想丢弃。(想起QQ上写好多说说,当年….. 啊 黑历史 之后看见写的简直无语凝噎,都是无病呻吟的一些什么鬼东西啊!😧

准备

  • 安装好rclone
  • 已经配置完mastodon ps:当然看着教程随便设置同步位置也可以的

编写备份脚本

注意 : 这是源代码模式安装Mastodon后的执行备份脚本,因为最近迁移至Docker-Arm64,所以重新写了下Docker下的备份脚本,在下面。

编辑backup.sh

1
sudo nano /opt/onedrive_thy/mastodon-backup/backup.sh

我直接放在Onedrive的mastodon备份目录下咯

写入下面这么多东西:

 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
27
28
29
30
31
32

#!/bin/bash

# Save this file at  /opt/onedrive_thy/mastodon-backup/backup.sh
# Set a cronjob:  0 19 * * * /bin/bash /opt/onedrive_thy/mastodon-backup/backup.sh > /opt/onedrive_thy/mastodon-backup/logs/backup.log 2>&1

#Get the relative path of the backup script
backup_script=$(dirname "$realpath $0")

#Loading the Config
source $backup_script/config.sh

#Clean up old db dump
#rm /home/mastodon/live/dbbackup-*

#Generating a database dump backup
su - mastodon -c "cd /home/mastodon/live && pg_dump -Fc mastodon_production > dbbackup-$(date +"%Y_%m_%d_%I_%M_%p").dump"

#Loading the rclone password
#export RCLONE_CONFIG_PASS=$rclone_config_pass

#Moving the database backup
rclone move /home/mastodon/live/dbbackup-* $rclone_config_name:$onedrive_folder_name/$backup_folder_name

#Copying important files
rclone copy /home/mastodon/live/.env.production $rclone_config_name:/$onedrive_folder_name/$backup_folder_name
rclone copy /var/lib/redis/dump.rdb $rclone_config_name:/$onedrive_folder_name/$backup_folder_name
#rclone copy /etc/elasticsearch/jvm.options $rclone_config_name:$onedrive_folder_name/$backup_folder_name
    
#Remove dump files
rm /home/mastodon/live/dbbackup-*

编写完成后使用Ctrl+X退出,按Y保存。

  • PS : 因为我没有开全文搜索引擎 elasticsearch所以注释掉咯
  • PSS : 每个人的路径不一样,自己找找呗 😆

编辑config.sh

1
sudo nano /opt/onedrive_thy/mastodon-backup/config.sh

写入以下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash

# Save this file at  /opt/onedrive_thy/mastodon-backup/config.sh
# Replace the values below with your onedrive config

rclone_config_name="thy_disk"
# rclone_config_pass="yourpassword"
onedrive_folder_name=Data/Linux/VPS/Backups/TencentCloud/mastodon-backup
backup_folder_name="$(date +"%Y_%m_%d_%I_%M_%p")"

编写完成后使用Ctrl+X退出,按Y保存。

脚本检查

全部编写完成后运行一次:

1
bash /opt/onedrive_thy/mastodon-backup/backup.sh > /opt/onedrive_thy/mastodon-backup/logs/backup.log 2>&1

这一步会卡一会儿执行程序,在执行完成后查询日志 :

1
cat /opt/onedrive_thy/mastodon-backup/logs/backup.log

或者直接登录网页或者win上的Onedrive查看(这好方便!

配置定期备份

编写定时任务:

1
crontab -e

写入下面这一坨,以后每天的凌晨5点(5点不是凌晨了叭)自动执行备份任务:

1
2
0 5 * * * bash /opt/onedrive_thy/mastodon-backup/backup.sh > /opt/onedrive_thy/mastodon-backup/logs/backup.log 2>&1

编写完成后使用Ctrl+X退出,按Y保存。

Docker下的备份脚本 | 2022-02-18 update

首先要明确自己需要备份哪些内容

按照优先级如下分类

  • .env.production 文件
  • 数据库备份
  • 嘟文图片
  • 站点图片
  • 外站数据

编写脚本mastodon-docker-backup.sh

 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
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
#Get the relative path of the backup script
backup_script=$(dirname "$realpath $0")

#Loading the Config
#source $backup_script/config.sh

rclone_config_name="thy_disk"
# rclone_config_pass="yourpassword"
onedrive_folder_name=Data/Linux/VPS/Backups/OracleCloud/mastodon-backup
backup_folder_name="$(date +"%Y_%m_%d_%I_%M_%p")"


mastodon_folder=/home/mastodon/mastodon
#docker ps | grep mastodon_db | awk '{print $1}'
nowtime=$(date +%F)
SQL_FILE=sql_$nowtime.dump



cp ${mastodon_folder}/.env.production ${mastodon_folder}/backup/.env.production 


#Clean up old db dump
rm ${mastodon_folder}/backup/sql*


#Generating a database dump backup
cd ${mastodon_folder} && docker exec -it  mastodon-db-1 /bin/bash -c "pg_dump -U postgres -Fc mastodon_production -f /home/${SQL_FILE}" && docker cp mastodon-db-1:/home/${SQL_FILE} ${mastodon_folder}/backup/${SQL_FILE}

rclone move ${mastodon_folder}/backup/${SQL_FILE} $rclone_config_name:$onedrive_folder_name/$backup_folder_name

rclone copy ${mastodon_folder}/.env.production $rclone_config_name:/$onedrive_folder_name/$backup_folder_name

rclone copy ${mastodon_folder}/redis/dump.rdb $rclone_config_name:/$onedrive_folder_name/$backup_folder_name

echo 'OK'

因为暂时对图片备份没有需求所以先咕咕 (太大了叭!!

完成咯

这写完之后,大概服务器上任意什么位置的文件都可以使用Onedrive来备份了叭。尤其是我那些经常忘记的小容量VPS上的文件,那天不注意就没了。

赞美巨硬!!🎉🎉🎉

共书写了23.4k字·共 21篇文章
Built with Hugo
Theme Stack designed by Jimmy