Daily ,Weekly and Monthly backup from Linux to Windows

Scenario :-

Setup a backup script to take daily, weekly and monthly backups to a remote windows server.  I wrote this bash script to meet the clients requirement and its worked  perfectly.  With some minor changes you can use the same script to setup daily ,weekly and monthly backup’s to the local linux server. I have setup separate scripts for daily, weekly and monthly backup’s. So that if somebody searching for the same scenario then they can understand the logic very easily.

Overview :-

1)  Created a folder ” backup ” in the remote windows server backup drive.

2) Created a user as follows



3)  Grant necessary  privileges  for this user to the backup directory as follows .


4 )   Mount the windows backup drive using /etc/fstab file follows ( with windows username and password )

//winserver/backup  /WIN_BACKUP  cifs  username=backup,password=pass 0 0

5)  Decided to run and setup  separate Daily , weekly and monthly backup scripts as follows

 

Crontab entries :-

#### Daily  backup at 03:01 am on  Monday to Satuarday
01 03  * * 1-6  /bin/bash /usr/local/scripts/daily_backup.sh > /dev/null 2>&1

##### Weekly  backukp – every  Sunday at 05:01 am
01 05  * * 0    /bin/bash /usr/local/scripts/weekly_backup.sh > /dev/null 2>&1

##### Monthly  backup – First day of every month at 06:01 am
01 06 1 * *  /bin/bash /usr/local/scripts/monthly_backup.sh > /dev/null 2>&1

 

Backup Script Files :-

 

1)   /usr/local/scripts/daily_backup.sh

#!/bin/bash
PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH
## To find the day output will be like "Mon,Tue,Wed etc "
path=`date | awk '{print $1}'`
# Already created the folders Mon,Tue,Wed,..Sat inside /WIN_BACKUP/daily
# Backup scripts directory
rsync -avzub --copy-links /usr/local/scripts/   /WIN_BACKUP/daily/$path/scripts
# Backup website files 
rsync -avzub --copy-links  --exclude 'logs'  --exclude 'logs'  --exclude '*.tar'  --exclude '*.gz'  --exclude '*.zip' --exclude '*.sql'  /usr/local/www/   /WIN_BACKUP/daily/$path/UsrLocalWww

 

2)  /usr/local/scripts/weekly_backup.sh

#!/bin/bash PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH
cd /WIN_BACKUP/website_weekly/
mkdir sun-`date +%Y%m%d`
cd sun-`date +%Y%m%d`
mkdir -p scripts
mkdir -p UsrLocalWww
# Backup scripts directory
rsync -avzub --copy-links /usr/local/scripts/    /WIN_BACKUP/website_weekly/sun-`date +%Y%m%d`/scripts
# Backup website files 
rsync -avzub --copy-links  --exclude 'logs'  --exclude 'logs'  --exclude '*.tar'  --exclude '*.gz'  --exclude '*.zip' --exclude '*.sql'  /usr/local/www/  /WIN_BACKUP/website_weekly/sun-`date +%Y%m%d`/UsrLocalWww

 

3) /usr/local/scripts/monthly_backup.sh

#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH
## To find the current month , out put will be " Jan, Feb, Mar etc " 
path=`date | awk '{print $2}'`
# Create the corresponding direcotries with current month
mkdir -p /WIN_BACKUP/website_monthly/$path/scripts
mkdir -p /WIN_BACKUP/website_monthly/$path/UsrLocalWww
# Backup scripts directory
rsync -Cavz /usr/local/scripts/   /WIN_BACKUP/website_monthly/$path/scripts
# Backup all websites
rsync -Cavz --exclude 'log'  --exclude 'logs'  --exclude '*.tar'  --exclude '*.gz'  --exclude '*.zip' --exclude '*.sql'  /usr/local/www/   /WIN_BACKUP/website_monthly/$path/UsrLocalWww

I took almost 1 day to complete this setup and now its running fine 🙂 . Hope that this documentation will  definitely help somebody, who is looking for the same setup.

For mysql daily,weekly and monthly backup setup check : MySql Backup Script