#!/bin/bash # --------------------------------------------------------------- # Company: University of Rostock # Engineer: Tim Brockmann # Create Date: 17.10.2023 # Project Name: APU-testbed # File Name: start_backup.sh # Target OS: Tiny Core Linux 14.0 # Dependencies: - rsync v3.1.1 # Task: # This script is part of the pre-configured tiny core and # initializes the backup process. Therefore it fetches the # ---------------------------------------------------------------- # 1. create logfile and prepare local and remote paths # ---------------------------------------------------------------- # define timezone and update time export TZ=Europe/Berlin sudo ntpd -q sh /etc/init.d/settime.sh sleep 10 # wait to set the correct ntp time # define the control pc as the central testbed instance export CTRLPC=192.168.0.1 # get execution time of the script export EXEC_TIME=$(date "+%Y-%m-%d_%T") # Get my current IP address (eth0) export IP_ADDRESS=$(ifconfig eth0 | grep -o -E 'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}' | awk -F ':' '{print $2}') # Perform the nslookup and store the result in a variable export NSLOOKUP_RESULT=$(nslookup $IP_ADDRESS) # Extract the hostname and path's from the nslookup result using awk export APU_HOSTNAME=$(echo "$NSLOOKUP_RESULT" | awk '/Name:/{getline; print}' | awk '{print $4}') export LOG_FILE_NAME="${EXEC_TIME}_${APU_HOSTNAME}_backup.log" export LOCAL_LOG_FILE="/home/tc/${LOG_FILE_NAME}" # create empty log file touch $LOCAL_LOG_FILE echo "Start -----------------------------------------------------------------" | tee -a ${LOCAL_LOG_FILE} time_to_wait=$((0 + $RANDOM % 15)) echo "Waiting random time ($time_to_wait seconds) to avoid a simultaneous access to the rsync server" | tee -a ${LOCAL_LOG_FILE} sleep $time_to_wait # log path on cotrol pc export REMOTE_PATH="/home/tftproot/" export REMOTE_LOG_PATH="${REMOTE_PATH}backup_logs/${APU_HOSTNAME}/" export BACKUP_SCRIPT_NAME="apu_backup_plan.sh" export PART_SCRIPT_NAME="partitioning_ssd.sh" get_backup_instructions() { # gets apu_backup_plan.sh from control pc echo "Start getting backup plan....." | tee -a ${LOCAL_LOG_FILE} rsync -ahxL --stats --delete -e 'ssh -o StrictHostKeyChecking=no' apu@"${CTRLPC}":${REMOTE_PATH}${BACKUP_SCRIPT_NAME} /home/tc/ | tee -a ${LOCAL_LOG_FILE} rsync -ahxL --stats --delete -e 'ssh -o StrictHostKeyChecking=no' apu@"${CTRLPC}":${REMOTE_PATH}${PART_SCRIPT_NAME} /home/tc/ | tee -a ${LOCAL_LOG_FILE} echo "Getting backupplan done ............." | tee -a ${LOCAL_LOG_FILE} } run_backup() { bash /home/tc/${BACKUP_SCRIPT_NAME} | /opt/scripts/predate.sh | tee -a ${LOCAL_LOG_FILE} } transfer_logfile() { # create directory rsync -ahx --inplace --stats --delete -e 'ssh -o StrictHostKeyChecking=no' /home/tc/*.log apu@"${CTRLPC}":"${REMOTE_LOG_PATH}" # rsync log file } # create remote directory (-p .. make parent directories as needed) ssh apu@${CTRLPC} "mkdir -p "${REMOTE_LOG_PATH}"" # ------------------------------------------------------------------------------------------------ # 2. copy the backup script from the ctrlpc # ------------------------------------------------------------------------------------------------ get_backup_instructions # ------------------------------------------------------------------------------------------------ # 3. run the backup script # ------------------------------------------------------------------------------------------------ run_backup # ------------------------------------------------------------------------------------------------ # 4. copy the logfile to the ctrlpc # ------------------------------------------------------------------------------------------------ echo "Done ------------------------------------------------------------------" | tee -a ${LOCAL_LOG_FILE} transfer_logfile # ------------------------------------------------------------------------------------------------ # on node side the bootlocal-script starts the run-script: ## run script # 1. the run script creates an empty logfile with the hostname in the identifier (e.g. apu84_tc.log) # 2. get backup script and backup plan file (backup_plan.conf) from ctrl pc # 3. if there is no one, write it to the logfile and stop this run # -> otherwise # 4. if both files are loaded, run the backup script # ## stop run (part of run script) # 1. close log file # 2. rsync the logfile to the control pc (specific location is nedded -> node_logs) # 3. reboot (or wait for reboot -> still not clear -> leave configurable)