Backup-Restore Linux

Backup
# slow singlethreaded gzip
tar -czpf /backup/backup.tar.gz --exclude=/backup --one-file-system /

# or with multithreaded gzip
apt install pigz -y
tar --use-compress-program=pigz -cpf /backup/backup.tar.gz --exclude=/backup --one-file-system /

# upload to backup server
rsync -avP /backup/* bkpsrv:/share/

Restore
# prepare filesystem and unpack backup
mkfs.ext4 /dev/sda1
mount /dev/sda1 /mnt
rsync -avP bkpsrv:/share/backup.tar.gz /mnt/
tar -xpzf /mnt/backup.tar.gz -C /mnt --numeric-owner

# restore grub
for f in dev dev/pts sys proc ; do mount --bind /$f /mnt/$f ; done
chroot /mnt

# GRUB 1 (Debian / Ubuntu / RHEL 6)
dpkg-reconfigure grub-pc
grub-install --recheck /dev/sda

# GRUB 2 (RHEL 7)
grub2-install /dev/sda
grub2-mkconfig -o /boot/grub2/grub.cfg

# manually recheck configs
# dont forget about UUID in fstab
less /dev/fstab
less /boot/grub/grub.cfg

Automation script with rotation
#!/bin/bash

MAXBACKUPS=3
BACKUPSDIR=/backup
DATE=`date +"%Y%m%d.%H%M%S"`
USERDIR=userus

# mount
mount 10.10.10.100:/mnt/sdb/backup /backup

# backup
tar --use-compress-program=pigz -cpf $BACKUPSDIR/backup-$DATE-root.tar.gz --exclude=/backup --exclude=/opt/steam --exclude=/home --exclude=/swapfile --one-file-system /
tar --use-compress-program=pigz -cpf $BACKUPSDIR/backup-$DATE-home-$USERDIR.tar.gz --exclude=/home/$USERDIR/.steam --exclude=/home/$USERDIR/.local/share/Steam --exclude=/home/$USERDIR/.cache --one-file-system /home/$USERDIR

# clenaup
cd $BACKUPSDIR
ls -A1t | grep .gz | grep home | tail -n +$MAXBACKUPS | xargs rm -f
ls -A1t | grep .gz | grep root | tail -n +$MAXBACKUPS | xargs rm -f

exit 0