From fd4f8d22fe682932cf3744b90ed9f328b4444703 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sun, 22 Sep 2013 09:54:32 -0700 Subject: [PATCH] Fixing bugs with trying to delete /bin or create folders in root --- rotate.sh | 57 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/rotate.sh b/rotate.sh index 99d2fc2..39d4046 100644 --- a/rotate.sh +++ b/rotate.sh @@ -1,6 +1,7 @@ #!/bin/bash -# Author: Marcello Torchio +# Authors: Marcello Torchio, Will Bradley +# https://github.com/zyphlar/backup-rotation-script # Set the directory name that you wish to backup DIRTOBACKUP=$1 @@ -8,28 +9,38 @@ DIRTOBACKUP=$1 # Set up the backup destination directory BACKUPDIRDEST=$2 -# Set up the number of days for backup rotation +# Set up the number of days for backup rotation (archives older than these # of days ago will be deleted) NUMDAYS=7; -# Start logging -echo "["$(date)"] - Started Backup" -# Check if the destination dir exists. If not create it -if [ ! -d "$BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/" ]; then - # Creates directory for today's backup - mkdir $BACKUPDIR/"$(date +"%m-%d-%Y")" +# Check for input / help / usage +if [ -z "$2" ] + then + echo "Usage: ./rotate.sh source_folder destination_folder" +else + + # Start logging + echo "["$(date)"] - Started Backup" + # Check if the destination dir exists. If not create it + if [ ! -d "$BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/" ]; then + # Creates directory for today's backup + mkdir $BACKUPDIRDEST/"$(date +"%m-%d-%Y")" + fi + + # Echo the log line. + echo "["$(date)"] - Backup directory $DIRTOBACKUP in $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/" + # Execute the backup command. This backup consist in a recursive copy of the source dir into destination dir + cp -R $DIRTOBACKUP $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/ + + TOTMB=$(du $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/ -hs) + echo "["$(date)"] - Finished Backup - $TOTMB Copied" + + # Search for folders older than NUMDAYS days and remove them. This makes possible rotation of backups + if [ "$BACKUPDIRDEST" != "" ]; then + for i in `find $BACKUPDIRDEST/ -maxdepth 1 -type d -mtime +$NUMDAYS -print`; + do + echo -e "["$(date)"] - Found backup folder from $(stat -c %y $i)! Deleting directory $i"; + #\rm -rf $i; + done + fi + fi - -# Echo the log line. -echo "["$(date)"] - Backup directory $DIRTOBACKUP in $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/" -# Execute the backup command. This backup consist in a recursive copy of the source dir into destination dir -cp -R $DIRTOBACKUP $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/ - -TOTMB=$(du $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/ -hs) -echo "["$(date)"] - Finished Backup - $TOTMB Copied" - -# Search for folders older than NUMDAYS days and remove them. This makes possible rotation of backups -for i in `find $BACKUPDIRDEST/ -maxdepth 1 -type d -mtime +$NUMDAYS -print`; -do - echo -e "["$(date)"] - Found outdated backup folder! Deleting directory $i"; - \rm -rf $i; -done