#!/bin/ash
#
#
#
# *** INTRODUCTION ***
 clear
 echo ""
 echo " Welcome to 4MLinux Backup Scripts!"
 echo ""
 echo " You will be asked to choose a source partition and a target partition."
 echo " The script will copy all the files located on the source partition,"
 echo " and it will create a compressed backup archive on the target partition."
 echo ""
 echo " NOTES:"
 echo " 1) The backup script is safe (it will NOT delete or format any data)."
 echo " 2) The target partition must be big enough to store your backup file."
 echo " 3) There is an option to send the backup file to a remote server."
 echo ""
 echo -n " Press Enter to continue..."; read
#
#
#
# *** SOURCE PARTITION ***
 clear
 lsdisk
 echo ""
 echo "Which file system do you wish to backup ?"
 read -p "(for example: sda1): " SOURCE
 if [ ! -b /dev/$SOURCE ]; then
   echo "Error: the block device '/dev/$SOURCE' does not exist."
   exit
 fi
 mkdir /mnt/$SOURCE 2>/dev/null
 umount /dev/$SOURCE 2>/dev/null
 ntfs-3g /dev/$SOURCE /mnt/$SOURCE 2>/dev/null
 mount /dev/$SOURCE /mnt/$SOURCE 2>/dev/null
 rmdir /mnt/$SOURCE 2>/dev/null
 if [ ! -e /mnt/$SOURCE ]; then
   echo "Error: I am unable to mount /dev/$SOURCE."
   exit
 fi
#
#
#
# *** TARGET PARTITION ***
 clear
 lsdisk
 echo ""
 echo "Where do you wish to save the backup file?"
 read -p "(for example: sda2): " TARGET
 if [ ! -b /dev/$TARGET ]; then
   echo "Error: the block device '/dev/$TARGET' does not exist."
   exit
   else
   if [ $TARGET = $SOURCE ]; then
     echo "Error: you must not backup $SOURCE to $SOURCE."
     exit
   fi
 fi
 mkdir /mnt/$TARGET 2>/dev/null
 umount /dev/$TARGET 2>/dev/null
 ntfs-3g /dev/$TARGET /mnt/$TARGET 2>/dev/null
 mount /dev/$TARGET /mnt/$TARGET 2>/dev/null
 rmdir /mnt/$TARGET 2>/dev/null
 if [ ! -e /mnt/$TARGET ]; then
   echo "Error: I am unable to mount /dev/$TARGET."
   exit
 fi
#
#
#
# *** BACKUP OPTIONS ***
 clear
 echo ""
 echo " Choose the archive that you want to create:"
 echo " 1) backup.tar.zst (very fast)"
 echo " 2) backup.tar.gz (fast)"
 echo " 3) backup.tar.bz2 (slow)"
 echo " 4) backup.tar.xz (very slow)"
 echo ""
 echo " NOTE:"
 echo " Compression ratios are usually as folows:"
 echo " tar.xz > tar.bz2 > tar.gz > tar.zst"
 echo ""
 read -p " Your choice (1,2,3,4)?: " CHOICE
 case "$CHOICE" in
 '1')
 BACKUP=backup.tar.zst
 TAR_OPTION=cv
 ;;
 '2')
 BACKUP=backup.tar.gz
 TAR_OPTION=cvz
 ;;
 '3')
 BACKUP=backup.tar.bz2
 TAR_OPTION=cvj
 ;;
 '4')
 BACKUP=backup.tar.xz
 TAR_OPTION=cv
 ;;
 *)
 echo " Error: '$CHOICE' is out of the valid range (1,2,3,4)."
 exit
 ;;
 esac
#
#
#
# *** BACKUP FILE ***
 echo ""
 if [ -e /mnt/$TARGET/$BACKUP ]; then
   echo " The file '$BACKUP' already exists on /dev/$TARGET."
   read -p " Do you wish to overwrite it (y/n)?: " OVERWRITE
   OVERWRITE=X$OVERWRITE
   if [ $OVERWRITE = Xy ]; then
     rm /mnt/$TARGET/$BACKUP
     else
     echo " Script interrupted by user."
     exit
   fi
 fi
 cd /mnt/$SOURCE
 tar -$TAR_OPTION -f /mnt/$TARGET/$BACKUP *
 cd /mnt/$TARGET
 echo "Compressing, please wait..."
 if [ $CHOICE = 1 ]; then
   mv -f backup.tar.zst backup.tar
   zstd -v --rm backup.tar
 fi
 if [ $CHOICE = 4 ]; then
   mv -f backup.tar.xz backup.tar
   xz -v backup.tar
 fi
#
#
#
# *** FTP ***
 clear
 echo ""
 df /dev/$TARGET | grep 100% > /tmp/fsbackup
 if [ -s /tmp/fsbackup ]; then
   echo " ERROR: no space left on /dev/$TARGET."
   rm /tmp/fsbackup
   exit
   else
   rm /tmp/fsbackup
   echo " The backup file '/mnt/$TARGET/$BACKUP' is ready."
   read -p " Do you wish to send it to a remote server (y/n)? " FTP
   FTP=X$FTP
   if [ ! $FTP = Xy ]; then
     echo " Bye!"
     else
     echo /mnt/$TARGET > /tmp/local
     ftp-menu
   fi
 fi
