Saturday, February 20, 2016

Bash shell script: back up production server

#!/bin/bash

#back up production on development server

# options added 11.23.2015

#written by Ken Sipe

# variables
today=$(date +%y%m%d)
c_source=/sever2-cgi-bin
d_source=/sever2-docs
t_source=/server2-tid
c_target=/usr2/cgi-bin
d_target=/usr2/docs
t_target=/usr3/docs/tid

if [ $EUID -eq 0 ]
then
                while getopts acdht opt
        do
                case "$opt" in
                    h)
                        # help

                        echo "Options:"
                        echo "-a. Backup all webserver files (e.g. cgi-bin, docs, tid directories)"
                        echo "-c. Backup the cgi-bin directory only"
                        echo "-d. Backup the docs directory only"
                        echo "-t. Backup the tid directory only"
                        echo "Note: You can select more than one option (e.g. -ct or -d -c)"
                        echo ;;
                    a)
                        #cgi-bin routine

                        if cd $c_source
                        then
                                tar -cvf - . | (cd $c_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin_t3p.$today
                        else
                                echo "Directory $c_source does not exist"
                        fi

                        #docs routine

                        if cd $d_source
                        then
                                tar -cvf - . | (cd $d_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_docs_t3p.$today
                        else
                                echo "Directory $d_source does not exist"
                        fi

                        #tid routine

                        if cd $t_source
                        then
                                tar -cvf - . | (cd $t_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_tid_t3p.$today
                        else
                                echo "Directory $t_source does not exist"
                        fi

                        #list results

                        echo "Directory $c_target log saved"
                        ls -lR $c_target > /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin.$today

                        echo "Directory $d_target log saved"
                        ls -lR $d_target > /homeb/d54712/usr-scripts-save/backup/logs/log_docs.$today

                        echo "Directory $t_target log saved"
                        ls -lR $t_target > /homeb/d54712/usr-scripts-save/backup/logs/log_tid.$today ;;
                    c)

                        #cgi-bin routine

                        if cd $c_source
                        then
                                tar -cvf - . | (cd $c_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin_t3p.$today
                        else
                                echo "Directory $c_source does not exist"
                        fi

                        #list results

                        echo "Directory $c_target log saved"
                        ls -lR $c_target > /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin.$today ;;

                    d)

                        #docs routine

                        if cd $d_source
                        then
                                tar -cvf - . | (cd $d_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_docs_t3p.$today
                        else
                                echo "Directory $d_source does not exist"
                        fi

                        #list results

                        echo "Directory $d_target log saved"
                        ls -lR $d_target > /homeb/d54712/usr-scripts-save/backup/logs/log_docs.$today ;;

                    t)

                        #tid routine

                        if cd $t_source
                        then
                                tar -cvf - . | (cd $t_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_tid_t3p.$today
                        else
                                echo "Directory $t_source does not exist"
                        fi

                        #list results

                        echo "Directory $t_target log saved"
                        ls -lR $t_target > /homeb/d54712/usr-scripts-save/backup/logs/log_tid.$today ;;

                esac
        done
else
        echo "This script must be run as root"
        echo "User is $USER"
fi

No comments:

Post a Comment