Advertisement
eva2000

convert.sh

Dec 30th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.42 KB | None | 0 0
  1. #!/bin/bash
  2. #######################################
  3. DEBUG='y'
  4. COPY='n'
  5. # directory with cpanel prefixed backup- files are
  6. BACKUPDIR='/path/to/cpanel/backupdirectory'
  7. #######################################
  8. convert() {
  9.   filename=$1
  10.   # break filename down into 3 parts to manipulate the timestamp into correct format
  11.   # in parta variable
  12.   checkmonth=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print $1}')
  13.   if [[ ${#checkmonth} < 2 ]]; then
  14.     parta=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print "_temp_"$3"0"$1$2}')
  15.   else
  16.     parta=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print "_temp_"$3$1$2}')
  17.   fi
  18.   partb=$(echo $filename | awk -F '_' '{print $2}')
  19.   partc=$(echo $filename | awk -F '_' '{print $3}')
  20.   if [[ "$DEBUG" = [Yy] ]]; then
  21.     # debug mode just outputs the 3 divided parts to make sure it
  22.     # is correct output
  23.     echo "-------------------------------"
  24.     echo "debug mode output: "
  25.     echo "$parta"
  26.     echo "$partb"
  27.     echo "$partc"
  28.     echo "-------------------------------"
  29.     echo
  30.   fi
  31.   # piece together the parts required for the new filename
  32.   newfilename="${parta}${partc}"
  33.   echo "renaming $1 to $newfilename ..."
  34.   if [[ "$DEBUG" = [Yy] ]]; then
  35.     # in debug mode you can either output via echo just the new filename
  36.     # and old file name when COPY='n' set or if COPY='y' set you can test
  37.     # copy rename of file to newfilename
  38.     # otherwise if debug mode is disabled DEBUG='n', it does live rename
  39.     # of file name with prefix = backup- in directory you define at BACKUPDIR
  40.     # variable above
  41.     echo "-------------------------------"
  42.     if [[ "$COPY" = [Yy] ]]; then
  43.       echo "debug mode output (copy instead of rename): "
  44.       echo "\cp -fa "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}""
  45.       \cp -fa "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}"
  46.     else
  47.       echo "debug mode output (echo only): "
  48.       echo "original file: ${BACKUPDIR}/${1}"
  49.       echo "renamed file: ${BACKUPDIR}/${newfilename}"
  50.     fi
  51.   else
  52.     echo "mv "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}""
  53.     mv -f "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}"
  54.   fi
  55. }
  56.  
  57. list_dirfiles() {
  58.   for f in $(find "$BACKUPDIR" -type f -name 'backup-*' -printf "%f\n"); do  
  59.     echo "found $f"
  60.     convert $f
  61.   done
  62.   echo
  63.   ls -lah $BACKUPDIR
  64. }
  65. list_dirfiles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement