2

This is for a simple dropbox backup program that runs automatically once a week on tuesday. (Note this is on MacOS rather than Linux)

The code for the first month is below.

Basically, the code checks to see if the current year exists. If it doesn't it creates and enters it, if it does it enters it.

It then creates all the months (if they already exist it does nothing)

Finally, it checks to see if weeks 1-4 exist and if one doesn't then it creates it and makes the backup there.

However, only in an ideal world does each month have 4 weeks in it (for instance Jan 2017 has 5 Tuesdays) so how could I change the code so it automatically knows how many times to back up within that month?

    clear
#Sets year variable
date +"%Y"
# Store Year variable
cyear=$(date +'%Y')
echo "Year = $cyear"
clear

#changes the directory to the back up directory and makes sure the year is correct
cd /Users/DBUP/BUP
if [ ! -d "$cyear" ]; 
    then mkdir $cyear;
fi
clear
echo "Year $cyear Created or Found"
sleep 2s
clear

#Creates all of the months within a year
cd /Users/DBUP/BUP/$cyear
mkdir 01;
mkdir 02;
mkdir 03;
mkdir 04;
mkdir 05;
mkdir 06;
mkdir 07;
mkdir 08;
mkdir 09;
mkdir 10; 
mkdir 11;
mkdir 12;
clear
echo "Months Created or Found"
sleep 2s
clear   

#Creates the week files in each month
#Month 01 Start
cd /Users/DBUP/BUP/$cyear/01
if [ ! -d "wk1" ];
    then mkdir wk1
    cp -a /Users/user/Dropbox/. /Users/DBUP/BUP/$cyear/01/wk1
    clear
    echo "Week 1, month 1 Back Up Completed";
else if [ ! -d "wk2" ]; 
    then mkdir wk2; 
    cp -a /Users/user/Dropbox/. /Users/DBUP/BUP/$cyear/01/wk2;
    clear;
    echo "Week 2, month 1 Back Up Completed";
else if [ ! -d "wk3" ];
    then mkdir wk3; 
    cp -a /Users/user/Dropbox/. /Users/DBUP/BUP/$cyear/01/wk3;
    clear;
    echo "Week 3, month 1 Back Up Completed";
else    if [ ! -d "wk4" ];
    then mkdir wk4;
    cp -a /Users/user/Dropbox/. /Users/DBUP/BUP/$cyear/01/wk4;
    clear;
    echo "Week 4, month 1 Back Up Completed";
#Month 01 End 

#Month 01
fi
fi
fi
fi
#End
abba2566
  • 21
  • 1
  • One option would obviously be to use 52 weeks rather than organising it by month. But that would not be as easy to navigate when looking for a certain backup. But if it isn't possible, that is obviously a possibility. – abba2566 Jan 25 '17 at 11:53
  • Are you looking to know if a month has 4 or 5 weeks? is it? – Inian Jan 25 '17 at 11:55
  • You need to discover the `elif` keyword. The entire script could probably be pruned down to approximately 1/3. – tripleee Jan 25 '17 at 12:09

0 Answers0