Backup in Linux
Tags: linux
Published: 2023-02-05 14:10:03



> Backup refers to the retrieval of the original data stored in the system in preparation for possible accidents during service operation

## Why do we use Backup System?

If the operating system fails to provide normal service to the client due to a problem, it is best to provide continuous service to the client through rapid recovery. The fastest and most effective way to solve this problem is to import the Backup data stored in the Backup System and perform a recovery operation on the operating system.

For these reasons, the Backup System should continue to operate.
It becomes an essential element for the stability and effective data management of the entire system.

Backup data is important to maintain the best data at all times through periodic operations.

It is recommended to store the actual service on a separate storage medium rather than the server that operates it.

## 3 Types of Backup

1. `tar`Backup
2. `dd` Backup
3. `rsync` Backup

## tar Backup

> Tar Backup allows us to create backup files in the ‘zip’ format. We can extract this zipped file to any directory we want to ‘recover’ from the backup file.
> 

### Syntax

####For Backing up

`tar cvfzp [BackupData name] [Backup option] [Backup target]`

####For recovery 

`tar xvfzp [BackupData name] [-C option] [extract location]`

### Options

| 옵션 | 설명 |
| --- | --- |
| c | tar 파일 생성 |
| z | tar + gzip |
| f | tar로 생성되는 데이터 형식이 파일인 것을 나타낸다 |
| v | tar 작업 진행 결과를 화면에 표시한다 |
| p | tar로 묶이는 데이터에 대한 퍼미션을 보존한다는 의미가 된다 |
| x | tar 파일 해제 |
| C | tar로 묶인 데이터를 해제할 경로를 지정하는데 사용된다 |

### Example

```bash
#Backing up the file

[root@Linux-1 /]# cd /
[root@Linux-1 /]# mkdir /backup
[root@Linux-1 /]# tar cvfzp /backup/backup.tar.gz --exclude=/var/cache /var
[root@Linux-1 /]# ls -ld /backup/backup.tar.gz 
-rw-r--r-- 1 root root 27257564  1월 26 10:26 /backup/backup.tar.gz

#Recovering from the backup file

[root@Linux-1 /]# tar xvfzp /backup/backup.tar.gz -C /home/itbank
[root@Linux-1 /]# ls -l /home/itbank
합계 0
drwxr-xr-x 18 root root 254  1월 10 10:52 var
```

## dd Backup

> `dd` or ‘Disk Dump’ is a process to backup an entire copy of a hard disk to another hard disk connected to the same system.
> 

### Syntax

`dd if=[복제할 대상 디스크 이름] of=[복제 데이터를 저장 할 디스크 이름] bs=[작업 단위]`

<aside>
👉 Here

if : 입력 파일
of : 출력 파일
bs : 읽기, 쓰기 작업 단위

</aside>

### Example

```bash
[root@Linux-1 /]# dd if=/dev/sda of=/dev/sdb bs=512
[root@Linux-1 /]# fdisk -l /dev/sdb
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 83886079 40893440 8e Linux LVM
```

## rsync Backup

> `rsync` is a command-line tool for **copying files and directories between local and remote systems** that should be in every Linux sysadmin's toolbox
> 

### Syntax

####For sending the data to the destination

`rsync [옵션] [source] [user@IP]:[destination]` 

####For receiving or getting the data from the destination host

`rsync [옵션] [user@IP]:[source] [destination]` 

### Options

| 옵션 | 설명 |
| --- | --- |
| v | 작업 과정을 자세히 표시 ( Verbosity ) |
| z | 파일 복사 시 압축을 진행 ( Compress ) |
| a | Archive 모드로 복사를 진행 ( 복사되는 데이터의 기존 정보를 전부 유지하며 복사를 진행) |
| l | 크기와 시간정보가 일치하는 파일또한 복사대상으로 설정 (기존 파일에 덮어쓰기 진행) |
| h | 파일 복사 결과 출력 |

### Example

```bash
## Data 보내기
# 192.168.1.2 Server
[root@Linux-2 ~]# mkdir /backup

# 192.168.1.1 Server

[root@Linux-1 ~]# mkdir /test
[root@Linux-1 ~]# touch /test/A /test/B /test/C
[root@Linux-1 ~]# rsync -avzh /test root@192.168.1.2:/backup
root@192.168.1.2's password: 
sending incremental file list
test/
test/A
test/B
test/C

# 192.168.1.2 Server
[root@Linux-2 ~]# ls -lR /backup
합계 0
drwxr-xr-x. 2 root root 33 3월 9 21:53 test

[root@Linux-2 ~]# ls -lR /backup/test
합계 0
-rw-r--r-- 1 root root 0 3월 9 20:30 A
-rw-r--r-- 1 root root 0 3월 9 20:30 B
-rw-r--r-- 1 root root 0 3월 9 20:30 C

## Data 가져오기
# 192.168.1.1 Server
[root@Linux-1 ~]# rsync -avzh root@192.168.1.2:/backup/test /backup
root@192.168.1.2 password:
receiving incremental file list
test/
test/A
test/B
test/C

[root@Linux-1 ~]# ls -lR /backup
합계 0
drwxr-xr-x. 2 root root 33 3월 9 21:53 test

[root@Linux-1 ~]# ls -lR /backup/test
합계 0
-rw-r--r-- 1 root root 0 3월 9 20:30 A
-rw-r--r-- 1 root root 0 3월 9 20:30 B
-rw-r--r-- 1 root root 0 3월 9 20:30 C
```

# Practical

## using ‘tar’

### **Tar를 이용 한 “Full Backup”**

- 절대 경로를 이용한 Tar Backup
    
    ```bash
    # Creating the backup file
    [root@Linux-1 ~]# tar cvfzp /backup/var_log.tar.gz --exclude=/var/log/vmware* /var/log
    
    # Removing the /var/log
    [root@Linux-1 ~]# rm -rf /var/log
    
    # Reovering from the backup file
    [root@Linux-1 ~]# tar xvfzp /backup/var_log.tar.gz -C /
    ```
    

- 상대 경로를 이용 한 Tar Backup
    
    ```bash
    [root@Linux-1 ~]# cd /var/log
    [root@Linux-1 log]# tar cvfzp /backup/var_log.tar.gz --exclude=/var/log/vmware* ./*
    [root@Linux-1 ~]# cd ~
    [root@Linux-1 ~]# rm -rf /var/log
    [root@Linux-1 ~]# mkdir /var/log
    [root@Linux-1 ~]# tar xvfzp /backup/var_log.tar.gz -C /var/log
    ```
    

### **Tar를 이용 한 “증분 Backup”**

<aside>
※ 증분 Backup의 경우 Archive 파일의 이름은 tgz로 지정하고, snapshot 파일을 생성 후 작업을 진행한다. 

※ 증분 Backup의 경우에도 최초 1회는 Full Backup으로 진행 된다.

※ 증분 Backup 작업시에는 옵션의 순서가 매우 중요하며, 반드시 "f" 옵션은 가장 마지막에 위치하는 것이 좋다

</aside>

<aside>
※ snapshot 파일의 역할은 현재 Backup이 완료 된 데이터의 목록을 저장하고 있는 역할

※ snapshot 파일을 이용하여 현재 Backup이 완료 된 데이터와 완료되지 않은 데이터를 구분할 수 있게된다.

</aside>

- We will first create `/test` directory then create 2 text files in it.
    
    ```bash
    [root@Linux-1 ~]# mkdir /test
    [root@Linux-1 ~]# echo "1" > /test/test1.txt
    [root@Linux-1 ~]# echo "2" > /test/test2.txt
    
    [root@Linux-1 ~]# cd /test
    
    [root@Linux-1 test]# ll
    합계 8
    -rw-r--r-- 1 root root 2  1월 26 13:27 test1.txt
    -rw-r--r-- 1 root root 2  1월 26 13:27 test2.txt
    ```
    

- Using the `tar` command to create a backup file for these `.txt`
    
    ```bash
    [root@Linux-1 test]# tar -g /backup/backup-0.snap -zcvpf /backup/backup-0.tgz ./*
    ./test1.txt
    ./test2.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz 
    
    # Checking the contents in the backup-0.tgz
    [root@Linux-1 test]# tar tf /backup/backup-0.tgz
    ./test1.txt
    ./test2.txt
    ```
    
- Adding another text file in our backup file,
    
    ```bash
    [root@Linux-1 test]# echo "3" > /test/test3.txt
    [root@Linux-1 test]# tar -g /backup/backup-0.snap -zcvpf /backup/backup-1.tgz ./*
    ./test3.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.tgz 
    
    # Checking the contents in the backup-1.tgz
    [root@Linux-1 test]# tar tf /backup/backup-1.tgz
    ./test3.txt
    ```
    

- Adding another text file in our backup file,
    
    ```bash
    [root@Linux-1 test]# echo "4" > /test/test4.txt
    [root@Linux-1 test]# tar -g /backup/backup-0.snap -zcvpf /backup/backup-2.tgz ./*
    ./test4.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.tgz  backup-2.tgz  
    
    # Checking the contents in the backup-2.tgz
    [root@Linux-1 test]# tar tf /backup/backup-2.tgz 
    ./test4.txt
    ```
    

### **증분 Backup 데이터 복원**

<aside>
※ 차등 Backup 진행 시 기존 snapshot 파일을 복사하여 새로운 snapshot 파일을 생성 후 Backup을 진행한다.

※ 차등 Backup 진행 후 복사 한 snapshot 파일은 다시 삭제작업을 진행

※ 차등 Backup의 개념은 최초 1회 Full Backup 진행, 이후 작업은 증분 Backup을 수행하는데 새로 증가 된 데이터에 대해서맊 Full Backup을 진행하는 개념

※ 즉 새로운 Backup Archive 파일을 계속해서 맊들 필요가 없으므로, 최초 Full Backup Archive 파일과 증가분에 대한 Full Backup을 진행 한 Archive 파일만 존재하면 된다.

</aside>

- To begin our test, we need to delete the `/test` directory and create a new `/test` directory
    
    ```bash
    [root@Linux-1 test]# cd ~
    [root@Linux-1 ~]# rm -rf /test
    [root@Linux-1 ~]# mkdir /test
    ```
    

- We will use the `tar` command and the `-C` option to recover from the backup file
    
    ```bash
    [root@Linux-1 ~]# tar xvzpf /backup/backup-0.tgz -C /test
    ./test1.txt
    ./test2.txt
    
    [root@Linux-1 ~]# ls /test
    test1.txt  test2.txt
    ```
    

- We will follow with the rest of the backup files
    
    ```bash
    [root@Linux-1 ~]# tar xvzpf /backup/backup-1.tgz -C /test
    ./test3.txt
    
    [root@Linux-1 ~]# tar xvzpf /backup/backup-2.tgz -C /test
    ./test4.txt
    
    [root@Linux-1 ~]# ls /test
    test1.txt  test2.txt  test3.txt  test4.txt
    ```
    

We can remove the `/backup` and `/test` directory after the test.

### **Tar를 이용 한 “차등 Backup”**

<aside>
※ 차등 Backup 진행 시 기존 snapshot 파일을 복사하여 새로운 snapshot 파일을 생성 후 Backup을 진행한다.

※ 차등 Backup 진행 후 복사 한 snapshot 파일은 다시 삭제작업을 진행

※ 차등 Backup의 개념은 최초 1회 Full Backup 진행, 이후 작업은 증분 Backup을 수행하는데 새로 증가 된 데이터에 대해서만 Full Backup을 진행하는 개념

※ 즉 새로운 Backup Archive 파일을 계속해서 만들 필요가 없으므로, 최초 Full Backup Archive 파일과 증가분에 대한 Full Backup을 진행 한 Archive 파일만 존재하면 된다

</aside>

- We will first create the `/test` directory and add 2 text files in it.
    
    ```bash
    [root@Linux-1 ~]# mkdir /test
    [root@Linux-1 ~]# echo "1" > /test/test1.txt
    [root@Linux-1 ~]# echo "2" > /test/test2.txt
    
    [root@Linux-1 ~]# cd /test
    
    [root@Linux-1 test]# ll
    합계 8
    -rw-r--r-- 1 root root 2  1월 26 14:52 test1.txt
    -rw-r--r-- 1 root root 2  1월 26 14:52 test2.txt
    ```
    

- We will again create a snapshot file and the `.tgz` backup file
    
    ```bash
    [root@Linux-1 test]# tar -g /backup/backup-0.snap -zcvpf /backup/backup-0.tgz ./*
    ./test1.txt
    ./test2.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz
    
    # Checking the contents inside the backup-0.tgz
    [root@Linux-1 test]# tar tf /backup/backup-0.tgz
    ./test1.txt
    ./test2.txt
    ```
    

- Copying the snapshot file at the same location with a different name
    
    ```bash
    [root@Linux-1 test]# cp /backup/backup-0.snap /backup/backup-1.snap
    ```
    

- Creating a new text file and backing it up using the new snapshot file name
    
    ```bash
    [root@Linux-1 test]# echo "3" > /test/test3.txt
    [root@Linux-1 test]# tar -g /backup/backup-1.snap -zcvpf /backup/backup-1.tgz ./*
    ./test3.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.snap  backup-1.tgz
    
    [root@Linux-1 test]# tar tf /backup/backup-1.tgz
    ./test3.txt
    ```
    

- Deleting the new snapshot file that we created.
    
    ```bash
    [root@Linux-1 test]# rm -rf /backup/backup-1.snap
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.tgz
    ```
    

- Again, copying the backup snapshot file into the same directory
    
    ```bash
    [root@Linux-1 test]# cp /backup/backup-0.snap /backup/backup-1.sn
    ```
    

- Creating a new text file and making a backup tar for that text using the snapshot
    
    ```bash
    [root@Linux-1 test]# echo "4" > /test/test4.txt
    
    [root@Linux-1 test]# tar -g /backup/backup-1.snap -zcvpf /backup/backup-1.tgz ./*
    ./test3.txt
    ./test4.txt
    
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.snap  backup-1.tgz
    
    [root@Linux-1 test]# tar tf /backup/backup-1.tgz 
    ./test3.txt
    ./test4.txt
    ```
    

- Again removing the new snapshot file
    
    ```bash
    [root@Linux-1 test]# rm -rf /backup/backup-1.snap
    [root@Linux-1 test]# ls /backup
    backup-0.snap  backup-0.tgz  backup-1.tgz
    ```
    

- We can repeat the above step with the new `test5.txt` file
    
    ```bash
    [root@Linux-1 test]# cp /backup/backup-0.snap /backup/backup-1.snap
    
    [root@Linux-1 test]#  echo "5" > /test/test5.txt
    [root@Linux-1 test]#  tar -g /backup/backup-1.snap -zcvpf /backup/backup-1.tgz ./*
    ./test3.txt
    ./test4.txt
    ./test5.txt
    
    [root@Linux-1 test]# ls /backup/
    backup-0.snap  backup-0.tgz  backup-1.snap  backup-1.tgz
    
    [root@Linux-1 test]# tar tf /backup/backup-1.tgz 
    ./test3.txt
    ./test4.txt
    ./test5.txt
    
    [root@Linux-1 test]# rm -rf /backup/backup-1.snap 
    [root@Linux-1 test]# ls /backup/
    backup-0.snap  backup-0.tgz  backup-1.tgz
    ```
    
    We can confirm that the same `.tgz` file contains all the test text files as we used the same snapshot files for their backup.
    
     
    

### **증분 Backup 데이터 복원**

- We will delete the `/test` directory first and then create a new `/test` directory
    
    ```bash
    [root@Linux-1 test]# cd ~
    [root@Linux-1 ~]# rm -rf /test
    [root@Linux-1 ~]# mkdir /test
    ```
    

- Recovering from the Full backup tar file inside our backup directory to our new `/test` directory
    
    ```bash
    [root@Linux-1 ~]# tar xvzpf /backup/backup-0.tgz -C /test
    ./test1.txt
    ./test2.txt
    
    [root@Linux-1 ~]# ls /test
    test1.txt  test2.txt
    ```
    

- Recovering from the 차증 Backup file inside our backup directory
    
    ```bash
    [root@Linux-1 ~]# tar xvzpf /backup/backup-1.tgz -C /test
    ./test3.txt
    ./test4.txt
    ./test5.txt
    
    [root@Linux-1 ~]# ls /test
    test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
    ```
    
    <aside>
    💡 Important point is that we always recover the “Full backup” file first before recovering any other backup file.
    
    </aside>
    

## using ‘dd’

<aside>
💡 Need a new 40GB HDD for the VM.

</aside>

- We will make a new partition and format that with xfs.
    
    ```bash
    [root@Linux-1 ~]# fdisk /dev/sdb
    [root@Linux-1 ~]# mkfs.xfs /dev/sdb1
    ```
    

- Creating 4 new files for testing
    
    ```bash
    [root@Linux-1 ~]# touch ./a ./b ./c ./d
    ```
    

- Copying the whole /dev/sda to our /dev/sdb1
    
    ```bash
    [root@Linux-1 ~]# dd if=/dev/sda of=/dev/sdb bs=64k
    327680+0 records in
    327680+0 records out
    21474836480 bytes (21 GB) copied, 21.3336 s, 1.0 GB/s
    ```
    

- We can confirm the results using
    
    ```bash
    [root@Linux-1 ~]# fdisk -l /dev/sdb
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1   *        2048     2099199     1048576   83  Linux
    /dev/sdb2         2099200    41943039    19921920   8e  Linux LVM
    ```
    

- Now, from our main computer, we will delete the actual VM HDD(`/dev/sda`) and use the `/dev/sdb` to boot
    
   
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdc4xzi04jvtcsl3x2nw.png)
Delete this .vmdk file    

- Deleting the HDDs from the VM.
    
   
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90i6dhbhcwbpftdzfle7.png)
    

- Adding an existing virtual disk.
    
  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg2ktqxgxdp8t7o48xrl.png)
   

- We made this HDD the backup for our Linux system so we will choose this VM’s location in the setup
    
    
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eyu62b3q7ip2hw4jjjx.png)
   

- Once we login to the Linux,
    
    ```bash
    [root@Linux-1 ~]# ifconfig
    ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.128  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::5640:44e3:92c3:1cdc  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:18:62:a4  txqueuelen 1000  (Ethernet)
    ```
    

- We can also check our test files present in this drive.
    
    ```bash
    [root@Linux-1 ~]# ls -ld ./a ./b ./c ./d
    -rw-r--r-- 1 root root 0  1월 26 21:41 ./a
    -rw-r--r-- 1 root root 0  1월 26 21:41 ./b
    -rw-r--r-- 1 root root 0  1월 26 21:41 ./c
    -rw-r--r-- 1 root root 0  1월 26 21:41 ./d
    ```
    

## using ‘rsync’

- We need to make a test directory and some files in it.
    
    ```bash
    [root@Linux-1 ~]# mkdir /backup
    [root@Linux-1 ~]# ll /backup/
    합계 0
    -rw-r--r-- 1 root root 0  1월 26 21:41 a
    -rw-r--r-- 1 root root 0  1월 26 21:41 b
    -rw-r--r-- 1 root root 0  1월 26 21:41 c
    -rw-r--r-- 1 root root 0  1월 26 21:41 d
    ```
    

- Before using `rsync` we need to make sure that we have the rsync package
    
    ```bash
    [root@Linux-1 ~]# rpm -qa | grep rsync
    [root@Linux-1 ~]# yum -y install rsync
    ```
    

- After the installation, we have to do some settings inside the `.conf` file for rsync
    
    ```bash
    [root@Linux-1 ~]# vi /etc/rsyncd.conf
    
    [test]                          = 사용할 rsync 서비스 이름
    path = /backup/                 = 데이터 원본 경로
    comment = rsync_test            = 코멘트
    uid = root                      = 권한 사용자
    gid = root                      = 권한 그룹
    user chroot = yes
    read only = yes
    hosts allow = 192.168.1.129     = rsync 클라이언트 IP, localhost일 경우 입력 x
    max connections = 10            = 연결 수명 최대 길이
    timeout = 30                    = 미연결시 끊기는 시간
    ```
    

- Restarting and enabling the daemon for rsync after the conf file setting
    
    ```bash
    [root@Linux-1 ~]# systemctl restart rsyncd
    [root@Linux-1 ~]# systemctl enable rsyncd
    ```
    

- From Linux-2, we need to prepare an empty directory
    
    ```bash
    ➜  ~ mkdir /backup                                                                             
    ➜  ~ ls -l /backup
    total 0
    ```
    

- Backing up all the files inside Linux-1 directory to Linux-2 directory
    
    ```bash
    [root@Linux-1 ~]# rsync -avzh /backup root@192.168.1.129:/backup
    root@192.168.1.129's password: 
    sending incremental file list
    backup/
    backup/a
    backup/b
    backup/c
    backup/d
    ```
    

- If we check in Linux-2,
    
    ```bash
    ➜  ~ ls -l /backup
    total 0
    drwxr-xr-x 2 root root 42 Jan 26 22:05 backup
    ➜  ~ ls -l /backup/backup 
    total 0
    -rw-r--r-- 1 root root 0 Jan 26 21:41 a
    -rw-r--r-- 1 root root 0 Jan 26 21:41 b
    -rw-r--r-- 1 root root 0 Jan 26 21:41 c
    -rw-r--r-- 1 root root 0 Jan 26 21:41 d
    ```
    

- Or we can get something from Linux-1 as Linux-2
    
    ```bash
    # From Linux-1
    [root@Linux-1 ~]# touch /backup/e /backup/f
    [root@Linux-1 ~]# ls -l /backup/
    합계 0
    -rw-r--r-- 1 root root 0  1월 26 21:41 a
    -rw-r--r-- 1 root root 0  1월 26 21:41 b
    -rw-r--r-- 1 root root 0  1월 26 21:41 c
    -rw-r--r-- 1 root root 0  1월 26 21:41 d
    -rw-r--r-- 1 root root 0  1월 26 22:16 e
    -rw-r--r-- 1 root root 0  1월 26 22:16 f
    
    # From Linux-2
    ➜  ~ rsync -avzh root@192.168.1.128:/backup /backup
    root@192.168.1.128's password: 
    receiving incremental file list
    backup/
    backup/e
    backup/f
    
    # Checking /backup in Linux-2
    ➜  ~ ll /backup/backup 
    total 0
    -rw-r--r-- 1 root root 0 Jan 26 21:41 a
    -rw-r--r-- 1 root root 0 Jan 26 21:41 b
    -rw-r--r-- 1 root root 0 Jan 26 21:41 c
    -rw-r--r-- 1 root root 0 Jan 26 21:41 d
    -rw-r--r-- 1 root root 0 Jan 26 22:16 e
    -rw-r--r-- 1 root root 0 Jan 26 22:16 f
    ```