Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #----------------------------------------------------------------------------------
- #A script which magically makes a backup of a folder and stores it on the local HDD
- #It can also send the file via a secure shell tunnel or upload it on a ftp server
- #It also sends an e-mail containing information of the backup file.
- #Works on debian based distributions.
- #----------------------------------------------------------------------------------
- echo -e "Welcome. This script does a backup of a folder/file.\nYou are running this script as user: `whoami`"
- if [[ `whoami` != "root" ]]; then
- echo -e "\nI advise you to run this script as root, since you don't have the permissions for all the folders/files"
- fi
- echo -e "Checking if you have rsync installed..."
- if [[ ! -f /usr/bin/rsync ]]; then
- {
- echo "You don't have rsync installed. Can i install it for you? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- {
- if [[ `whoami` != "root" ]]; then
- {
- echo -e "You need to run this script as root, if you want to be notified via e-mail!\nQuitting"
- exit 0
- }
- else
- {
- apt-get install rsync -y
- echo -e "\nRsync installed!"
- }
- fi
- }
- else
- {
- echo -e "You need rsync to make a backup!\nQuitting..."
- exit 0
- }
- fi
- }
- fi
- echo -e "\nPlease specify the folder you want to backup (example: /var/www/)" ; read -e path
- while [[ ! -f $path ]] && [[ ! -d $path ]] ; do
- {
- echo -e "The folder/file doesn't exist. Try again: "; read -e path
- }
- done
- echo -e "------------------------------"
- if [[ -f $path ]]; then
- echo -e "\nThe file exists. \nFile size: `du -h $path | tail -1 | awk '{print $1}'`B."
- fi
- if [[ -d $path ]]; then {
- echo -e "\nThe folder exists. \nFolder size: `du -h $path 2>/dev/null | tail -1 | awk '{print $1}'`B."
- echo -e "Do you want to print out the folders/files in the directory? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- ls -a $path
- fi
- }
- fi
- echo "Do you want to make a backup of the folder/file? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- {
- echo "Do you want to be notified via e-mail when the backup is finished? [y/N]: "; read mailyn
- if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
- {
- if [[ ! -f /usr/sbin/ssmtp ]]; then
- {
- echo "You don't have ssmtp installed. Can i install it for you? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- {
- if [[ `whoami` != "root" ]]; then
- {
- echo -e "You need to run this script as root, if you want to be notified via e-mail!\nQuitting"
- exit 0
- }
- else
- {
- apt-get install ssmtp -y
- echo -e "\nAdding lines to the ssmtp config file..." #add your own settings here.
- echo "root=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
- echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf
- echo "rewriteDomain=" >> /etc/ssmtp/ssmtp.conf
- echo "hostname=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
- echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
- echo "AuthUser=darkmarkan" >> /etc/ssmtp/ssmtp.conf
- echo "AuthPass=test" >> /etc/ssmtp/ssmtp.conf
- echo -e "\nThe config file is now set up!"
- }
- fi
- }
- fi
- }
- else
- {
- if cat /etc/ssmtp/ssmtp.conf | grep -q "darkmarkan"
- then
- echo "Looks like the config file is already set up."
- else
- {
- echo -e "\nAdding lines to the ssmtp config file..." #add your own settings here.
- echo "root=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
- echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf
- echo "rewriteDomain=" >> /etc/ssmtp/ssmtp.conf
- echo "hostname=darkmarkan@gmail.com" >> /etc/ssmtp/ssmtp.conf
- echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
- echo "AuthUser=darkmarkan" >> /etc/ssmtp/ssmtp.conf
- echo "AuthPass=thisisatest" >> /etc/ssmtp/ssmtp.conf
- echo -e "\nThe config file is now set up!"
- }
- fi
- }
- fi
- }
- fi
- 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
- if [[ $yn == "1" ]]; then
- {
- filesize=`du $path 2>/dev/null | tail -1 | awk '{print $1}'`
- 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
- location=`df $path2 | tail -1 | awk '{print $4}'`
- if [[ ! -d $path2 ]]; then
- {
- echo -e "The folder doesn't exists. Do you want me to create it? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- mkdir $path2;
- location=`df $path2 | tail -1 | awk '{print $4}'`
- else
- echo -e "Fine! Create the folder yourself then.\nI'm quitting!"; exit 0
- fi
- }
- fi
- while [ "$filesize" -ge "$location" ]; do
- {
- echo -e "\nI'm sorry but there's not enough space left to store the backup."
- echo -e "Your backup is "$filesize"KB big and the destination has only "$location"KB left."
- echo -e "\nSelect a new destination: "; read -e path2
- if [[ ! -d $path2 ]]; then
- {
- echo -e "The folder doesn't exists. Do you want me to create it? [y/N]: "; read yn
- if [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" ]]; then
- mkdir $path2
- else
- echo -e "Fine! Create the folder yourself then.\nI'm quitting!"; exit 0
- fi
- }
- fi
- location=`df $path2 | tail -1 | awk '{print $4}'`
- }
- done
- date=$(date +'%Y_%m_%d-%H_%M')
- read -p "Press [Enter] key to make the backup."
- tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" "$path2"; rm "$date.tar"
- echo "Tarring the folder/file."
- read -p "Press [Enter] key to send mail."
- echo "Your $path backup is complete and waiting for you at the location $path2/" | ssmtp -v marko@freenode.si #change your mail
- echo -e "Mail sent!\nBye."
- }
- else
- {
- echo -e "I will store the file on a remote HDD. \nPlease enter 1 for ssh or 2 for ftp: "; read sshftp
- if [[ $sshftp == "1" ]]; then
- {
- echo "Give me the IP number of the ssh server: " ; read ip
- echo "Give me the username: "; read username
- date=$(date +'%Y_%m_%d-%H_%M')
- read -p "Press [Enter] key to make the backup."
- tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" pwd
- echo "Tarring the folder/file."
- echo "Storing the file via scp..."
- scp "$date.tar" "$username"@"$ip":/home/"$username"/
- if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
- {
- echo "Your $path backup is complete and waiting for you at the location $username@$ip." | ssmtp -v marko@freenode.si #change your mail
- echo -e "Mail sent!\nBye."
- }
- fi
- rm "$date.tar"
- }
- else
- {
- echo "Give me the IP number of the ftp server: " ; read ip
- echo "Give me the username: "; read username
- echo "Give me the password: "; read passwd
- date=$(date +'%Y_%m_%d-%H_%M')
- read -p "Press [Enter] key to make the backup."
- tar -zcvf "$date.tar" "$path" && rsync -avh "$date.tar" pwd
- echo "Tarring the folder/file."
- echo "Storing the file via ftp..."
- ftp -n -v $ip << EOT
- ascii
- user $username $passwd
- prompt
- put $date.tar
- bye
- EOT
- if [[ $mailyn == "y" || $mailyn == "Y" || $mailyn == "Yes" || $mailyn == "yes" ]]; then
- {
- echo "Your $path backup is complete and waiting for you at the location $username@$ip." | ssmtp -v marko@freenode.si #change your mail
- echo -e "Mail sent!\nBye."
- }
- fi
- rm "$date.tar"
- }
- fi
- }
- fi
- }
- else
- echo -e "\nWhy did you run this script then?"; exit 0
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement