Bash script help

Discussion in 'Programming/Scripts' started by Diggy, Jul 5, 2012.

  1. Diggy

    Diggy New Member

    [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: Jul 6, 2012
  2. TiTex

    TiTex Member

    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: Jul 6, 2012
  3. Diggy

    Diggy New Member

    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: Jul 6, 2012
  4. sjau

    sjau Local Meanie Moderator

    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}"
    
     
  5. Diggy

    Diggy New Member

    Thank you for the tip!

    Diggy
     
  6. sjau

    sjau Local Meanie Moderator

    Still, I don't fully comprehend what you try to backup and when. Could you explain?
     
  7. Diggy

    Diggy New Member

    Each night, a main database on a db server is backed up on that server before nightly transactions are run, and after they're run. Each before and after database, along with the db transaction log, is placed in its own sub-directory named for the date of the backup, as in "20120516" and "20120516_after". After a month's worth of these backups are created, I archive them on a NAS device. After three months, I delete the oldest backups on the db server. Rather than do this manually, I was looking to automate at least most of the process. Hence the script.
     

Share This Page