Bash - Create a backup of a file or directory

# @param String $path when path is a file, a copy is made with .bak appended to it's name. # if path is a directory, a compressed tarball will be made of the directory # function bak() { bakfile="$1.bak" filename="$bakfile" i=1 while [ -e $filename ] do filename="${bakfile}.${i}" (( i++ )) done if [ -d $1 ]; then dir=$1 dir=${dir%/} echo $dir tarfile="${dir}.tar.gz" tar -zcvf $tarfile $dir elif [ -f $1 ]; then cp $1 $filename else echo "BAK: unsupported item type - must be file or directory" fi }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.