Backup/Restore Unix-like Operating Systems

💾 Backup/Restore with dd command

To back up data using dd and apply maximum compression with xz, you can use a pipeline. The basic process is to use dd to copy the data from a source (such as a disk or partition) and then pipe that data into xz for compression. Here's how you can do it:

1. Backup with dd and xz Compression (Max Compression)

The command below uses dd to create a disk image and pipes it through xz for compression with maximum compression settings:

Terminal > sudo dd if=/dev/sdX bs=1G status=progress oflag=sync | xz -z -9 -c > /path/to/backup.img.xz

Breakdown of the Command:

Terminal > sudo dd: Runs dd with root privileges (you need this for accessing raw devices).

  • if=/dev/sdX: The input file. Replace /dev/sdX with the path of the disk or partition you want to back up (e.g., /dev/sda).
  • bs=1G: Block size for dd. You can adjust this value for performance. Larger block sizes typically improve speed, but this may vary based on the specific system and disk.
  • status=progress: Displays the progress of the operation.
  • oflag=sync: Ensures that data is written synchronously, preventing data loss in case of an unexpected shutdown or disconnection.
  • |: The pipe operator sends the output from dd to xz without creating an intermediate file.
  • xz -z -9 -c: Compresses the output from dd with xz using maximum compression:
  • -z: Tells xz to compress the data.
  • -9: Sets the compression level to maximum (this provides the best compression but takes the longest time).
  • -c: Sends the compressed output to stdout (this is required since you're redirecting it to a file).
  • > /path/to/backup.img.xz: The final output file for the compressed backup.

2. Restore from the Backup

To restore the backup, you can use the following command:

Terminal > xz -d -c /path/to/backup.img.xz | sudo dd of=/dev/sdX bs=1G status=progress oflag=sync

Breakdown of the Restore Command:

Terminal > xz -d -c /path/to/backup.img.xz: Decompress the .xz file.

  • -d: Decompress the file.
  • -c: Output the decompressed content to stdout.
  • | sudo dd of=/dev/sdX bs=1G: Pipe the decompressed data into dd and write it to the disk or partition (/dev/sdX), replacing the contents.
Note: Be cautious when using dd and xz as they can overwrite data. Make sure you're specifying the correct source and destination. Always double-check the device names.

Performance Consideration:

The compression (xz -9) provides maximum compression but can be slow, especially for large disks. If performance is an issue, you might experiment with less aggressive compression levels (e.g., -6 or -8) to find a good balance between compression size and speed.

💾 Backup/Restore with dump command

In FreeBSD, you can use dump to create backups of file systems and then pipe the output through xz to apply maximum compression. Here’s how you can do it:

Step 1: Backup with dump and xz Compression (Max Compression)

The basic syntax to back up a FreeBSD filesystem with dump and compress it using xz is:

Terminal > sudo dump -0L -f - /dev/ada0p1 | xz -z -9 -c > /path/to/backup.dump.xz

Breakdown of the Command:

Terminal > sudo dump: Runs the dump command with root privileges (necessary for accessing raw device files).

  • -0L: These options mean:
  • -0: Level 0 backup, which means a full backup. (This is typically used for the first backup, or when you want to perform a complete backup.)
  • -L: Follow symbolic links (this ensures that symlinks are backed up correctly).
  • -f -: Tells dump to send its output to stdout (instead of writing to a file, it writes to the pipe).
  • /dev/ada0p1: The device or partition you want to back up. Replace /dev/ada0p1 with your actual device (e.g., /dev/ada0p1 or /dev/da0s1).
  • |: The pipe operator sends the `dump` output to xz for compression.
  • xz -z -9 -c: Compresses the `dump` output with xz using maximum compression:
  • -z: Compress the input data.
  • -9: Use the highest compression level for xz (this will take the longest time but gives the best compression).
  • -c: Directs the compressed output to stdout.
  • > /path/to/backup.dump.xz: The final destination for the compressed backup.

Example:

To back up the filesystem on /dev/ada0p1 to a compressed file /mnt/backups/backup.dump.xz, you would run:

Terminal > sudo dump -0L -f - /dev/ada0p1 | xz -z -9 -c > /mnt/backups/backup.dump.xz

Step 2: Restore from the Backup

To restore from the compressed backup, you can reverse the process by decompressing the file and piping it into restore:

Terminal > xz -d -c /path/to/backup.dump.xz | sudo restore -r -f -

Breakdown of the Restore Command:

Terminal > xz -d -c /path/to/backup.dump.xz: Decompress the .xz file.

  • -d: Decompress the file.
  • -c: Output the decompressed content to stdout.
  • | sudo restore -r -f -: Pipe the decompressed data to restore:
  • -r: Specify a restore operation (restores files as they were when the backup was taken).
  • -f -: Tells restore to read from stdin.

Additional Notes:

  • Compression Considerations: Using xz -9 provides the maximum compression, but it can be slow, especially on large backups. If speed is important, you can try lower compression levels (e.g., -6 or -8).
  • Backup Level: The -0 option in dump means a full backup. You can use other levels (e.g., -1, -2, etc.) for incremental backups.
  • Device and Partition Names: Make sure you correctly specify the device name of the partition you're backing up (e.g., /dev/ada0p1). You can check your devices using ls /dev/.
Page Term Reference