#!/bin/bash
####################################
#
# Backup minecraft world to a
# specified folder.
#
####################################
set -e
#sudo systemctl stop watchdog >/dev/null
version=2.1
day_raw=$(date +%A_%d_%m_%y)

function backup_ {
echo -e "\e[95mMinecraft auto-backup version $version\e[0m"
#archive_file="minecraft_ubuntu_$day_raw"
archive_file="minecraft_ubuntu"
output_folder=/tmp
echo -e "Creation archive: $archive_file\n"
sleep 1
cd $output_folder
if  [ -f /tmp/Minecraft_*.tar.xz ]
then
rm Minecraft-*.tar.xz
fi
if  [ -f /tmp/Minecraft_*.md5 ]
then
rm Minecraft-*.md5
fi
if  [ -f /tmp/Minecraft_*.tar ]
then
rm Minecraft_*.tar
fi
echo -e "\e[97mPacking files...\e[0m"
tar cf - --exclude='/home/wareck/Minecraft/cache' --exclude='/home/wareck/Minecraft/minecraft.tar.xz' --exclude='/home/wareck/Minecraft/backups' --exclude='/home/wareck/Minecraft/build' --exclude='/home/wareck/Minecraft/paperclip.jar' --exclude='/home/wareck/Minecraft/spigot-1.*.jar' --exclude='/home/wareck/Minecraft/logs' /home/wareck/Minecraft --checkpoint=.300 | pixz >$output_folder/$archive_file.tar.xz
md5sum $output_folder/$archive_file.tar.xz > $archive_file.md5

echo -e "\n\e[97mBackup local : \e[0m"
sudo cp -v $output_folder/$archive_file.tar.xz /media/Backup/Backup/Minecraft/
sudo cp -v $output_folder/$archive_file.md5 /media/Backup/Backup/Minecraft/

echo -e "\n\e[97mUpload on Freebox...\e[0m"
echo "Mounting..."
sudo mount -t cifs //192.168.1.254/Disque\ dur/  /media/Nas/ -o user=freebox,password=password,uid=1000,gid=1000,rw,vers=1.0
sleep 5
sudo cp -v $output_folder/$archive_file.tar.xz /media/Nas/mc_backup
sudo cp -v $output_folder/$archive_file.md5 /media/Nas/mc_backup
sleep 5
sudo umount /media/Nas
echo "Unmounting..."
sleep 2

echo -e "\n\e[97mUpload on FTP...\e[0m"
ncftpput -v -u "wareck" -p "zorn692611" ftpperso.free.fr minecraft_bkp/ $output_folder/$archive_file.tar.xz
sleep 1

echo -e "\nBackup done."
}

backup_
