Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ rake uninstall # cronスクリプトをアンインストール
| postgresql_dump | PostgreSQLのダンプファイルを作成します。 |
| postgresql_snapshot | PostgreSQLのZFSスナップショットを作成・管理します。 |
| reboot_required | システムに再起動が必要かを判定します。 |
| rsync_backup | rsyncでファイルを外部サーバーにバックアップします。 |
| service_restart | 設定されたサービスを再起動します。 |

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

### rsync_backup

| キー | 説明 | デフォルト |
| --- | --- | --- |
| dest | SSH転送先 (user@host:/path) | user@host:/path/to/backup |
| sources | バックアップ対象ディレクトリの配列 | [/etc, /usr/local/etc, ...] |
| excludes | 除外パターンの配列 | [.git, .zfs] |

## ライセンス

MIT
44 changes: 44 additions & 0 deletions app/lib/writers_base/tool/rsync_backup_tool.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module WritersBase
class RsyncBackupTool < Tool
def exec(args = {})
result = {success: [], failure: []}
sources.each do |src|
sync(src, result)
rescue => e
logger.error(tool: underscore, src:, error: e.message.strip)
result[:failure].push(src:, error: e.message.strip)
end
return result
end

def description
return 'rsyncでファイルを外部サーバーにバックアップします。'
end

private

def sync(src, result)
remote_path = "#{dest}#{src}"
logger.info(tool: underscore, src:, dest: remote_path, message: '同期開始')
command = CommandLine.new([
'rsync', '-avz', '--delete',
*excludes.flat_map {|pattern| ['--exclude', pattern]},
"#{src}/",
remote_path
])
return if test?
command.exec
raise command.stderr unless command.status.zero?
logger.info(tool: underscore, src:, dest: remote_path, message: '同期完了')
result[:success].push(src)
end

def dest
return config["/#{underscore}/dest"]
end

def excludes
return config["/#{underscore}/excludes"] || []
end
end
end
19 changes: 18 additions & 1 deletion config/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package:
- tkoishi@b-shock.co.jp
license: MIT
url: https://github.com/THE-POWERNEWS/writersbase-tools
version: 1.4.0
version: 1.4.1
ruby:
jit: false
bin: /usr/bin/ruby3.3
Expand Down Expand Up @@ -72,3 +72,20 @@ mastodon_media_cleanup:
- media remove-orphans
- media remove --remote-headers
- preview_cards remove -c 1
rsync_backup:
dest: user@host:/path/to/backup
sources:
- /etc
- /usr/local/etc
- /home/mastodon/repos/mastodon
excludes:
- .git
- .zfs
- .cache
- node_modules
- vendor/bundle
- tmp
- '*.bak'
- '*.log'
- '*.swp'
- '*.tmp'