#1  
Old 5th July 2012, 20:50
Diggy Diggy is offline
Junior Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default [SOLVED] Bash script help

Hi, all.

I'm in the midst of creating a bash script to archive certain directories to a NAS device. I'm not a great script writer, and would appreciate your help. Here's what I have, so far:

#!/bin/bash
FILENAME="backup"`date -d "last month" +%Y%m`".txt"
touch $FILENAME
touch --date "2012-06-01" /tmp/start
touch --date "2012-06-30" /tmp/end
find /data/nightly_data_backup -type d -newer /tmp/start -not -newer /tmp/end -print | sort > $FILENAME
mount -t nfs netdisk219:/mnt//vol001/volume02/Archive/Database /backup
tar -czvf /backup/2012_04.tar.gz --files-from $FILENAME

How can I use variables to substitute in the dates in the commands:

touch --date "2012-06-01" /tmp/start
touch --date "2012-06-30" /tmp/end

The dates should be the begin and end dates of the preceding month from the date I run the script, and:

tar -czvf /backup/2012_04.tar.gz --files-from $FILENAME

The name of the tar file should also be the preceding month, in the format shown above.

Many thanks.

Diggy

Last edited by Diggy; 6th July 2012 at 20:22.
Reply With Quote
Sponsored Links
  #2  
Old 6th July 2012, 13:30
TiTex TiTex is offline
Senior Member
 
Join Date: Aug 2011
Location: Cluj-Napoca,Romania
Posts: 125
Thanks: 0
Thanked 18 Times in 17 Posts
Send a message via Skype™ to TiTex
Default

I'm trying to learn bash scripting , so will this work for you ?

Code:
#!/bin/bash

##Current day in numeric format i.e 31
day=$(date +%d)

##Current month in numeric format i.e 12
month=$(date +%m)

##Current year in 4 digit format i.e 2012
year=$(date +%Y)

##Last month in numeric format
lmonth=$(date -d "last month" +%m)

##Number of days in previous month
lday=`cal $lmonth $year | egrep -v [a-z] | wc -w`

FILENAME=backup-$(date -d "last month" +%Y-%m).txt
touch $FILENAME
touch --date "$year-$lmonth-01" /tmp/start
touch --date "$year-$lmonth-$lday" /tmp/end

mount -t nfs netdisk219:/mnt//vol001/volume02/Archive/Database /backup
find /data/nightly_data_backup -type d -newer /tmp/start -not -newer /tmp/end -print | sort > $FILENAME
tar -czvf /backup/$year"_"$lmonth".tar.gz" --files-from $FILENAME

Last edited by TiTex; 6th July 2012 at 13:35.
Reply With Quote
  #3  
Old 6th July 2012, 14:25
Diggy Diggy is offline
Junior Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

TITex,

Thanks for your response. I can test what you wrote, but here's what I came up with, and it works:

#!/bin/bash
#first day of previous month
FIRST_DAY=`date -d "-1 month -$(($(date +"%d")-1)) days" +"%b%e"`
#first day of this month
LAST_DAY=`date -d "-0 month -$(($(date +"%d")-1)) days" +"%b%e"`
#name file to be used by tar
FILENAME="backup"`date --date="last month" +%Y%m`".txt"
#date part of tar file name
FILEDATE=`date --date="last month" +%Y_%m`
#create file to be used by tar
touch $FILENAME
#create start and end dates of backup directories to be tarred
touch --date "$FIRST_DAY" /tmp/start
touch --date "$LAST_DAY" /tmp/end
#identify the directories to be tarred and add them to the file to be used by tar
find /data/nightly_data_backup -type d -newer /tmp/start -not -newer /tmp/end -print | sort > $FILENAME
#start portmap service
service portmap start
#open connection to NAS
mount -t nfs netdisk219:/mnt//vol001/volume02/Archive/Database /backup
#back up the database directories to NAS
tar -czf /backup/$FILEDATE".tar.gz" --files-from $FILENAME
#close connection to NAS
umount /backup
#stop portmap service
service portmap stop
#clean up
rm -f /tmp/start /tmp/end
rm -f /root/$FILENAME

The script runs on a CentOS 6.2 system. More elegant solutions might be available but, as I mentioned, it works. I'll test it again next month and, if it still works as expected, I'll create a monthly cron job to run it.

Hope it helps with your learning.

Diggy

Last edited by Diggy; 6th July 2012 at 14:42.
Reply With Quote
  #4  
Old 6th July 2012, 20:10
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,146
Thanks: 4
Thanked 55 Times in 51 Posts
Default

I'm not quite following what you try to backup when....

However I made a few adjustements to your script. Quote whatever you can and for variables you can use ${var} which makes it simpler in quotes to seperate it from other text.

Code:
#!/bin/bash

#first day of previous month
FIRST_DAY=`date -d "-1 month -$(($(date +"%d")-1)) days" +"%b%e"`

#first day of this month
LAST_DAY=`date -d "-0 month -$(($(date +"%d")-1)) days" +"%b%e"`

#name file to be used by tar
FILENAME="backup"`date --date="last month" +%Y%m`".txt"

#date part of tar file name
FILEDATE=`date --date="last month" +%Y_%m`

#create file to be used by tar
touch "${FILENAME}"

#create start and end dates of backup directories to be tarred
touch --date "${FIRST_DAY}" "/tmp/start"
touch --date "${LAST_DAY}" "/tmp/end"

#identify the directories to be tarred and add them to the file to be used by tar
find "/data/nightly_data_backup" -type d -newer "/tmp/start" -not -newer "/tmp/end" -print | sort > "${FILENAME}"

#start portmap service
service portmap start

#open connection to NAS
mount -t nfs "netdisk219:/mnt//vol001/volume02/Archive/Database" "/backup"

#back up the database directories to NAS
tar -czf "/backup/${FILEDATE}.tar.gz" --files-from "${FILENAME}"

#close connection to NAS
umount "/backup"

#stop portmap service
service portmap stop

#clean up
rm -f "/tmp/start" "/tmp/end"
rm -f "/root/${FILENAME}"
__________________
"Common sense is not as common as commonly believed" by sjau

Auto-Install Script for ISPConfig and Horde on a Vanilla Debian Stable

Need more Repos for Ubuntu? Repository Generator
Need more Repos for Debian? Debian Repository Generator
Reply With Quote
  #5  
Old 6th July 2012, 20:23
Diggy Diggy is offline
Junior Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the tip!

Diggy
Reply With Quote
  #6  
Old 6th July 2012, 20:26
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,146
Thanks: 4
Thanked 55 Times in 51 Posts
Default

Still, I don't fully comprehend what you try to backup and when. Could you explain?
__________________
"Common sense is not as common as commonly believed" by sjau

Auto-Install Script for ISPConfig and Horde on a Vanilla Debian Stable

Need more Repos for Ubuntu? Repository Generator
Need more Repos for Debian? Debian Repository Generator
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot login to phpmyadmin penkku HOWTO-Related Questions 1 24th February 2011 09:26
i am having some problem ? cobro Installation/Configuration 20 30th April 2008 16:26
Cannot log onto pop3 server Debian Etch Perfect Server docean Installation/Configuration 2 19th March 2008 00:23
https protocol stops page load tjd General 7 18th November 2007 18:06


All times are GMT +2. The time now is 01:26.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.