From 35ed6782e6cc1a5d2d1c5169ce604f54fc5d400b Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Sun, 24 May 2020 11:06:30 -0300 Subject: [PATCH] Implementing zBackup to backups and organizing script --- .gitignore | 1 + Backup/.Backup_HD.sh | 99 ++++++++++++++++++++++++++++++++++++++ Backup/.Backup_HD_1TB.sh | 77 ----------------------------- Backup/exclude | 5 -- Backup/runBackup.sh | 2 +- Backup/zRestore.sh | 3 ++ Services/minecraft-hour.sh | 6 +-- 7 files changed, 107 insertions(+), 86 deletions(-) create mode 100755 Backup/.Backup_HD.sh delete mode 100755 Backup/.Backup_HD_1TB.sh create mode 100755 Backup/zRestore.sh diff --git a/.gitignore b/.gitignore index 6c50eed..58d7b7e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .serverBackupPath .sshKeyBackupPath .sshKey +Backup/.lastDisk diff --git a/Backup/.Backup_HD.sh b/Backup/.Backup_HD.sh new file mode 100755 index 0000000..9e537bb --- /dev/null +++ b/Backup/.Backup_HD.sh @@ -0,0 +1,99 @@ +LastDisk=$(< .lastDisk) +if [ "$LastDisk" = '1' ]; then + ActiveDisk=2 +else + ActiveDisk=1 +fi + +YEAR=`date +%Y` +HDPath="/run/media/bruno/Backup_$ActiveDisk" + +ZBACKUP="$HDPath/`date +%Y-Q%q`" +YEARMONTH=`date +%m_%b-%d` +HDYEARMONTH="$ZBACKUP/backups/$YEARMONTH" + + +# 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 +echo "HD Backup path : $ZBACKUP" +echo "HD Year-Month path: $HDYEARMONTH" +echo +echo Press enter key to start the backup +read +echo + +if [ ! -d "$ZBACKUP" ]; then + mkdir -p "$ZBACKUP" + zbackup init --non-encrypted "$ZBACKUP" + + #If diskSpace less than ~100GB... + diskSpace=$(df --local --output=avail,target | grep "$HDPath\$" | awk '{ print $1}') + if [[ "$diskSpace" < 100000000 ]]; then + # delete oldest backup folder + oldestFolder=$(/bin/ls -dt "$HDPath"/????-Q? | tail -n 1) + echo + echo "$HDPath/$oldestFolder" + rm -rI "$HDPath/$oldestFolder" + fi +fi + +mkdir -p "$HDYEARMONTH" + +function backup () { + nice -n 19 tar c --exclude-ignore=.no-backup --add-file=.backup --exclude-from=./exclude "$1" | zbackup backup --non-encrypted --silent "$2" + zbackup restore --silent --non-encrypted "$2" > /dev/null 2> "$2"_status + if [[ $(cat "$2"_status) != "" ]]; then + echo -e "\033[1;31m ERROR UNPACKING $2 \033[0m" + 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 "/home/bruno/" "$HDYEARMONTH/LinuxHome-bruno" || echo "" + +echo -e "\e[97m `date +%r` - Admin\e[39m" +backup "/home/admin/" "$HDYEARMONTH/LinuxHome-admin" || echo "" + +echo -e "\e[97m `date +%r` - Lost+Found\e[39m" +backup "/home/lost+found/" "$HDYEARMONTH/LinuxHome-lost+found" || echo "" + +echo -e "\e[97m`date +%r` - Copying Localização folder (2/7)...\e[39m" +backup "/run/media/bruno/Multimedia/Localização/" "$HDYEARMONTH/Multimedia-localizacao" || echo "" + +echo -e "\e[97m`date +%r` - Copying My Documents folder (3/7)...\e[39m" +backup "/run/media/bruno/Multimedia/MyDocuments/" "$HDYEARMONTH/Multimedia-MyDocuments" || echo "" + +echo -e "\e[97m`date +%r` - Copying Música folder (4/7)...\e[39m" +backup "/run/media/bruno/Multimedia/Música/" "$HDYEARMONTH/Multimedia-musica" || echo "" + + +# Rsync Fotos e VMs +alias myrsync='nice -n 19 rsync -a' + +echo -e "\e[97m`date +%r` - Copying Fotos folder (5/7)...\e[39m" +myrsync "/run/media/bruno/Multimedia/Fotos" "$HDPath/$YEAR/" || echo "" + +echo -e "\e[97m`date +%r` - Copying Video folder (6/7)...\e[39m" +myrsync --exclude-from=/run/media/bruno/Multimedia/Videos/.no-backup "/run/media/bruno/Multimedia/Videos" "$HDPath/$YEAR/" || echo "" + +echo -e "\e[97m`date +%r` - Copying Virtual Machines folder (7/7)...\e[39m" +myrsync "/run/media/bruno/Multimedia/Virtual Machines" "$HDPath/$YEAR/" || echo "" + + +# Show result +echo -e "\e[97m`date +%r` - Backup finished. Please, verify your log files.\e[39m" + +echo "$ActiveDisk" > .lastDisk +device=$(mount | grep "Backup_$ActiveDisk" | cut -d " " -f1) + +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 diff --git a/Backup/.Backup_HD_1TB.sh b/Backup/.Backup_HD_1TB.sh deleted file mode 100755 index 388f79d..0000000 --- a/Backup/.Backup_HD_1TB.sh +++ /dev/null @@ -1,77 +0,0 @@ -YEARMONTH=`date +%Y/%m-%b` -YEAR=`date +%Y` -HDPath="/run/media/bruno/Backup_" -LastDisk=$(< .lastDisk) - -if [ "$LastDisk" = '1' ]; then - NextDisk=2 -else - NextDisk=1 -fi - -#backup="rsync -aq --inplace --exclude-from=./exclude" -backup="nice -n 19 rsync -a --exclude-from=./exclude" -HDPath="$HDPath$NextDisk" -HDYEARMONTH="$HDPath/$YEARMONTH" - -# If folder does not exist, exit with error -[ ! -d "$HDPath" ] && echo "This disk was used last time. Please, plug Backup_$NextDisk before running this script." && exit 1 - -echo -echo "Backup command : $backup" -echo "HD Backup path : $HDPath" -echo "HD Year-Month path: $HDYEARMONTH" -echo -echo Press enter key to start the backup -read -echo - -mkdir -p "$HDPath/$YEAR" -mkdir -p "$HDYEARMONTH" -mkdir -p "$HDYEARMONTH/LinuxHome" -mkdir -p "$HDYEARMONTH/LinuxHome/bruno" -mkdir -p "$HDYEARMONTH/LinuxHome/admin" -mkdir -p "$HDYEARMONTH/LinuxHome/lost+found" - - -echo -e "\e[97m`date +%r` - Copying Dropbox folder (1/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/Dropbox" "$HDYEARMONTH/Multimedia/" || echo "" - -echo -e "\e[97m`date +%r` - Copying Linux Home folder (2/7)...\e[39m" - -echo -e "\e[97m `date +%r` - Bruno\e[39m" -eval $backup "/home/bruno/" "$HDYEARMONTH/LinuxHome/bruno/" || echo "" - -echo -e "\e[97m `date +%r` - Admin\e[39m" -eval $backup "/home/admin/" "$HDYEARMONTH/LinuxHome/admin/" || echo "" - -echo -e "\e[97m `date +%r` - Lost+Found\e[39m" -eval $backup "/home/lost+found/" "$HDYEARMONTH/LinuxHome/lost+found/" || echo "" - -echo -e "\e[97m`date +%r` - Copying Localização folder (3/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/Localização" "$HDYEARMONTH/Multimedia/" || echo "" - -echo -e "\e[97m`date +%r` - Copying My Documents folder (4/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/MyDocuments" "$HDYEARMONTH/Multimedia/" || echo "" - -echo -e "\e[97m`date +%r` - Copying Música folder (5/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/Música" "$HDYEARMONTH/Multimedia/" || echo "" - -echo -e "\e[97m`date +%r` - Copying Fotos folder (6/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/Fotos" "$HDPath/$YEAR/" || echo "" - -echo -e "\e[97m`date +%r` - Copying Virtual Machines folder (7/7)...\e[39m" -eval $backup "/run/media/bruno/Multimedia/Virtual\ Machines" "$HDPath/$YEAR/" || echo "" - -echo -e "\e[97m`date +%r` - Backup finished. Please, verify your log files.\e[39m" - -echo "$NextDisk" > .lastDisk -kdialog --title "Backup Complete" --msgbox "Backup finished successfully" - -echo "Press any key to close" -read - -#Umount device -device=$(mount | grep Backup_$LastDisk | cut -d " " -f1) -udisksctl unmount -b $device && udisksctl power-off -b $device -exit diff --git a/Backup/exclude b/Backup/exclude index d10f636..129522d 100755 --- a/Backup/exclude +++ b/Backup/exclude @@ -1,10 +1,5 @@ .cache/* -Downloads/* -TransMit\ Tools\ -\ Studio\ 2015/* -Apps/* -shared/* .npm/* -bruno/Android/* bruno/go/* bruno/node_modules/* Virtual\ Machines/projetomovase/* diff --git a/Backup/runBackup.sh b/Backup/runBackup.sh index c5f7a14..53afbd5 100755 --- a/Backup/runBackup.sh +++ b/Backup/runBackup.sh @@ -1 +1 @@ -terminator -m -T BACKUP -x sudo systemd-inhibit bash .Backup_HD_1TB.sh & +terminator -m -T BACKUP -x sudo systemd-inhibit bash /home/bruno/Apps/linuxShortcuts/Backup/.Backup_HD.sh & diff --git a/Backup/zRestore.sh b/Backup/zRestore.sh new file mode 100755 index 0000000..184ac26 --- /dev/null +++ b/Backup/zRestore.sh @@ -0,0 +1,3 @@ +date +zbackup --non-encrypted --silent --cache-size 512mb restore $1 > $2 +date diff --git a/Services/minecraft-hour.sh b/Services/minecraft-hour.sh index 5c0c5f0..01a5bf7 100755 --- a/Services/minecraft-hour.sh +++ b/Services/minecraft-hour.sh @@ -2,7 +2,7 @@ if ! screen -list | grep -q "Minecraft"; then exit 1 fi -BACKUPDIR="/home/bruno/Backups/Minecraft" +BACKUPDIR="/home/bruno/Backups/zBackup-Minecraft/backups" SERVERDIR="/home/bruno/Apps/Minecraft" SURVIVALDIR="$SERVERDIR/BedrockServer_Survival_19132" CREATIVEDIR="$SERVERDIR/BedrockServer_Creative_19134" @@ -50,8 +50,8 @@ done BACKUPDATE=$(date +"%Y-%m-%d_%H-%M-%S") mkdir "$BACKUPDIR/$BACKUPDATE" -/bin/tar cz --exclude=behavior_packs/* --exclude=resource_packs/* -f "$BACKUPDIR/$BACKUPDATE/Survival.tar.gz" "$SURVIVALDIR/worlds/Survival" -/bin/tar cz --exclude=behavior_packs/* --exclude=resource_packs/* -f "$BACKUPDIR/$BACKUPDATE/Creative.tar.gz" "$CREATIVEDIR/worlds/Creative" +/bin/tar c --exclude=behavior_packs/* --exclude=resource_packs/* "$SURVIVALDIR/worlds/Survival" | zbackup --nonencrypted backup "$BACKUPDIR/$BACKUPDATE-Survival" +/bin/tar c --exclude=behavior_packs/* --exclude=resource_packs/* "$CREATIVEDIR/worlds/Creative" | zbackup --nonencrypted backup "$BACKUPDIR/$BACKUPDATE-Creative" runAllWorlds "^u" runAllWorlds "save resume\n"