侧边栏壁纸
  • 累计撰写 99 篇文章
  • 累计创建 54 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

DD command script

FlyingEagle
2025-02-17 / 0 评论 / 0 点赞 / 208 阅读 / 6,647 字

DD rebuild boot disk with brand new OS

bash <(wget --no-check-certificate -qO- 'https://raw.githubusercontent.com/MoeClub/Note/master/InstallNET.sh') -d 11 -v 64 -p "password" -port "22"
curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O reinstall.sh $_
bash reinstall.sh debian 12

  • use your own img
bash reinstall.sh dd --img="https://your-remote-url/backup.img.gz"
  • if the download need authen username:Bob, password:hiccup
bash reinstall.sh dd --img="http://Bob:hiccup@<your-ip>/backup.img.gz"
bash <(wget --no-check-certificate -qO- 'https://rp.h6.work/https://raw.github.com/leitbogioro/Tools/master/Linux_reinstall/InstallNET.sh') -debian 12 -pwd 'dasadada.'

DD disk speed test

dd if=/dev/zero of=test bs=8k count=1000000 status=progress
  • run in the directory associated to disk

DD system disk backup

  • use “lsblk” check the backup disk name, “/dev/sda
dd if=/dev/sda of=/mnt/debian_backup.img bs=4M status=progress && sync 
  • if= → Input file (system disk)
  • of= → Output file (backup location on /mnt)
  • bs=4M → Read/write in 4MB chunks for speed
  • status=progress → Shows progress
  • sync → Ensures all data is written, flushes all write operations from the buffer to the disk, preventing incomplete or corrupted backups.

DD compress to remote path with ssh

dd if=/dev/sda bs=4M | gzip | ssh user@remote "cat > backup.img.gz"

or

dd if=/dev/sda bs=4M | gzip | ssh user@remote "dd of=backup.img.gz"
  • back to the user home dir, such as root user back to the /root dir
dd if=/dev/sda bs=4M | gzip | ssh root@remote "cat > /tmp/backup.img.gz"
  • it will be saved in /tmp/ (world-readable, but deleted on reboot)

compressed backup to save space (~50% smaller if there’s empty space):

dd if=/dev/sda bs=4M | gzip > /mnt/debian_backup.img.gz && sync

skip the disk error and compress

dd if=/dev/sda bs=4M conv=noerror,sync | gzip > /mnt/debian_backup.img.gz && sync

How to Monitor dd Progress with pv

Step 1: Install pv

  apt update && apt install pv

Step 2: Get the Total Size of /dev/sda`

TOTAL_SIZE=$(cat /sys/block/sda/size)      #update the actuall disk from "sda"

Step 3: Convert Sectors to Bytes`

TOTAL_SIZE=$((TOTAL_SIZE * 512))

**Step 4: Run dd with pv for Progress Tracking

dd if=/dev/sda bs=4M | pv -s $TOTAL_SIZE --progress --eta --rate --bytes | gzip > /mnt/debian_backup.img.gz && sync  #update the actuall disk from "sda"

Expected Output Format

1.79GiB  [16.6MiB/s]  [=====>                     ]  10%  ETA 0:30:00

To restore the IMG to the system disk

dd if=/mnt/debian_backup.img of=/dev/sda bs=4M status=progress && 
sync

To restore from the compressed backup

gunzip -c /mnt/debian_backup.img.gz | dd of=/dev/sda bs=4M status=progress && sync

SSH Restore compressed backup:(Remote → Local)

ssh root@remote "gunzip -c /mnt/debian_backup.img.gz" | dd of=/dev/sda bs=4M status=progress && sync

Restore from web download

wget --user=username --password=password -O - "https://url/debian_backup.img.gz" | gunzip -c | dd of=/dev/sda bs=4M status=progress && sync
wget --user=myuser --ask-password -O - "https://url/debian_backup.img.gz" | gunzip -c | dd of=/dev/sda bs=4M status=progress && sync
  • input password later, avoid input the password in command and disclose in command history
wget --user='myuser' --password='mypa'\''ssword' -O - "https://url/debian_backup.img.gz" | gunzip -c | dd of=/dev/sda bs=4M status=progress && sync
  • If the actual password is mypa’ssword, it is split into:
  1. ‘mypa’ (first part)
  2. ‘’’ (escaped single quote)
  3. ‘ssword’ (remaining part)

Verify Using md5sum or sha256sum :

    1. Generate checksum for the system disk
md5sum /dev/sda
  • Or for higher security:
sha256sum /dev/sda
    1. Generate checksum for the backup IMG:
md5sum /mnt/debian_backup.img.gz

or

sha256sum /mnt/debian_tf_backup.img
    1. batch scrpt to check if the data is matched
#!/bin/bash

# Paths
SOURCE_DISK="/dev/sda"
BACKUP_FILE="/mnt/backup.img.gz"

# Temporary files for checksums
SOURCE_CHECKSUM="/tmp/sda_checksum.txt"
BACKUP_CHECKSUM="/tmp/backup_checksum.txt"

# Check if running as root (needed for /dev/sda)
if [ "$(id -u)" -ne 0 ]; then
    echo "Error: This script must be run as root to read $SOURCE_DISK."
    exit 1
fi

# Check if files exist
if [ ! -b "$SOURCE_DISK" ]; then
    echo "Error: $SOURCE_DISK not found or not a block device."
    exit 1
fi
if [ ! -f "$BACKUP_FILE" ]; then
    echo "Error: $BACKUP_FILE not found."
    exit 1
fi

# Calculate checksum of original disk
echo "Calculating SHA256 checksum of $SOURCE_DISK... (this may take a while)"
sha256sum "$SOURCE_DISK" | cut -d' ' -f1 > "$SOURCE_CHECKSUM"
if [ $? -ne 0 ]; then
    echo "Error: Failed to compute checksum of $SOURCE_DISK."
    exit 1
fi

# Calculate checksum of uncompressed backup
echo "Calculating SHA256 checksum of uncompressed $BACKUP_FILE... (this may take a while)"
gunzip -c "$BACKUP_FILE" | sha256sum | cut -d' ' -f1 > "$BACKUP_CHECKSUM"
if [ $? -ne 0 ]; then
    echo "Error: Failed to compute checksum of $BACKUP_FILE."
    exit 1
fi

# Compare checksums
echo "Comparing checksums..."
if diff "$SOURCE_CHECKSUM" "$BACKUP_CHECKSUM" > /dev/null; then
    echo "Success: Checksums match! The backup is consistent with $SOURCE_DISK."
else
    echo "Warning: Checksums do NOT match. The backup may be incomplete or corrupted."
    echo "Original ($SOURCE_DISK): $(cat $SOURCE_CHECKSUM)"
    echo "Backup ($BACKUP_FILE):  $(cat $BACKUP_CHECKSUM)"
fi

# Clean up
rm -f "$SOURCE_CHECKSUM" "$BACKUP_CHECKSUM"
echo "Done."

result shows

Success: Checksums match! The backup is consistent with /dev/sda. #matched

or

Warning: Checksums do NOT match. The backup may be incomplete or corrupted. #unmatched

restore from DD img file from rescue mode

scp -P 22 ~/.ssh/id_rsa root@your-rescue-ip:/root/.ssh/id_rsa
  • send the ssh key to vps which need to be restored by img file
chmod 600 /root/.ssh/id_rsa

  • update the key ownership
ssh -i /root/.ssh/id_rsa -p 2222 root@your-vps-ip "cat /mnt/backup.img.gz" | gunzip -c | dd of=/dev/vda bs=4M status=progress && sync
  • Breakdown
  1. -i /root/.ssh/id_rsa: Specifies the SSH key.
  2. -p 2222: Sets the SSH port to 2222.
  3. root@your-vps-ip: VPS address (replace your-vps-ip with the actual IP).
  4. cat /mnt/backup.img.gz: Streams the 5.9GB file from the VPS.
  5. gunzip -c | dd of=/dev/vda: Decompresses (to 25GB) and writes to /dev/vda.
  6. bs=4M status=progress: 4MB blocks with progress output.
  7. && sync: Ensures writes complete.
  • delete the ssh key before reboot
  1. Remove the Key
rm -f /root/.ssh/id_rsa

  1. Check for Key
ls -la /root/.ssh/ 

If host key verification failed. reset key record

ssh-keygen -R <vps-ip>

Check the img file partition structure

fdisk -l <image_file>

Example Output:

fdisk -l disk.img
Disk disk.img: 2 GiB, 2147483648 bytes, 4194304 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
Disklabel type: gpt
Disk identifier: XXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Device Start End Sectors Size Type
disk.img1 2048 411647 409600 200M EFI System
disk.img2 411648 2508799 2097152 1G Linux filesystem
disk.img3 2508800 4194300 1685501 823M Linux filesystem

0

评论区