Backup-Restore Linux

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

# with multithreaded gzip (pigz)
apt install pigz -y
tar -I 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
fdisk /dev/sda
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 /etc/fstab
less /boot/grub/grub.cfg
Backup automation script with rotation
#!/bin/bash

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

# 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,/opt/steam,/home,/swapfile} --one-file-system /
tar --use-compress-program=pigz -cpf $BACKUPSDIR/backup-$DATE-home.tar.gz --exclude=/home/*/{.bash_history,.cache,.steam,.local/share/Steam,src,VirtualBox\ VMs,} /home

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

# unmount
umount -f /backup

exit 0