Skip to content

Instantly share code, notes, and snippets.

@theoparis
Last active August 20, 2020 19:39
Show Gist options
  • Save theoparis/928664b752192a92c73b0ca570c7a72a to your computer and use it in GitHub Desktop.
Save theoparis/928664b752192a92c73b0ca570c7a72a to your computer and use it in GitHub Desktop.
backup scripts
#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################
# What to backup.
backup_files="/srv/homelab/data /etc/nginx/sites-enabled"
# Where to backup to.
dest="/root/backup"
# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
dpkg-query -f '${binary:Package}\n' -W > $dest/../packages.txt
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest
# WARNING: this will overwrite the current installation with the backup contents!
if [[ ! $SKIP_PROMPT ]]; then
read -p "Are you sure you want to restore the latest backup? This will overwrite the current installation with the backup contents! " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "User cancelled restoration, exiting."
exit 1
fi
fi
dest="/root/backup"
latest_file=$(ls -t $dest | head -n 1)
sudo tar -xzvf $dest/$latest_file -C /
# Prepare Packages
sudo apt update
sudo apt-get dist-upgrade
# Install packages list from file
sudo apt install $(< $dest/../packages.txt)
# Clean: Fix eventually broken dependencies and remove unnecessary
sudo apt -f install
sudo apt autoremove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment