Ticker

6/recent/ticker-posts

Backup and Restoration script in a Linux

First, we have created ./script directory.

Then we have applied for full permission for this directory. The below command runs on Linux.

#sudo chmod 777 .script 


This script will help you to take a backup and while restoring the backup, it will list all the backups in the directory. I hope it will help you somewhere so please save this script.

Backup Script



Then We added the below code to the .script directory.


#!/bin/bash

SRC="Source Path"
DES="Destination Path"
SRCFILE=Filename
#DESFILE=sampleenv_$(date +%-Y-%-m-%-d).tar.gz
DESFILE=sampleenv_$(date +%Y.%m.%d-%H.%M.%S).tar.gz # Destination Filename
BKUPFILE=sampleenv_bak_$(date +%Y.%m.%d-%H.%M.%S) # Backup Filename
#BKUPFILE=sampleenv_bak_$(date +%-Y-%-m-%-d)

echo "Select Appropriate Option:"
echo "Enter 1 to Backup the code..."
echo "Enter 2 to Restore the code... "
echo "Type Exit to exit from the script... "

while :
do
read INPUT_STRING
case $INPUT_STRING in


cd $SRC && tar -cvf $DESFILE $SRCFILE
# You can also add SCP command to move the backup on remote server
break
;;

#Add Remote server option
echo "Please select the backup which you want to restore"
cd $SRC
files=$(ls sampleenv*.tar.gz)
i=1

 for j in $files
do
echo "$i.$j"
file[i]=$j
i=$(( i + 1 ))
done

 echo "Enter your choice"
read input
echo "You selected the backup ${file[$input]}"
mv -f $SRCFILE $BKUPFILE
tar -xvf ${file[$input]}

 break
;;
Exit)
echo "Exiting..."
break
;;
*)
echo "Sorry, Select the appropriate option..."
;;
esac
done
echo
echo "Completed!!!"


Then run the script directory command.

#sudo ./script

Then started the Backup source to destination folder or file.
Reactions

Post a Comment

0 Comments