1

PostgreSQL のベース バックアップを実行しようとしています。私のコードは、Ruby で実行される単純な Bash です。ベースバックアップを実行し、それを /tmp/(DIR) に tar します。

 file "/tmp/basebackup.sh" do
 user "postgres"
 mode 0755
 content <<-EOH
     su - postgres
     export PATH=$PATH:/usr/lib/postgresql/9.2/bin
     su -c "/usr/lib/postgresql/9.2/bin/initdb -D /tmp/pg_data/ -s --no-locale --encoding=UTF8"  - postgres
     su -c "/usr/lib/postgresql/9.2/bin/pg_ctl -D /tmp/pg_data -l logfile start" - postgres
     su -c "/usr/lib/postgresql/9.2/bin/psql -c "SELECT pg_start_backup('initial backup for SR')" template1" - postgres
     tar -cvf pg_base_backup.tar /tmp/pg_data
     su -c "/usr/lib/postgresql/9.2/bin/psql -c "SELECT pg_stop_backup()" template1" - postgres
     exit 0
 EOH
 end

 execute "/tmp/basebackup.sh"  do
      ignore_failure true
      action :run
 end

これを行うより良い方法はありますか?

4

1 に答える 1

2

Ruby は__END__、実行可能なスクリプトの終了をマークするために使用される which をサポートしています。それ以降は、実行中のプログラムからは見えませんが、通常のファイルのように、DATAファイル ハンドルを使用してアクセスできます。read次のコードを試して、動作することを確認してください。

File.open('trashme.txt', 'w') do |content|
  content << DATA.read
end

__END__
su - postgres
export PATH=$PATH:/usr/lib/postgresql/9.2/bin
su -c "/usr/lib/postgresql/9.2/bin/initdb -D /tmp/pg_data/ -s --no-locale --encoding=UTF8"  - postgres
su -c "/usr/lib/postgresql/9.2/bin/pg_ctl -D /tmp/pg_data -l logfile start" - postgres
su -c "/usr/lib/postgresql/9.2/bin/psql -c "SELECT pg_start_backup('initial backup for SR')" template1" - postgres
tar -cvf pg_base_backup.tar /tmp/pg_data
su -c "/usr/lib/postgresql/9.2/bin/psql -c "SELECT pg_stop_backup()" template1" - postgres
exit 0

この機能を使用して、シェル スクリプトを通常の Ruby/chef スクリプトの外に移動し、メンテナンスと明確化を図ります。

于 2013-05-06T04:16:44.473 に答える