Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #######################################
- DEBUG='y'
- COPY='n'
- # directory with cpanel prefixed backup- files are
- BACKUPDIR='/path/to/cpanel/backupdirectory'
- #######################################
- convert() {
- filename=$1
- # break filename down into 3 parts to manipulate the timestamp into correct format
- # in parta variable
- checkmonth=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print $1}')
- if [[ ${#checkmonth} < 2 ]]; then
- parta=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print "_temp_"$3"0"$1$2}')
- else
- parta=$(echo $filename | awk -F '_' '{print $1}' | sed -e 's|backup-||' | awk -F '.' '{print "_temp_"$3$1$2}')
- fi
- partb=$(echo $filename | awk -F '_' '{print $2}')
- partc=$(echo $filename | awk -F '_' '{print $3}')
- if [[ "$DEBUG" = [Yy] ]]; then
- # debug mode just outputs the 3 divided parts to make sure it
- # is correct output
- echo "-------------------------------"
- echo "debug mode output: "
- echo "$parta"
- echo "$partb"
- echo "$partc"
- echo "-------------------------------"
- echo
- fi
- # piece together the parts required for the new filename
- newfilename="${parta}${partc}"
- echo "renaming $1 to $newfilename ..."
- if [[ "$DEBUG" = [Yy] ]]; then
- # in debug mode you can either output via echo just the new filename
- # and old file name when COPY='n' set or if COPY='y' set you can test
- # copy rename of file to newfilename
- # otherwise if debug mode is disabled DEBUG='n', it does live rename
- # of file name with prefix = backup- in directory you define at BACKUPDIR
- # variable above
- echo "-------------------------------"
- if [[ "$COPY" = [Yy] ]]; then
- echo "debug mode output (copy instead of rename): "
- echo "\cp -fa "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}""
- \cp -fa "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}"
- else
- echo "debug mode output (echo only): "
- echo "original file: ${BACKUPDIR}/${1}"
- echo "renamed file: ${BACKUPDIR}/${newfilename}"
- fi
- else
- echo "mv "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}""
- mv -f "${BACKUPDIR}/${1}" "${BACKUPDIR}/${newfilename}"
- fi
- }
- list_dirfiles() {
- for f in $(find "$BACKUPDIR" -type f -name 'backup-*' -printf "%f\n"); do
- echo "found $f"
- convert $f
- done
- echo
- ls -lah $BACKUPDIR
- }
- list_dirfiles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement