Skip to content

Instantly share code, notes, and snippets.

@89luca89
Last active January 24, 2018 22:31
Show Gist options
  • Save 89luca89/54d2b69de35eb5bfaada05e9089cd27d to your computer and use it in GitHub Desktop.
Save 89luca89/54d2b69de35eb5bfaada05e9089cd27d to your computer and use it in GitHub Desktop.
Simple Backup utility, recognizing where to backup and (according to partition space) what profile to use
#!/bin/bash
ssh_backup() {
echo "What's the IP address?"
read IP
echo "What's the port?"
read port
SSH="ssh -p $port"
DIR=$IP":/home/"$USERNAME;
echo "What profile?\n"
options=("64gb" "128gb" ">256gb")
echo "Backing up /home to "$DIR"..."
select opt in "${options[@]}"
do
case $opt in
"64gb")
echo " less than 64gb"
echo " using profile Exclude-64"
echo rsync -z -e "$SSH" --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude-64 ~/ $DIR 2>&1 | tee -a /tmp/backup.log
break
;;
"128gb")
echo " less than 128gb"
echo " using profile Exclude-128"
rsync -aAHz -e "$SSH" --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude-128 ~/ $DIR 2>&1 | tee -a /tmp/backup.log
break
;;
">256gb")
echo " more than 128gb"
echo " using profile Exclude"
rsync -aAHz -e "$SSH" --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude ~/ $DIR 2>&1 | tee -a /tmp/backup.log
break
;;
esac
done
echo "
——————--
-- All backed up! --
——————--
" | tee -a /tmp/backup.log
# Log file is created or appended on BACKUP Drive from /tmp:
# (backup.log will be removed from /tmp on next reboot.)
scp /tmp/backup.log $DIR/backup.log
sync
}
home() {
# Check if at least one directory for Backup exists
if ! ls /run/media/$USER | grep BACKUP > /dev/null; then
echo "NO BACKUP DRIVE FOUND! Please make sure BACKUP Drive USB is connected." ; exit 1
fi
# Check if at least one directory for Backup exists
if ! ls /run/media/$USERNAME | grep BACKUP | grep -v ROOT > /dev/null; then
echo "NO HOME DRIVE FOUND! Please make sure BACKUP Drive USB is connected."
fi
# Log file is created in /tmp:
echo "
Backup sent to BACKUP Drive USB from $HOSTNAME by ‘$USERNAME’ on:" > /tmp/backup.log
date| tee -a /tmp/backup.log
echo "
Errors:" | tee -a /tmp/backup.log
# FOR EACH backup directory, execute the backup
for d in $(ls /run/media/$USERNAME | grep BACKUP | grep -v ROOT)
do
DIR="/run/media/"$USERNAME/$d/$USERNAME;
echo "
Backing up /home to "$DIR"..."
# Different profile if backing up to PenDrive instead of HDD/SSD
PARTITION_SIZE=$(df | grep "/run/media/"$USERNAME/$d$ | awk '{print $2}' )
if [ $PARTITION_SIZE -le 67782100 ]; then
echo " less than 64gb"
echo " using profile Exclude-64"
rsync -z --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude-64 ~/ $DIR 2>&1 | tee -a /tmp/backup.log
elif [ $PARTITION_SIZE -ge 67782100 -a $PARTITION_SIZE -le 133564200 ]; then
echo " less than 128gb"
echo " using profile Exclude-128"
rsync -aAHz --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude-128 ~/ $DIR 2>&1 | tee -a /tmp/backup.log
elif [ $PARTITION_SIZE -ge 133564200 ]; then
echo " more than 128gb"
echo " using profile Exclude"
rsync -aAHz --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude ~/ $DIR 2>&1 | tee -a /tmp/backup.log
fi
echo "
——————--
-- All backed up! --
——————--
" | tee -a /tmp/backup.log
# Log file is created or appended on BACKUP Drive from /tmp:
# (backup.log will be removed from /tmp on next reboot.)
cat /tmp/backup.log | tee -a $DIR/backup.log
# Writing all cached data to drives and exiting:
sync
done
}
system() {
# Check if at least one directory for Backup exists
if ! ls /run/media/$USER | grep BACKUP > /dev/null; then
echo "NO BACKUP DRIVE FOUND! Please make sure BACKUP Drive USB is connected." ; exit 1
fi
rm /tmp/backup.log
# Check if at least one directory for Backup exists
if ! ls /run/media/$USERNAME | grep BACKUP | grep ROOT > /dev/null; then
echo "NO SYSTEM DRIVE FOUND! Please make sure BACKUP Drive USB is connected."
fi
# Log file is created in /tmp:
echo "
Backup sent to BACKUP Drive USB from $HOSTNAME by ‘$USERNAME’ on:" > /tmp/backup.log
date| tee -a /tmp/backup.log
echo "
Errors:" | tee -a /tmp/backup.log
# FOR EACH backup directory, execute the backup
for d in $(ls /run/media/$USERNAME | grep BACKUP | grep ROOT)
do
DIR="/run/media/"$USERNAME/$d;
echo "
Backing up System to "$DIR"..."
sudo rsync -aAXHz --delete --info=progress2 --recursive --exclude-from /home/luca-linux/bin/BackupRSync/exclude / $DIR 2>&1 | tee -a /tmp/backup.log
sudo mkdir -p $DIR/home
sudo mkdir -p $DIR/var/log
sudo mkdir -p $DIR/var/spool
sudo mkdir -p $DIR/dev
sudo mkdir -p $DIR/proc
sudo mkdir -p $DIR/sys
sudo mkdir -p $DIR/tmp
sudo mkdir -p $DIR/run
sudo mkdir -p $DIR/mnt
sudo mkdir -p $DIR/media
echo "
——————--
-- All backed up! --
——————--
" | tee -a /tmp/backup.log
# Log file is created or appended on BACKUP Drive from /tmp:
# (backup.log will be removed from /tmp on next reboot.)
cat /tmp/backup.log | sudo tee -a $DIR/backup.log > /dev/null
# Writing all cached data to drives and exiting:
sync
done
}
# present menu for user to choose which workspace to open
USERNAME=$USER
PS3="Please choose your option: "
options=("Home" "System" "Full" "SSH")
echo "Available options"
echo "------------------"
echo " "
select opt in "${options[@]}"
do
case $opt in
"Home")
home
STRING="Home"
break
;;
"System")
system
STRING="System"
break
;;
"Full")
system
home
STRING="Full System and Home"
break
;;
"SSH")
ssh_backup
STRING="SSH"
break
;;
esac
done
notify-send -i gnome-disks -u critical "$STRING Backup " 'Done'
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment