mirror of
https://github.com/brunofontes/linuxShortcuts.git
synced 2024-11-24 02:01:08 +00:00
129 lines
3.9 KiB
Bash
Executable File
129 lines
3.9 KiB
Bash
Executable File
function backup () {
|
|
# $1 - Repository
|
|
# $2-$N - Files/Folders to backup
|
|
|
|
#--stats = show stats at end
|
|
#--progress = show each file being processed
|
|
nice -n 19 borg create --compression auto,zstd,9 --exclude-from=./exclude $1 "$2"
|
|
}
|
|
|
|
function backupNoCompression () {
|
|
# $1 - Repository
|
|
# $2-$N - Files/Folders to backup
|
|
|
|
#--stats = show stats at end
|
|
#--progress = show each file being processed
|
|
nice -n 19 borg create --compression none --exclude-from=./exclude $1 "$2"
|
|
}
|
|
|
|
function checkBackup() {
|
|
echo -n "`date +%r`- Checking the backup..."
|
|
borg check "$1" > "$1"_status
|
|
if [[ $(cat "$1"_status) != "" ]]; then
|
|
echo -e "\n\033[1;31m ERROR UNPACKING $1 \033[0m"
|
|
else
|
|
echo ".OK!"
|
|
rm "$1"_status
|
|
fi
|
|
echo
|
|
}
|
|
|
|
function pruneBackup() {
|
|
echo -e "\e[97m`date +%r` - Prune old backups...\e[39m"
|
|
borg prune -v --list --dry-run --keep-weekly=8 --keep-monthly=12 --keep-yearly=3 $1
|
|
}
|
|
|
|
|
|
cd /home/bruno/Apps/linuxShortcuts/Backup/
|
|
|
|
LastDisk=$(< .lastDisk)
|
|
if [ "$LastDisk" = '1' ]; then
|
|
ActiveDisk=2
|
|
else
|
|
ActiveDisk=1
|
|
fi
|
|
|
|
YEAR=`date +%Y`
|
|
HDPath="/run/media/bruno/Backup_$ActiveDisk"
|
|
|
|
BACKUPPATH="$HDPath/Backup"
|
|
YEARMONTH=`date +%Y-%m-%d`
|
|
HDYEARMONTH="$BACKUPPATH::$YEARMONTH"
|
|
|
|
echo
|
|
echo "Disk : $ActiveDisk"
|
|
echo "HD Backup path : $BACKUPPATH"
|
|
echo "HD Year-Month path: $HDYEARMONTH"
|
|
echo
|
|
echo "Please, insert Disk #$ActiveDisk and press enter to start backup"
|
|
read
|
|
|
|
timeout=30
|
|
echo -n "Mounting Backup_$ActiveDisk"
|
|
device=$(mount | grep "Backup_$ActiveDisk" | cut -d " " -f1)
|
|
while [[ "$device" != *"/dev/"* ]]; do
|
|
[[ $timeout -lt 1 ]] && echo -e "\e[97m Timeout!\e[39m" && break
|
|
(( timeout-- ))
|
|
echo -n "."
|
|
sleep 1s
|
|
device=$(mount | grep "Backup_$ActiveDisk" | cut -d " " -f1)
|
|
done
|
|
|
|
# If folder does not exist, exit with error
|
|
[ ! -d "$HDPath" ] && echo "This disk was used last time. Please, plug Backup_$ActiveDisk before running this script." && read && exit 1
|
|
echo ".OK!"
|
|
echo
|
|
|
|
if [ ! -d "$BACKUPPATH" ]; then
|
|
mkdir -p "$BACKUPPATH"
|
|
borg init --encryption=none $BACKUPPATH
|
|
|
|
#Prune old backups
|
|
pruneBackup $BACKUPPATH
|
|
fi
|
|
|
|
echo -e "\e[97m`date +%r` - Copying Linux Home folder (1/7)...\e[39m"
|
|
echo -e "\e[97m `date +%r` - Bruno\e[39m"
|
|
backup "$HDYEARMONTH-LinuxHome-bruno" "/home/bruno/" || echo ""
|
|
|
|
echo -e "\e[97m `date +%r` - Admin\e[39m"
|
|
backup "$HDYEARMONTH-LinuxHome-admin" "/home/admin/" || echo ""
|
|
|
|
echo -e "\e[97m`date +%r` - Copying Localização folder (2/7)...\e[39m"
|
|
backup "$HDYEARMONTH-Multimedia-localizacao" "/run/media/bruno/Multimedia/Localização/" || echo ""
|
|
|
|
echo -e "\e[97m`date +%r` - Copying My Documents folder (3/7)...\e[39m"
|
|
backup "$HDYEARMONTH-Multimedia-MyDocuments" "/run/media/bruno/Multimedia/MyDocuments/" || echo ""
|
|
|
|
echo -e "\e[97m`date +%r` - Copying Música folder (4/7)...\e[39m"
|
|
backup "$HDYEARMONTH-Multimedia-musica" "/run/media/bruno/Multimedia/Música/" || echo ""
|
|
|
|
checkBackup "$BACKUPPATH"
|
|
|
|
# Rsync Fotos e VMs
|
|
echo -e "\e[97m`date +%r` - Copying Fotos folder (5/7)...\e[39m"
|
|
backupNoCompression "$HDPath/Fotos::$YEARMONTH" "/run/media/bruno/Multimedia/Fotos" || echo ""
|
|
checkBackup "$HDPath/Fotos/"
|
|
|
|
echo -e "\e[97m`date +%r` - Copying Video folder (6/7)...\e[39m"
|
|
backupNoCompression "$HDPath/Videos::$YEARMONTH" "/run/media/bruno/Multimedia/Videos" || echo ""
|
|
checkBackup "$HDPath/Videos/"
|
|
|
|
echo -e "\e[97m`date +%r` - Copying Virtual Machines folder (7/7)...\e[39m"
|
|
backup "$HDPath/VirtualMachines::$YEARMONTH-2" "/run/media/bruno/Multimedia/Virtual Machines" || echo ""
|
|
checkBackup "$HDPath/VirtualMachines/"
|
|
|
|
# Show result
|
|
echo -e "\e[97m`date +%r` - Backup finished. Please, verify your log files.\e[39m"
|
|
|
|
echo "$ActiveDisk" > .lastDisk
|
|
|
|
kdialog --title "Backup Complete" --msgbox "Backup finished successfully"
|
|
echo
|
|
echo "Backup on $device is finished. Press any key to close..."
|
|
read
|
|
|
|
#Try to umount device
|
|
udisksctl unmount -b "$device" && udisksctl power-off -b "$device"
|
|
exit
|