[354] | 1 | #!/bin/bash
|
---|
| 2 | #######################################################################
|
---|
| 3 | # This is a script to execute daily/weekly backups over NFS #
|
---|
| 4 | # with mondorescue set of tools made by Hugo Rabson #
|
---|
| 5 | # http://www.mondorescue.com <-- Visit and contribute ! #
|
---|
| 6 | # This script made by Phazeman with a great help of #
|
---|
| 7 | # crash3m @ #mandrake, twkm, Lurch and Speuler @ #bash on #
|
---|
| 8 | # irc.freenode.net. It has been written to make it easy to use on #
|
---|
| 9 | # several machines at the same time. #
|
---|
| 10 | # This product is distributed under the GPL license and is issued #
|
---|
| 11 | # without warrant of anytime against damages, loss of property, or #
|
---|
| 12 | # loss of profita. #
|
---|
| 13 | #######################################################################
|
---|
| 14 |
|
---|
| 15 | # Set the debug level to 1 if you want to print all the params and exit
|
---|
| 16 | # Otherwise set to 0
|
---|
| 17 |
|
---|
| 18 | debug="0"
|
---|
| 19 |
|
---|
| 20 | # Get the date
|
---|
| 21 | date=`date +"%d-%m-%Y"`
|
---|
| 22 |
|
---|
| 23 | function check_conf {
|
---|
| 24 | if [ -f "/etc/backup-kungfu.conf" ]
|
---|
| 25 | then
|
---|
| 26 | echo -e "\nConfiguration file found. Proceeding...\n"
|
---|
| 27 | source /etc/backup-kungfu.conf
|
---|
| 28 | else
|
---|
| 29 | echo -e "\n\033[40m\033[1;31mERROR: No configuration file found ! \nExiting now...\033[0m\n"
|
---|
| 30 | exit 1
|
---|
| 31 | fi
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | function check_mondo {
|
---|
| 35 | if [ -x $mondo_path/$mondo_exe ]
|
---|
| 36 | then
|
---|
| 37 | echo -e "mondoarchive executable found. Proceeding..."
|
---|
| 38 | else
|
---|
| 39 | echo -e "\033[40m\033[1;31mERROR: mondo_exe variable is set wrong.\nPlease edit the config file and rerun the script.\nExiting now...\033[0m\n"
|
---|
| 40 | exit 1
|
---|
| 41 | fi
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | function check_backup_dir {
|
---|
| 45 | if [ -f $backup_dir ]
|
---|
| 46 | then
|
---|
| 47 | echo -e "\033[40m\033[1;31mERROR: backup_dir variable is set wrong.\nPlease edit the config file and rerun the script.\nExiting now...\033[0m\n"
|
---|
| 48 | exit 1
|
---|
| 49 | else
|
---|
| 50 | echo -e "The backup dir found. Proceeding...\n"
|
---|
| 51 | fi
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | function check_path {
|
---|
| 55 | if [ -x $mondo_path/$mondo_exec ]
|
---|
| 56 | then
|
---|
| 57 | if [ "$PATH" == "" ]
|
---|
| 58 | then
|
---|
| 59 | echo -e "Running as cron job. Exporting path...\n"
|
---|
| 60 | export PATH=$mondo_path
|
---|
| 61 | fi
|
---|
| 62 | else
|
---|
| 63 | echo -e "\033[40m\033[1;31mERROR: No mondoarchive executable found in $mondo_path/.\nPlease edit the config file and then run again.\nExiting now...\033[0m\n"
|
---|
| 64 | exit 1
|
---|
| 65 | fi
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | function remove_garbage {
|
---|
| 69 | echo -e "Removing the files left from previous mondoarchive run...\n"
|
---|
[1051] | 70 | rm -rf /var/cache/mondo
|
---|
[354] | 71 | rm -rf ${backup_dir}/mondo*
|
---|
| 72 | rm -rf ${backup_dir}/tmp*
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | function check_root {
|
---|
| 76 | if [ "$UID" != "0" ]
|
---|
| 77 | then
|
---|
| 78 | echo -e "\n\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Exiting...\033[0m\n"
|
---|
| 79 | exit 1
|
---|
| 80 | fi
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | function delete_old_backup {
|
---|
| 84 | answer = "x"
|
---|
| 85 | while "$(answer)" != ["y" | "Y" | "n" | "N"]
|
---|
| 86 | do
|
---|
| 87 | echo -n "Do you want to delete all the previous \
|
---|
| 88 | backups stored in $(backup_dir)/$(comp_name) ? (y/n) "
|
---|
| 89 | read answer
|
---|
| 90 | case "$(answer)" in
|
---|
| 91 | ["y" | "Y"] )
|
---|
| 92 | echo -e "Removing the old backup files now...\n"
|
---|
| 93 | rm -rf ${backup_dir}/${comp_name}*
|
---|
| 94 | ;;
|
---|
| 95 | *)
|
---|
| 96 | ;;
|
---|
| 97 | esac
|
---|
| 98 | done
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | function move_all {
|
---|
| 102 | # Remove all the previous backups
|
---|
| 103 | # echo -e "Removing the old backup files now...\n"
|
---|
| 104 | # rm -rf ${backup_dir}/${comp_name}*
|
---|
| 105 |
|
---|
| 106 | # Create the new folder and move all the iso and floppy images to it
|
---|
| 107 | echo -e "Creating the new backup directory and moving the files there...\n"
|
---|
| 108 | mkdir -p ${backup_dir}/${comp_name}_${date}/images
|
---|
[1051] | 109 | cp /var/cache/mindi/* ${backup_dir}/${comp_name}_${date}/images/
|
---|
[354] | 110 |
|
---|
| 111 | # If you want just move the iso files without renaming them just uncomment the next line
|
---|
| 112 | # and comment the next for loop
|
---|
| 113 | mv $backup_dir/*.iso ${backup_dir}/${comp_name}_${date}/
|
---|
| 114 |
|
---|
| 115 | #for i in `ls $(backup_dir)/*.iso`
|
---|
| 116 | #do
|
---|
| 117 | # mv $(backup_dir)/$(i) ${backup_dir}/${comp_name}_${date}/$(comp_name)_backup_$(date)_$i
|
---|
| 118 | #done
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | function print_params {
|
---|
| 122 | echo -e "Debug = $debug\n\
|
---|
| 123 | Mondo executable = $mondo_exe\n\
|
---|
| 124 | Mondo path = $mondo_path\n\
|
---|
| 125 | Backup Dir = $backup_dir\n\
|
---|
| 126 | Excludes = $excludes\n\
|
---|
| 127 | Comp Name = $comp_name\n\
|
---|
| 128 | Mondo Params = $params\n\
|
---|
| 129 | Boot Loader = $bootloader\n\
|
---|
| 130 | Launch from dir = $from_dir\n\
|
---|
| 131 | Date = $date\n\
|
---|
| 132 | Mondo execution = '$archiveit'\n"
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | function cd_from_dir {
|
---|
| 136 | if [ -d $from_dir ]
|
---|
| 137 | then
|
---|
| 138 | cd $from_dir
|
---|
| 139 | echo -e "Entering the \"From\" directory...\n"
|
---|
| 140 | else
|
---|
| 141 | echo -e "\033[40m\033[1;31mERROR: The from_dir variable is set wrong.\nPlease edit the config file and rerun the script.\nExiting now...\033[0m\n"
|
---|
| 142 | exit 1
|
---|
| 143 | fi
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | function set_exec {
|
---|
| 147 | archiveit="$mondo_path/${mondo_exe} ${params} -d ${backup_dir} -l ${bootloader} -E '${excludes}' &> /dev/null"
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | function do_cleanup {
|
---|
| 151 | remove_garbage;
|
---|
| 152 | exit 1;
|
---|
| 153 | }
|
---|
| 154 | trap do_cleanup INT
|
---|
| 155 |
|
---|
| 156 | function main {
|
---|
| 157 | if [ $debug == "0" ]
|
---|
| 158 | then # Execute the real backup
|
---|
| 159 | #Verify if root
|
---|
| 160 | check_root;
|
---|
| 161 |
|
---|
| 162 | # Check if mondo_exec set correct
|
---|
| 163 | check_mondo;
|
---|
| 164 |
|
---|
| 165 | # Check if the conf file created in /etc
|
---|
| 166 | check_conf;
|
---|
| 167 |
|
---|
| 168 | # Check if the backup_dir set correct
|
---|
| 169 | check_backup_dir;
|
---|
| 170 |
|
---|
| 171 | # Check mondo_path
|
---|
| 172 | check_path;
|
---|
| 173 |
|
---|
| 174 | # Set the execution command
|
---|
| 175 | set_exec;
|
---|
| 176 |
|
---|
| 177 | # Remove the garbage from the last run
|
---|
| 178 | remove_garbage;
|
---|
| 179 |
|
---|
| 180 | # Enter the big directory to launch the backup
|
---|
| 181 | cd_from_dir;
|
---|
| 182 |
|
---|
| 183 | #execute the backup
|
---|
| 184 | echo -e "Executing the backup now. Please hold on, it can take few howrs.\nLook in /var/log/mondoarchive.log for current status of the backup.\n"
|
---|
| 185 | eval $archiveit
|
---|
| 186 |
|
---|
| 187 | # Delete old backups from the dir
|
---|
| 188 | #delete_old_backup;
|
---|
| 189 |
|
---|
| 190 | # Copy the mindi images to the backup folder and clean up the mess
|
---|
| 191 | move_all;
|
---|
| 192 |
|
---|
| 193 | # Remove all the garbage left afer mondoarchive run
|
---|
| 194 | remove_garbage;
|
---|
| 195 | else # Print all the params for debug
|
---|
| 196 | check_root;
|
---|
| 197 | check_conf;
|
---|
| 198 | check_path;
|
---|
| 199 | set_exec;
|
---|
| 200 | print_params;
|
---|
| 201 | fi
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | # Execute the main function
|
---|
| 205 | main;
|
---|
| 206 |
|
---|