LoginSignup
12
2

More than 1 year has passed since last update.

EC2インスタンス xfsdumpによるバックアップ

Posted at

AMIを別アカウントで共有するのが嫌な場合(そんなケースないかもしれませんが)とか役に立つかもしれないので備忘録的な記事です。。。

1. 前提

  • パーティション:rootパーティション(/)のみ

2. EBSボリューム作成(バックアップ用)

まず、バックアップ対象のEC2インスタンスに割り当てるEBSボリュームをコンソールから作成します。「ボリュームの作成」をクリックします。

image.png

サイズは余裕をもって。割り当てるEC2インスタンスと同じAZを選択して、「ボリュームの作成」をクリックします。
image.png

3. EBSボリュームのアタッチ(バックアップ用)

次に、作成したEBSボリュームをバックアップ対象のEC2インスタンスに割り当てます。作成したEBSボリュームを右クリックして、「ボリュームのアタッチ」をクリックします。
image.png

インタンスIDを選択してアタッチをクリックします。
image.png

対象のEC2インスタンスをクリックして、ブロックデバイスが割り当てられている事を確認します。
image.png

4. バックアップ作成

割り当て後はSSH接続をして下記コマンドを実行していきます。

yum install -y xfsdump

# パーティション作成
fdisk /dev/xvdf

Command (m for help): n  
Partition type:  
   p   primary (0 primary, 0 extended, 4 free)  
   e   extended  
Select (default p): p  
Partition number (1-4, default 1):  
First sector (2048-209715199, default 2048):  
Using default value 2048  
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):  
Using default value 209715199  
Partition 1 of type Linux and of size 100 GiB is set  

Command (m for help): p  

Disk /dev/xvdf: 107.4 GB, 107374182400 bytes, 209715200 sectors  
Units = sectors of 1 * 512 = 512 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk label type: dos  
Disk identifier: 0x662bdfdb  

    Device Boot      Start         End      Blocks   Id  System  
/dev/xvdf1            2048   209715199   104856576   83  Linux  

Command (m for help): a  
Selected partition 1  

Command (m for help): p  

Disk /dev/xvdf: 107.4 GB, 107374182400 bytes, 209715200 sectors  
Units = sectors of 1 * 512 = 512 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk label type: dos  
Disk identifier: 0x662bdfdb  

    Device Boot      Start         End      Blocks   Id  System  
/dev/xvdf1   *        2048   209715199   104856576   83  Linux  

Command (m for help):  
Command (m for help): w  
The partition table has been altered!  

Calling ioctl() to re-read partition table.  
Syncing disks.  
[root@ip-10-0-0-119 ~]#  

# ファイルシステム作成
mkfs.xfs /dev/xvdf1

# ディレクトリ作成 & マウント
mkdir /mnt/new
mount -t xfs /dev/xvdf1 /mnt/new

# xfsdumpによるバックアップ実施
xfsdump -l 0 - /dev/xvda1 | gzip -c /mnt/new/backup.dump.gz

取得したバックアップファイルはscpなどでローカル端末側に保存しておきましょう。

5. EBSボリューム作成(リストア用)

別アカウントにてリストア用のEC2インスタンスに割り当てるようのEBSボリュームを作成し、EC2インスタンスに
割り当てます。(手順は#2,#3と同じでアカウントがちがうだけ)

6. リストア実施

yum install -y xfsdump

# パーティション作成
fdisk /dev/xvdf
# 手順4と同様に実施

# ファイルシステム作成
mkfs.xfs /dev/xvdf1

# ディレクトリ作成 & マウント
mkdir /mnt/new
mount -t xfs /dev/xvdf1 /mnt/new

# リストア 
cd /mnt/new
gzip -dc /tmp/backup.dump.gz | xfsrestore - ./

# proc sysfs devをマウント 
cd /tmp
mount -t proc /proc /mnt/new/proc
mount -t sysfs /sys /mnt/new/sys
mount --bind /dev /mnt/new/dev

# chroot実施
chroot /mnt/new

# grub.cfgを生成
grub2-mkconfig -o /boot/grub2/grub.cfg

# ブロックデバイスのUUIDを記録しておく
blkid | grep xvdf

# UUIDを書き換え
vi /etc/fstab

# システム停止
exit
shutdown -h now

7. EBSボリューム付け替え

EBSボリュームをすべてデタッチします。
後から作成したEBSボリュームをsda1としてアタッチします。

対象EC2インスタンスを起動してログインできれば無事リストア完了です。

12
2
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
12
2