2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ESXiでコマンドやスクリプトを定期実行させる方法と仮想マシンのオンラインバックアップ

Posted at

standerdライセンス以上ならvMotionが使えるので何も考えなくてよいが、フリーライセンスの場合は最小限の仮想化以外の機能が使えないため、cronを使ってバックアップを組む。
ESXiにはcrontabコマンドが存在していないため、cronを利用する場合は設定ファイルである/var/spool/cron/crontabs/rootを編集する必要がある。
書式自体は通常のcronの書式で問題ない。

これらの設定は再起動時に毎回クリアされてしまうため、ESXiの再起動後にも設定を有効にする場合は、再起動と同時に上記設定ファイルに設定を再度書き込むようにする必要がある。

事前準備と初期設定

作業はESXi本体にはsshで接続する。
この時、接続はチャレンジレスポンスで行う。

The time and date of this login have been sent to the system logs.

WARNING:
   All commands run on the ESXi shell are logged and may be included in
   support bundles. Do not provide passwords directly on the command line.
   Most tools can prompt for secrets or accept them from standard input.

VMware offers supported, powerful system administration tools.  Please
see www.vmware.com/go/sysadmintools for details.

The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
[root@esxi:~] 

ESXi再起動後も設定を保持させるように、カーネル読み込み時に実行される/etc/rc.local.d/local.shを編集する。

# !/bin/sh

# local configuration options

# Note: modify at your own risk!  If you do/use anything in this
# script that is not part of a stable API (relying on files to be in
# specific places, specific tools, specific output, etc) there is a
# possibility you will end up with a broken system after patching or
# upgrading.  Changes are not supported unless under direction of
# VMware support.

# Note: This script will not be run when UEFI secure boot is enabled.

# --- Apend ---
/bin/echo "0 22 * * * /bin/sh /vmfs/volumes/datastore1/vms_backup/ghettoVCB-master/ghettoVCB.sh -f /vmfs/volumes/d/bin/kill $(cat /var/run/crond.pid)
/bin/busybox crond
# --- end ---

exit 0

--- Apend ---後の一行目にあるように、/var/spool/cron/crontabs/rootに設定を書き込ませるような記述を行う。

バックアップ設定

実際のバックアップは無料ツールのghettoVCBを使用する。
作成していれば不要だが、データストア(ここではdatastore1)にバックアップ用のディレクトリを作る。

[root@esxi:~] cd /vmfs/volumes/datastore1
[root@esxi:/vmfs/volumes/datastore1] mkdir backup
[root@esxi:/vmfs/volumes/datastore1] cd backup

ESXiにはgitコマンドがないので、wgetでmaster.zipを落とす。

[root@esxi:/vmfs/volumes/datastore1/backup] wget https://github.com/lamw/ghettoVCB/archive/master.zip -o ghettoVCB-master.zip
[root@esxi:/vmfs/volumes/datastore1/backup] unzip ghettoVCB-master.zip
Archive:  ghettoVCB-master.zip
   creating: ghettoVCB-master/
  inflating: ghettoVCB-master/README
  inflating: ghettoVCB-master/ghettoVCB-restore.sh
  inflating: ghettoVCB-master/ghettoVCB-restore_vm_restore_configuration_template
  inflating: ghettoVCB-master/ghettoVCB-vm_backup_configuration_template
  inflating: ghettoVCB-master/ghettoVCB.conf
  inflating: ghettoVCB-master/ghettoVCB.sh
[root@esxi:~]  

もしESXiがインターネットに接続されていない場合は事前に作業端末にダウンロードし、SCPなどで転送する。
アップ後、バックアップに関する詳細を設定するためghettoVCB.shを編集する。

# バックアップ先のデータストア(NFSでほかのサーバをマウントしておく)およびフォルダを指定
VM_BACKUP_VOLUME=/vmfs/volumes/nfs/backup

# バックアップファイルのフォーマットを指定
DISK_BACKUP_FORMAT=thin

# バックアップの世代数を設定
VM_BACKUP_ROTATION_COUNT=3

上記が必要最低限の設定で、実際に設定できる項目は多数あるが割愛する。
詳細はここを参照。

次にバックアップ対象とする仮想マシンのリストファイルを作成する。
これはスクリプトの実行時に引数としてファイル名を渡すものなので、ファイル名は任意でよい。
本項はvmlist.txtとして作成し、仮想マシンの名前を記載する。

web0
web1

バックアップの確認とコマンド定期実行の設定

リスト作成後、手動でバックアップを実行してみて動作を確認。
この時、最初にスナップショットが作成されクローンが行われる。

[root@esxi:/vmfs/volumes/datastore1/backup] ./ghettoVCB.sh -f ./vmlist.txt

するとこんな感じに実行される。

Logging output to "/tmp/ghettoVCB-2019-10-22_03-15-01-2133669.log" ...
2019-10-22 03:15:01 -- info: ============================== ghettoVCB LOG START ==============================

2019-10-22 03:15:01 -- info: CONFIG - VERSION = 2019_01_06_4
2019-10-22 03:15:01 -- info: CONFIG - GHETTOVCB_PID = 2133669
2019-10-22 03:15:01 -- info: CONFIG - VM_BACKUP_VOLUME = /vmfs/volumes/datastore1/vms_backup
2019-10-22 03:15:01 -- info: CONFIG - ENABLE_NON_PERSISTENT_NFS = 0
2019-10-22 03:15:01 -- info: CONFIG - VM_BACKUP_ROTATION_COUNT = 2
2019-10-22 03:15:01 -- info: CONFIG - VM_BACKUP_DIR_NAMING_CONVENTION = 2019-10-22_03-15-01
2019-10-22 03:15:01 -- info: CONFIG - DISK_BACKUP_FORMAT = thin
2019-10-22 03:15:01 -- info: CONFIG - POWER_VM_DOWN_BEFORE_BACKUP = 0
2019-10-22 03:15:01 -- info: CONFIG - ENABLE_HARD_POWER_OFF = 0
2019-10-22 03:15:01 -- info: CONFIG - ITER_TO_WAIT_SHUTDOWN = 3
2019-10-22 03:15:01 -- info: CONFIG - POWER_DOWN_TIMEOUT = 5
2019-10-22 03:15:01 -- info: CONFIG - SNAPSHOT_TIMEOUT = 15
2019-10-22 03:15:01 -- info: CONFIG - LOG_LEVEL = info
2019-10-22 03:15:01 -- info: CONFIG - BACKUP_LOG_OUTPUT = /tmp/ghettoVCB-2019-10-22_03-15-01-2133669.log
2019-10-22 03:15:01 -- info: CONFIG - ENABLE_COMPRESSION = 0
2019-10-22 03:15:01 -- info: CONFIG - VM_SNAPSHOT_MEMORY = 0
2019-10-22 03:15:01 -- info: CONFIG - VM_SNAPSHOT_QUIESCE = 0
2019-10-22 03:15:01 -- info: CONFIG - ALLOW_VMS_WITH_SNAPSHOTS_TO_BE_BACKEDUP = 0
2019-10-22 03:15:01 -- info: CONFIG - VMDK_FILES_TO_BACKUP = all
2019-10-22 03:15:01 -- info: CONFIG - VM_SHUTDOWN_ORDER = 
2019-10-22 03:15:01 -- info: CONFIG - VM_STARTUP_ORDER = 
2019-10-22 03:15:01 -- info: CONFIG - RSYNC_LINK = 0
2019-10-22 03:15:01 -- info: CONFIG - BACKUP_FILES_CHMOD = 
2019-10-22 03:15:01 -- info: CONFIG - EMAIL_LOG = 0
2019-10-22 03:15:01 -- info: CONFIG - ENABLE NFS IO HACK = 0

2019-10-22 03:15:04 -- info: Initiate backup for fcserver
2019-10-22 03:15:04 -- info: Creating Snapshot "ghettoVCB-snapshot-2019-10-22" for web0
Option --adaptertype is deprecated and hence will be ignored
Destination disk format: VMFS thin-provisioned
Cloning disk '/vmfs/volumes/datastore0/web0/web0.vmdk'...
Clone: 100% done.
2019-10-22 03:20:21 -- info: Removing snapshot from fcserver ...
2019-10-22 03:20:21 -- info: Backup Duration: 5.28 Minutes
2019-10-22 03:20:21 -- info: Successfully completed backup for fcserver!

2019-10-22 03:20:24 -- info: Initiate backup for preforum
2019-10-22 03:20:24 -- info: Creating Snapshot "ghettoVCB-snapshot-2019-10-22" for preforum
Option --adaptertype is deprecated and hence will be ignored
Destination disk format: VMFS thin-provisioned
Cloning disk '/vmfs/volumes/datastore0/web1/web1.vmdk'...
Clone: 100% done.
2019-10-22 03:26:21 -- info: Removing snapshot from preforum ...
2019-10-22 03:26:21 -- info: Backup Duration: 5.95 Minutes
2019-10-22 03:26:21 -- info: Successfully completed backup for preforum!

2019-10-22 03:26:23 -- info: ###### Final status: All VMs backed up OK! ######

2019-10-22 03:26:23 -- info: ============================== ghettoVCB LOG END ================================

エラーなく実行されたのを確認したらcronに組み込む。
まず、再起動した場合にも設定が消えないように/etc/rc.local.d/local.shに記述。

/bin/echo "0 22 * * * /bin/sh /vmfs/volumes/datastore1/backup/ghettoVCB-master/ghettoVCB.sh -f /vmfs/volumes/datastore1/backup/ghettoVCB-master/vmlist.txt" >> /var/spool/cron/crontabs/root
/bin/kill $(cat /var/run/crond.pid)
/bin/busybox crond

次に、cronに設定。

0 22 * * * /bin/sh /vmfs/volumes/datastore1/backup/ghettoVCB-master/ghettoVCB.sh -f /vmfs/volumes/datastore1/backup/ghettoVCB-master/vmlist.txt

これで日次バックアップの完成。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?