Skip to content

Commit 4368c9b

Browse files
poozaclaude
andcommitted
add: rsync_backup_tool
rsyncで指定ディレクトリをSSH経由で外部サーバーにバックアップするツールを追加。 除外パターンの設定にも対応。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3a9044e commit 4368c9b

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ rake uninstall # cronスクリプトをアンインストール
5050
| postgresql_dump | PostgreSQLのダンプファイルを作成します。 |
5151
| postgresql_snapshot | PostgreSQLのZFSスナップショットを作成・管理します。 |
5252
| reboot_required | システムに再起動が必要かを判定します。 |
53+
| rsync_backup | rsyncでファイルを外部サーバーにバックアップします。 |
5354
| service_restart | 設定されたサービスを再起動します。 |
5455

5556
## 設定
@@ -127,6 +128,14 @@ rake uninstall # cronスクリプトをアンインストール
127128
| --- | --- | --- |
128129
| commands | 実行するtootctlサブコマンドの配列 | [media remove-orphans, ...] |
129130

131+
### rsync_backup
132+
133+
| キー | 説明 | デフォルト |
134+
| --- | --- | --- |
135+
| dest | SSH転送先 (user@host:/path) | user@host:/path/to/backup |
136+
| sources | バックアップ対象ディレクトリの配列 | [/etc, /usr/local/etc, ...] |
137+
| excludes | 除外パターンの配列 | [.git, .zfs] |
138+
130139
## ライセンス
131140

132141
MIT
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module WritersBase
2+
class RsyncBackupTool < Tool
3+
def exec(args = {})
4+
result = {success: [], failure: []}
5+
sources.each do |src|
6+
sync(src, result)
7+
rescue => e
8+
logger.error(tool: underscore, src:, error: e.message.strip)
9+
result[:failure].push(src:, error: e.message.strip)
10+
end
11+
return result
12+
end
13+
14+
def description
15+
return 'rsyncでファイルを外部サーバーにバックアップします。'
16+
end
17+
18+
private
19+
20+
def sync(src, result)
21+
remote_path = "#{dest}#{src}"
22+
logger.info(tool: underscore, src:, dest: remote_path, message: '同期開始')
23+
command = CommandLine.new([
24+
'rsync', '-avz', '--delete',
25+
*excludes.flat_map {|pattern| ['--exclude', pattern]},
26+
"#{src}/",
27+
remote_path
28+
])
29+
return if test?
30+
command.exec
31+
raise command.stderr unless command.status.zero?
32+
logger.info(tool: underscore, src:, dest: remote_path, message: '同期完了')
33+
result[:success].push(src)
34+
end
35+
36+
def dest
37+
return config["/#{underscore}/dest"]
38+
end
39+
40+
def excludes
41+
return config["/#{underscore}/excludes"] || []
42+
end
43+
end
44+
end

config/application.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,20 @@ mastodon_media_cleanup:
7272
- media remove-orphans
7373
- media remove --remote-headers
7474
- preview_cards remove -c 1
75+
rsync_backup:
76+
dest: user@host:/path/to/backup
77+
sources:
78+
- /etc
79+
- /usr/local/etc
80+
- /home/mastodon/repos/mastodon
81+
excludes:
82+
- .git
83+
- .zfs
84+
- .cache
85+
- node_modules
86+
- vendor/bundle
87+
- tmp
88+
- '*.bak'
89+
- '*.log'
90+
- '*.swp'
91+
- '*.tmp'

0 commit comments

Comments
 (0)