Backup
Updated: September 28, 2024
Creating backups on linux systems including cold storage, local and remote with rsync.
Table of Contents
COLD STORAGE BACKUP WITH USB LOCALLY
Format the USB to ext4 (best for linux systems) Create a mount point for the usb. We do this by simply making a directory in the location of our choosing. We are going to make a folder called mkbk in /mnt.
df -T /path/to/usb # find filesystem path of usb and format type
sudo umount /filesystem/from/above
sudo mkfs.ext4 /filesystem/from/above -L <label>
mkdir /mnt/bkup
Find the UUID/LABEL of the USB flash drive. Write them down.
sudo blkid | egrep -v loop # keep loop devices from showing in list
Edit /etc/fstab
sudo vim /etc/fstab # file use for creating custom settings for a mount.
UUID=f6c28bb7-410e-bfdb-5f4ee820117e /mnt/bkup ext4 rw,noauto,user,exec,async 0 2
# UUID=from earlier using blkid command.
# /mnt/bkup. The place we created the usb to be mounted.
# ext4. The format of the drive being mounted. Do not use NTFS for this.
# * rw=read and writable.
# * noauto=keeps from automatically mounting.
# * user=so normal user can mount and unmount. (do not require root)
# * exec=ability to execute commands (like rsync)
# * async=always use async as sync can shorten life of drives.
# 0=do not use backup dump program.
# 2=do a fsck after (root set to 1), 0 if skip fsck.
Mounting from fstab.
Now the USB flash drive should be mountable per our configuration.
mount /mnt/bkup # will mount the usb according the setting in fstab file.
mount -t ext4 # check to see if the mount worked, setting are correct.
umount -l /mnt/bkup # lazy unmount-make sure nothing is being written before unmounting.
BACKUP SCRIPTs
Create a file calling it whatever you want as long as you end it in .sh. Imma call mine bkup.sh. We will deal with where we put it later.
vim bkup.sh
Backup whole system.
#!/bin/sh
umount -l /mnt/bkup # make sure it is not already mounted.
mount /mnt/bkup # mount from settings in fstab file.
rsync -aAXv --delete --dry-run --exclude="lost+found" --exclude=".cache" --exclude="swapfile" --exclude=".ecryptfs" --exclude="/dev/*" --exclude="/proc/*" --exclude="/sys/*" --exclude="/tmp/*" --exclude="/run/*" --exclude="/mnt/*" --exclude="/media/*" / /mnt/bkup
umount -l /mnt/bkup # lazy unmount after backup is complete.
Make the script executable.
chmod 755 bkup.sh
Backup specific files.
#!/bin/sh
umount -l /mnt/bkup # make sure it is not already mounted.
mount /mnt/bkup # mount from settings in fstab file.
rsync -aAXRv --delete --prune-empty-dirs --dry-run ~/ssh/ ~/.zshrc /etc/fstab /etc/motd /var/www/ /home/./user1/download home/./user2/download /mnt/bkup
umount -l /mnt/bkup # lazy unmount after backup is complete.
CRON
# at 3:33 AM every Sunday
33 3 * * 0 sh bkup.sh
RESTORE
rsync -aAXv --delete --exclude="lost+found" /mnt/bkup/ /mnt/system