Mastodon Rclone 自动异地备份脚本
Rclone是一个命令行程序,用于管理云存储的文件。它是一个功能丰富的云供应商网络存储界面的替代品。超过40个云存储产品支持Rclone,包括S3对象存储、商业和消费者文件存储服务,以及标准传输协议。
步驟
使用rclone config
来添加新的配置,并为你的rclone配置设置密码。
新建目录
mkdir -p /opt/mastodon-backup/ mkdir -p /opt/mastodon-backup/logs mkdir -p /home/mastodon/S3_backup
安装7-Zip
apt install p7zip-full
添加备份脚本到 /opt/mastodon-backup/backup.sh
,设置一个cron job定时运行(如 0 3 * * * bash /opt/mastodon-backup/backup.sh > /opt/mastodon-backup/logs/backup.log 2>&1
)
可执行权限
chmod +x /opt/mastodon-backup/backup.sh
备份脚本
#!/bin/bash umask 027 # Replace the values below with your Rclone config rclone_config_name="scaleway" rclone_config_pass="yourpassword" s3_bucket_name="yourbucketname" backup_time="$(date +"%Y_%m_%d_%I_%M_%p")" # Remove old backup directory rm -rf /home/mastodon/S3_backup/* rm -rf /home/mastodon/live/db_*.dump # Create temporary backup directory backup_file_name=${backup_time}_$RANDOM backup_direcotry=/home/mastodon/S3_backup/${backup_file_name} [ -e ${backup_direcotry} ] || mkdir -p ${backup_direcotry} # Generate a database dump backup su - mastodon -c "cd /home/mastodon/live && pg_dump -Fc mastodon_production > db_${backup_time}.dump" # Move the database dump mkdir -p $backup_direcotry/database_dump/ && mv /home/mastodon/live/db_${backup_time}.dump $backup_direcotry/database_dump/ # Copy files mkdir -p $backup_direcotry/mastodon_env/ && cp /home/mastodon/live/.env.production $backup_direcotry/mastodon_env/backup.env.production mkdir -p $backup_direcotry/redis/ && cp /var/lib/redis/dump.rdb $backup_direcotry/redis/ mkdir -p $backup_direcotry/nginx/ && cp /etc/nginx/sites-available/* $backup_direcotry/nginx/ mkdir -p $backup_direcotry/elasticsearch/ && cp /etc/elasticsearch/jvm.options $backup_direcotry/elasticsearch/ mkdir -p $backup_direcotry/backup_script/ && cp /opt/mastodon-backup/backup.sh $backup_direcotry/backup_script/ # Compress 7z a /home/mastodon/S3_backup/${backup_file_name}.7z $backup_direcotry # Load the rclone password export RCLONE_CONFIG_PASS=$rclone_config_pass # Upload the backup rclone move /home/mastodon/S3_backup/${backup_file_name}.7z $rclone_config_name:$s3_bucket_name/ # Remove backup directory rm -rf /home/mastodon/S3_backup/*
0