backup - ademozel/test GitHub Wiki

#!/bin/bash
DIRECTORY=""
NOW=“$(date ‘+%d_%m_%Y-%H%M’)”
counter=0
#

  1. Get input from user
    cat <<EOF
    This script backs up the /home/$USERNAME directory to /backup, as well as any files and directories you specify here.
    Type the name of a directory or file name you want to back up, then press enter.
    Repeat for each directory or file name you want to back up.
    When done (or if you do not want to back up additional files or directories) just press Enter.
    EOF
    read DIRECTORY
  1. Check if $DIRECTORY string is non-zero
  2. (if it is, user pressed enter as first action)
    if [ -n “$DIRECTORY” ]
    then
    while test -n “$DIRECTORY
    do
    TOBACKUP[$counter]=“$DIRECTORY
    ((counter++))
    DIRECTORY=""
    read DIRECTORY
    done
    fi
  1. Back up the directories entered by user
    for i in ${TOBACKUP[@]}
    do
    echo -e “Backing up /home/$i to /backup/”
    rsync -av —no-whole-file $i /backup > \
    /backup/backup-log_"$NOW" 2>/backup/backup-errorlog_"$NOW"
    done
  1. Send log files via mail to user
    if test “$?” -eq 0
    then
    mail -s “Backup successful” “$USER” < /backup/backup-log_"$NOW"
    else
    mail -s “Some error occurred during backup” “$USER” < /backup/backup-errorlog_"$NOW"
    fi
    date
  2. The same, a bit more complicated:
    #for ((i=0;i<${#TOBACKUP[@]};i++))
    #do
  3. echo -e “\nBacking up ${TOBACKUP[$i]} to /backup”
  4. rsync -av —no-whole-file “${TOBACKUP[$i]}” /backup #done
⚠️ **GitHub.com Fallback** ⚠️