Backup methods (III): dd / tar
Making backups regularly is one of the most relevant tasks for a Linux user. In this third chapter of ‘Backup methods’ I will show you two other methods for backing up using basic commands like dd
and tar
.
Table of Contents
dd
You can copy entire disks to a file using dd
. Just take note of the name of the disk you want to back up (e.g.: /dev/sdb
).
dd if=/dev/sdb of=disk-backup.dd bs=1024
- Backup file must be in another disk.
- You can name your backup file whatever you want, and use the suffix you want (
dd
,img
,…) bs=<bytes>
: read and write up to<bytes>
bytes at a time. You can tweak this setting to improve backup speed (default value is 512).- Backup file size will be the same as disk size.
To restore a backup, just do the opposite:
dd if=disk-backup.dd of=/dev/sdb bs=1024
- This will overwrite ‘sdb’ data.
- I think it’s better to use the same
bs
number you used when backing up.
You can backup partitions the same way.
tar
You can archive folders or an entire filesystem using tar
.
tar cf backup.tar myfolder/
c
stands for “create”,f
stands for “archive file”.- You can optionally use a dash (
-
) before the parameters like most commands.
You can compress the file (using gzip) with the z
parameter.
tar czf backup.tar.gz myfolder/
When you want to restore a backup:
tar xpf backup.tar
x
means “extract”,p
means “extract file permissions”.- It will extract the contents in the working directory (the directory in which you are running the commands). If you just archived one folder (like the previous command), it will create that folder. If you archived several folders or files (
tar cf backup.tar file1 file2 file3
) you may wanttar
to create a folder for the archived contents. In this case, add this parameter:--one-top-level=<folder name>
. - Extract gzip compressed tar files with the
z
parameter.
You can create multi-volume archives (split files) with M
and L <bytes>
.
tar -cML 100M -f multivolume.tar.part01 myfolder/
- It will show you a prompt every time
tar
finishes creating a part. You will need to typen
and the file name of that part.Prepare volume #2 for ‘multivolume.tar.part01’ and hit return: n multivolume.tar.part02 Prepare volume #3 for ‘multivolume.tar.part02’ and hit return: n multivolume.tar.part03 Prepare volume #4 for ‘multivolume.tar.part03’ and hit return: n multivolume.tar.part04
To extract a multi-volume archive, just add the M
parameter and type n
and the file names when the program asks you for it.
$ tar -xMf multivolume.tar.part01 --one-top-level=extract
Prepare volume #2 for ‘multivolume.tar.part01’ and hit return: n multivolume.tar.part02
Prepare volume #3 for ‘multivolume.tar.part02’ and hit return: n multivolume.tar.part03
Prepare volume #4 for ‘multivolume.tar.part03’ and hit return: n multivolume.tar.part04
- Remember that
--one-top-level=<folder>
creates a folder for the extracted contents.
Check How to compress and extract files on Linux for more info.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: