Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.54 KB | None | 0 0
  1. #!/bin/bash
  2. #----------------------------------------------------------------------------------
  3. #A script which magically makes a backup of a folder and stores it on the local HDD
  4. #It can also send the file via a secure shell tunnel or upload it on a ftp server
  5. #It also sends an e-mail containing information of the backup file.
  6. #Works on debian based distributions.
  7. #----------------------------------------------------------------------------------
  8.  
  9. echo -e "Welcome. This script does a backup of a folder/file.\nYou are running this script as user: `whoami`"
  10. if [[ `whoami` != "root" ]]; then
  11.     echo -e "\nI advise you to run this script as root, since you don't have the permissions for all the folders/files"
  12. fi
  13. echo -e "Checking if you have rsync installed..."
  14. if [[ ! -f /usr/bin/rsync ]]; then
  15. {
  16.     echo "You don't have rsync installed. Can i install it for you? [y/N]: "; read yn
  17.     if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  18.     {
  19.         if [[ `whoami` != "root" ]]; then
  20.         {
  21.             echo -e "You need to run this script as root, if you want to be notified via e-mail!\nQuitting"
  22.             exit 0
  23.         }
  24.         else
  25.         {
  26.             apt-get install rsync -y
  27.             echo -e "\nRsync installed!"
  28.         }
  29.         fi     
  30.     }
  31.     else
  32.     {
  33.         echo -e "You need rsync to make a backup!\nQuitting..."
  34.         exit 0
  35.     }
  36.     fi
  37. }
  38. fi
  39.  
  40. echo -e "\nPlease specify the folder you want to backup (example: /var/www/)" ; read -e path
  41. while [[ ! -f $path ]] && [[ ! -d $path ]] ; do
  42. {
  43.     echo -e "The folder/file doesn't exist. Try again: "; read -e path
  44. }
  45. done
  46. echo -e "------------------------------"
  47. if [[ -f $path ]]; then
  48.     echo -e "\nThe file exists. \nFile size: `du -h $path | tail -1 | awk '{print $1}'`B."
  49.  
  50. fi
  51. if [[ -d $path ]]; then {
  52.     echo -e "\nThe folder exists. \nFolder size: `du -h $path 2>/dev/null | tail -1 | awk '{print $1}'`B."
  53.     echo -e "Do you want to print out the folders/files in the directory? [y/N]: "; read yn
  54.     if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  55.         ls -a $path
  56.     fi
  57. }
  58. fi
  59. echo "Do you want to make a backup of the folder/file? [y/N]: "; read yn
  60. if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  61. {
  62.     echo "Do you want to be notified via e-mail when the backup is finished? [y/N]: "; read mailyn
  63. if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
  64. {
  65.     if [[ ! -f /usr/sbin/ssmtp ]]; then
  66.     {
  67.         echo "You don't have ssmtp installed. Can i install it for you? [y/N]: "; read yn
  68.         if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  69.         {
  70.             if [[ `whoami` != "root" ]]; then
  71.             {
  72.                 echo -e "You need to run this script as root, if you want to be notified via e-mail!\nQuitting"
  73.                 exit 0
  74.             }
  75.             else
  76.             {
  77.                 apt-get install ssmtp -y
  78.                 echo -e "\nAdding lines to the ssmtp config file..." #add your own settings here.
  79.                 echo "root=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
  80.                 echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf
  81.                 echo "rewriteDomain=" >> /etc/ssmtp/ssmtp.conf
  82.                 echo "hostname=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
  83.                 echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
  84.                 echo "AuthUser=darkmarkan" >> /etc/ssmtp/ssmtp.conf
  85.                 echo "AuthPass=test" >> /etc/ssmtp/ssmtp.conf
  86.                 echo -e "\nThe config file is now set up!"
  87.             }          
  88.             fi
  89.         }
  90.         fi
  91.     }
  92.     else
  93.     {
  94.         if cat /etc/ssmtp/ssmtp.conf | grep -q "darkmarkan"
  95.         then
  96.             echo "Looks like the config file is already set up."
  97.         else
  98.         {
  99.             echo -e "\nAdding lines to the ssmtp config file..." #add your own settings here.
  100.             echo "root=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
  101.             echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf
  102.             echo "rewriteDomain=" >> /etc/ssmtp/ssmtp.conf
  103.             echo "hostname=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
  104.             echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
  105.             echo "AuthUser=darkmarkan" >> /etc/ssmtp/ssmtp.conf
  106.             echo "AuthPass=thisisatest" >> /etc/ssmtp/ssmtp.conf
  107.             echo -e "\nThe config file is now set up!"
  108.         }
  109.         fi
  110.     }
  111.     fi
  112. }
  113. fi
  114.     echo -e "\nDo you want to store the files on the local HDD (1) or via ssh/ftp on a remote HDD (2)?: "; read yn
  115.     if [[ $yn == "1" ]]; then
  116.     {  
  117.         filesize=`du $path 2>/dev/null | tail -1 | awk '{print $1}'`
  118.         echo -e "Path to the backup directory where the .tar file will be stored \n(if you don't have one, we will create it for you): "; read -e path2
  119.         location=`df $path2 | tail -1 | awk '{print $4}'`
  120.         if [[ ! -d $path2 ]]; then
  121.         {
  122.             echo -e "The folder doesn't exists. Do you want me to create it? [y/N]: "; read yn
  123.             if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  124.                 mkdir $path2;
  125.                 location=`df $path2 | tail -1 | awk '{print $4}'`
  126.             else
  127.             echo -e "Fine! Create the folder yourself then.\nI'm quitting!"; exit 0
  128.             fi
  129.         }
  130.         fi
  131.         while [ "$filesize" -ge "$location" ]; do
  132.         {
  133.             echo -e "\nI'm sorry but there's not enough space left to store the backup."
  134.             echo -e "Your backup is "$filesize"KB big and the destination has only "$location"KB left."
  135.             echo -e "\nSelect a new destination: "; read -e path2
  136.             if [[ ! -d $path2 ]]; then
  137.             {
  138.                 echo -e "The folder doesn't exists. Do you want me to create it? [y/N]: "; read yn
  139.                 if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
  140.                     mkdir $path2
  141.                 else
  142.                     echo -e "Fine! Create the folder yourself then.\nI'm quitting!"; exit 0
  143.                 fi
  144.             }
  145.             fi
  146.             location=`df $path2 | tail -1 | awk '{print $4}'`
  147.         }
  148.         done
  149.         date=$(date +'%Y_%m_%d-%H_%M')
  150.         read -p "Press [Enter] key to make the backup."
  151.         tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" "$path2"; rm "$date.tar"
  152.         echo "Tarring the folder/file."
  153.         read -p "Press [Enter] key to send mail."
  154.         echo "Your $path backup is complete and waiting for you at the location $path2/" | ssmtp -v marko@freenode.si #change your mail
  155.         echo -e "Mail sent!\nBye."
  156.     }
  157.     else
  158.     {
  159.         echo -e "I will store the file on a remote HDD. \nPlease enter 1 for ssh or 2 for ftp: "; read sshftp
  160.         if [[ $sshftp == "1" ]]; then
  161.         {
  162.             echo "Give me the IP number of the ssh server: " ; read ip
  163.             echo "Give me the username: "; read username
  164.             date=$(date +'%Y_%m_%d-%H_%M')
  165.             read -p "Press [Enter] key to make the backup."
  166.             tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" pwd
  167.             echo "Tarring the folder/file."
  168.             echo "Storing the file via scp..."
  169.             scp "$date.tar" "$username"@"$ip":/home/"$username"/
  170.             if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
  171.             {
  172.                 echo "Your $path backup is complete and waiting for you at the location $username@$ip." | ssmtp -v marko@freenode.si #change your mail
  173.                 echo -e "Mail sent!\nBye."
  174.             }
  175.             fi
  176.             rm "$date.tar"
  177.         }
  178.         else
  179.         {
  180.             echo "Give me the IP number of the ftp server: " ; read ip
  181.             echo "Give me the username: "; read username
  182.             echo "Give me the password: "; read passwd
  183.             date=$(date +'%Y_%m_%d-%H_%M')
  184.             read -p "Press [Enter] key to make the backup."
  185.             tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" pwd
  186.             echo "Tarring the folder/file."
  187.             echo "Storing the file via ftp..."
  188.             ftp -n -v $ip << EOT
  189.             ascii
  190.             user $username $passwd
  191.             prompt
  192.             put $date.tar  
  193.             bye
  194. EOT
  195.             if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
  196.             {
  197.                 echo "Your $path backup is complete and waiting for you at the location $username@$ip." | ssmtp -v marko@freenode.si #change your mail
  198.                 echo -e "Mail sent!\nBye."
  199.             }
  200.             fi
  201.             rm "$date.tar"
  202.            
  203.         }
  204.         fi
  205.  
  206.            
  207.  
  208.     }
  209.     fi
  210. }
  211. else
  212.     echo -e "\nWhy did you run this script then?"; exit 0
  213. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement