BASH   44

Rotate backup shell script to remove directories

Guest on 21st April 2022 01:11:12 AM

  1. #!/bin/bash
  2. # Program: Rotate backup shell script to remove directories (old backup files)
  3. # http://www.cyberciti.biz/tips/ssh-rotate-backup-shell-script.html
  4. # Author: nixCraft < vivek @ nixCraft DOT com >
  5. if [ "$#" == "0" ];then
  6.   echo "$0 upper-limit path {command}"
  7.   exit 1
  8. fi
  9. ### SSH Server setup ###
  10. SSH_USER="vivek"
  11. SSH_SERVER="nas.nixcraft.in"
  12. START=7
  13. DIR_FORMAT="%d-%m-%Y" # DD-MM-YYYY format
  14. #DIR_FORMAT="%m-%d-%Y" #MM-DD-YYYY format
  15. ## do not edit below ##
  16. LIMIT=$( expr $START + $1 )
  17.  
  18. ## default CMD ##
  19. CMD="ls"
  20. SSH_PATH="."
  21.  
  22. [ "$3" != "" ] && CMD="$3" || :
  23. [ "$2" != "" ] && SSH_PATH="$2" || :
  24.  
  25. DAYS=$(for d in $(seq $START $LIMIT);do date --date="$d days ago" +"${DIR_FORMAT}"; done)
  26. for d in $DAYS
  27. do
  28.   ssh ${SSH_USER}@${SSH_SERVER} ${CMD} ${SSH_PATH}/$d
  29. done

Raw Paste


Login or Register to edit or fork this paste. It's free.