1

Any ideas about a backup/restore script that can do the following:

A script that recursively backs up/restores files and dirs to/from an archive showing prompts for every dir whether to be backedup/restored or not. For example, lets say we have the following files and directories:

base path: /home/me/Downloads/test    
dir: /home/me/Downloads/test/1    
dir: /home/me/Downloads/test/1/1a    
file: /home/me/Downloads/test/1/filein1    
dir: /home/me/Downloads/test/2    
dir: /home/me/Downloads/test/3   
dir: /home/me/Downloads/test/4    
file: /home/me/Downloads/test/4/filein4    
file: /home/me/Downloads/test/backup.sh    
file: /home/me/Downloads/test/get_dirs_files.sh    
file: /home/me/Downloads/test/restore.sh    
file: /home/me/Downloads/test/script.sh    

The script should backup all files and dirs, but should prompt us yes/no for each dir and not files in the tree.

I am trying this:

#!/bin/bash

get_dirs= eval "find . -type d -exec echo \"{}\" \;"

echo $get_dirs

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 jcvpfw /backup.tar.bz2 $get_dirs
    echo "BACKUP COMPLETE"
    exit

else
    clear
    echo "| BAD OPTION! Select 1 or 2"
    echo "|--------------------------------------------------------------"
    echo "|  BACKUP YOUR SYSTEM: "
    echo $LIST
fi
done

Since I can get all the dirs by find . -type d -exec echo "{}" \;

And also interactively archive with w option of tar, that doesn't exactly backsup all the files in the current dir though.

3
  • 1
    Perhaps you should at least attempt a solution first, and then if you can't get it to work, you can post the code here and ask some specific questions about whichever parts of it aren't working. Your question as currently written just sounds like you're asking us to write your code for you, and/or that you think StackOverflow is a place to go shopping for ready-made solutions.
    – Mike Holt
    Mar 27, 2014 at 15:05
  • hey Mike, you are right! I was trying with different things let me paste my current script :) Mar 27, 2014 at 15:07
  • If you want to prompt on each found directory, perhaps you could use the -ok option of find.
    – lurker
    Mar 27, 2014 at 16:38

1 Answer 1

0

You may try something like this:

#/bin/bash

default_path=/home/user # or something else

if [[ -z ${1} ]]; then
    backup_path=${default_path}
else
    backup_path=${1} # sets if script got an argument with backup path
fi

all_dirs=`find ${backup_path} -type d`
backup_list=""

for dir in `echo ${all_dirs}`; do
    echo "${dir} [y/n/skip]: "
    read answer
    if [[ ${answer} == "y" ]]; then
        backup_list="`echo ${backup_list}` ${dir}"
    fi

    if [[ ${answer} == "skip" ]]; then
        break
    fi
done

tar -czpf backupfile.tar.gz ${backup_list}

exit 0

I think the code is very simple even without comments. It isn't a solution for a whole task, but you can use the idea.

2
  • What's the point of `echo ${backup_list}` over ${backup_list}? Looks like useless use of backticks to me. And the shebang needs a !. The string comparison operator is = (not ==, which is a bash nonportability sillyness that should die).
    – Jens
    Jan 23, 2015 at 11:00
  • I don't know another way how to add new parts to the existing string variable in bash (i'll be grateful for more smart solution). You're right, character "=" uses for string comparation, but "==" works well too.
    – fesia
    Jan 23, 2015 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.