Understand, Learn and Implement.. LINUX / UNIX DEVOPS CONTAINERS OPENSHIFT KUBERNETES DEVOPS TOOLS VIRTUALIZATION STORAGE BACKUP SCRIPTS MONITORING MIDDLEWARE

Friday, September 12, 2014

Bacup and Restore Script Linux

4:42 AM Posted by vinod No comments
Backup Script:
This is how it looks on screen:
Code:
|-------------------------------------------------------------
|  IT'S RECOMMENDED TO RUN THIS SCRIPT BEFORE GNOME LOGIN
|-------------------- Press CTRL+ALT+F1 at the GDM login
|-------------------------------------------------------------
|  BACKUP YOUR SYSTEM:
1) Backup
2) Exit
#?
You simply just type: sudo backup and choose with your number keys the wanted action and press Enter on the keyboard.
OK, lets start.
Open a terminal window and type:
$ vi backup.sh
Copy the following code into your empty text file:
Code:
#!/bin/bash
#
# A very simple backup script created by: 
# Vinod Kumar
# http://www.linunix.in
#
# This script is related to the article:
# Howto: Backup & Restore script. Part 1
# Published at: http://ubuntuforums.org/showthread.php?p=249408
#
# Instructions: 
# Change the values of "STORAGE_MEDIA" and "ROOT_EXCLUDE_DIRS"
# Name this file "restore", make it "executable" and put it in "/bin"
# Invoke by: "sudo backup"
#
# Feel free to modify however you want. If you make something better, 
# please post it at the ubuntuforum.org thread above :-)
#
#------------------------------------------------------------------------------------
#    CHANGE THE VALUES BELOW TO SUIT YOUR CONFIGURATION 
#------------------------------------------------------------------------------------

ROOT="/*"
ROOT_EXCLUDE_DIRS="--exclude=/lost+found --exclude=/proc --exclude=/mnt --exclude=/sys --exclude=/backup.tgz"

#------------------------------------------------------------------------------------
#             DO NOT CHANGE ANYTHING BELOW THIS LINE           
#------------------------------------------------------------------------------------
if [ "$USER" != "root" ]; then 
    echo "You are not root user, use: sudo backup"
    exit
fi
clear
echo "|-------------------------------------------------------------" 
echo "|  IT'S RECOMMENDED TO RUN THIS SCRIPT BEFORE GNOME LOGIN  "
echo "|-------------------- Press CTRL+ALT+F1 at the GDM login"
echo "|-------------------------------------------------------------"
echo "|  BACKUP YOUR SYSTEM: "
OPTIONS="Backup Exit"
LIST="1) Backup 2) Exit" 

select opt in $OPTIONS; do
if [ "$opt" = "Exit" ]; then
    clear
    exit

elif [ "$opt" = "Backup" ]; then
    tar cvpfz /backup.tgz $ROOT $ROOT_EXCLUDE_DIRS
    echo "BACKUP COMPLETE"
    exit
else
    clear
    echo "| BAD OPTION! Select 1 or 2"
    echo "|--------------------------------------------------------------"
    echo "|  BACKUP YOUR SYSTEM: "
    echo $LIST
fi
done
Now lets modify the script (If you have to...). By default, it makes a backup of everything in "/" so you probably don't have to change the "ROOT" configuration. Also the "ROOT_EXCLUDE_DIRS" may be left alone, but double check that it is like you want it to be according to Heliodes guide.
Save the file, exit the text editor and type in the terminal:
$ sudo chmod 775 backup.sh
$ sudo mv backup.sh /bin/

Now all you have todo to start the script is to type:
$ sudo backup
You can now try to open it, but exit with the option "2" and press Enter.
THE RESTORE SCRIPT:
This is how it looks on screen:
Code:
|-------------------------------------------------------------
|  IT'S RECOMMENDED TO RUN THIS SCRIPT BEFORE GNOME LOGIN
|-------------------- Press CTRL+ALT+F1 at the GDM login
|-------------------------------------------------------------
|  RESTORE YOUR SYSTEM:
1) Restore
2) Exit
#?
Again open a terminal window and type:
$ gedit restore
Copy the following code into your empty text file:
Code:
#!/bin/bash
#
# A very simple backup restore script created by: 
# Vinod Kumar
# http://www.linunix.in
#
# This script is related to the article:
# Howto: Backup & Restore script. Part 1
# Published at: http://ubuntuforums.org/showthread.php?p=249408
#
# Instructions: 
# Change the values of "BACKUP_FILE" and "CREATE_DIRS"
# Name this file "restore", make it "executable" and put it in "/bin"
# Invoke by: "sudo restore"
#
# Feel free to modify however you want. If you make something better, 
# please post it at the ubuntuforum.org thread above :-)
#
#---------------------------------------------------------------------------------
# CHANGE THE VALUES BELOW TO SUIT YOUR CONFIGURATION 
#---------------------------------------------------------------------------------

BACKUP_FILE="backup.tgz"
CREATE_DIRS="/proc /sys /mnt /mnt/mounted_drive"

#---------------------------------------------------------------------------------
#           DO NOT CHANGE ANYTHING BELOW THIS LINE              
#---------------------------------------------------------------------------------
if [ "$USER" != "root" ]; then
    echo "You are not root user, use: sudo restore"
    exit
fi
clear
echo "|-------------------------------------------------------------"
echo "|  IT'S RECOMMENDED TO RUN THIS SCRIPT BEFORE GNOME LOGIN  "
echo "|-------------------- Press CTRL+ALT+F1 at the GDM login"
echo "|-------------------------------------------------------------"
echo "|  RESTORE YOUR SYSTEM:"
tput sgr0
OPTIONS="Restore Exit"
LIST="1) Restore 2) Exit"

select opt in $OPTIONS; do
if [ "$opt" = "Exit" ]; then
    clear
    exit
elif [ "$opt" = "Restore" ]; then
    tar xvpfz $BACKUP_FILE -C /
    echo "|  RESTORE COMPLETE "
    if [[ -e "/proc" ]]; then
        echo "$CREATE_DIRS allready exists! "
    else
        mkdir $CREATE_DIRS
        echo "$CREATE_DIRS are created! "
    fi
    exit
else
    clear
    echo "| BAD OPTION! Select 1 or 2"
    echo "|--------------------------------------------------------------"
    echo "|  RESTORE YOUR SYSTEM:"
    echo $LIST
fi
done
Also this may need to be modified a bit. Change the value of "CREATE_DIRS" to fit your system. All you /mnt/XXXhave to be put here etc.
Save the file, exit the text editor and type in the terminal:
$ sudo chmod 775 backup 
$ sudo mv backup /bin/
Now all you have todo to start the script is to type:
$ sudo backup
You can now try to open it, but exit with the option "2" and press Enter.
Note: If you crash your system so bad that you can't start even start console, you have to use a liveCD and use the chroot command then wake the script by sudo /bin/restore

0 comments:

Post a Comment