jLinux

Untitled

Dec 11th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.60 KB | None | 0 0
  1. # Create a backup of a file with the timestamp before the extension
  2. function bkup {
  3.     target=$1
  4.     full_name=$(basename "$target")
  5.     file_path=$(dirname "$target")
  6.     file_extn="${full_name##*.}"
  7.     file_name="${full_name%.*}"
  8.     backup_file="${file_path}/${file_name}.${timestamp}.${file_extn}"
  9.  
  10.     if [[ ! -f $target ]]; then
  11.         echo "It doesn't look like ${target} is a regular file"
  12.         exit 1
  13.     fi
  14.  
  15.     timestamp=$(date +%m.%d.%y.%s)
  16.  
  17.     echo -n "Backing up '${target}' to '${backup_file}'... "
  18.  
  19.     cp "${target}" "${backup_file}" &>/dev/null
  20.  
  21.     if [[ $? -gt 0 ]]; then
  22.         echo "Error"
  23.     else
  24.         echo "Success"
  25.     fi
  26. }
Advertisement
Add Comment
Please, Sign In to add comment