Register
It is currently Thu Oct 30, 2014 9:55 am

Backing up work


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Jan 13, 2011 9:20 pm   
User avatar

Joined: Sat Dec 11, 2010 5:27 pm
Posts: 15
Location: NSW, Australia
Hi ppl

I have written this to backup selected directories from my /home directory to a separate partition mounted at /Archive. The backup script runs every day at 12 midday and 8 pm. There is a log of each run kept up to a maximum of 30 log files.

I then burn the /Archive parition to DVD once a week


Code:
#!/bin/bash

#############################################################
#
#     Script:    backall.sh
#
#    Version:    1.0
#
#   Function:    To backup selected directories from /home
#                    to an /Archive partition and keep logs
#
#     Author:    Alan Brown
#
#    Created:   14 January 2011
#
#   Modified:   14 January 2011
#
#     System:   Ubuntu 10.10 - Linux 2.6.35-24-generic i686
#                   GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
#
#############################################################

# Go to /home directory
cd ~

# The /Archive partition has a /Logs directory
# Set the log file name to this format - "YYYYmmdd-hhmm.log"
fn=/Archive/Logs/`date +%Y%m%d-%H%M`.log

# Directory for sample and testscripts
rsync -aru --progress -s /home/user/bash /Archive >> $fn

# Working scripts
rsync -aru --progress -s /home/user/bin /Archive >> $fn

# C program development directory
rsync -aru --progress -s /home/user/C /Archive >> $fn

# C++ development directory
rsync -aru --progress -s /home/user/C++ /Archive >> $fn

# General documents, accounts, letters
rsync -aru --progress -s /home/user/Documents /Archive >> $fn

# Linux notes and information
rsync -aru --progress -s /home/user/Linux /Archive >> $fn

# Photos
rsync -aru --progress -s /home/user/Pictures /Archive >> $fn

# Email and Calendar
rsync -aru --progress -s /home/user/.thunderbird /Archive >> $fn

# Music playlists
rsync -aru --progress -s /home/user/Music/Playlists /Archive/Music >> $fn

# CD and DVD indexes
rsync -aru --progress -s /home/user/CDIndexes /Archive >> $fn

# Meditation information
rsync -aru --progress -s /home/user/Meditation /Archive >> $fn

#############################################################
#
# Remove earlier log files so we hold only a specified maximum
#

# Go to the Logs directory
cd /Archive/Logs

# Maximum number of logs to keep
max=30

# Get the number of files in the directory
x=`ls -u1 | wc -l`

# do while the number of logs is greater than the maximum
while [[ $x > $max ]]
do
    # Get the name of the oldest file and remove it
    y=`ls -u1 | tail -1`
    rm $y
    # Get the new count of files in the directory
    x=`ls -u1 | wc -l`
done   

#############################################################
#       End of Script
#############################################################


I hope this is of use

Any comments would be welcome.

Alan


Top
 Profile  
 PostPosted: Tue Jan 25, 2011 11:30 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,
Code:
#!/bin/bash

shopt -s nullglob #because if /Archive/Logs is empty, "${LogFiles[@]}" would expand to litterally '/Archive/Logs/*', instead of nothing.

home="/home/user" #maybe $HOME can be used?
logs="/Archive/Logs"
fn="$logs/$(date +%Y%m%d-%H%M).log"
maxLogs=30
DirList=( bash C C++ Documents Linux Pictures .thunderbird Music/Playlists CDIndexes Meditation )

for dir in "${DirList[@]}"
do echo "rsync -aru --progress -s \"$home/$dir\" \"Archive/${dir%/*}\" >> \"$fn\""
#if it's allright, change to: do rsync -aru --progress -s "$home/$dir" "Archive/${dir%/*}" >> "$fn"
done

LogFiles=( "$logs"/* )
for (( x=0; x<${#LogFiles[@]}-maxLogs; x++))
do toDeleteFiles+=( "${LogFiles[x]}" )
done
echo "rm \"${toDeleteFiles[@]}\""
#if it's all right, change to: rm "${toDeleteFiles[@]}"
it's not fully tested


Top
 Profile  
 PostPosted: Fri Jan 28, 2011 5:07 pm   
User avatar

Joined: Sat Dec 11, 2010 5:27 pm
Posts: 15
Location: NSW, Australia
Thank you for your reply and suggestions. As you can see I am still a beginner in bash scripting.

I will work through your suggestions and rewrite my script.

Thanks

Alan


Top
 Profile  
 PostPosted: Wed Feb 09, 2011 6:54 pm   
User avatar

Joined: Sat Dec 11, 2010 5:27 pm
Posts: 15
Location: NSW, Australia
Watael

This is my adaptation of your script and now works OK.

Code:

     1   #!/bin/bash
       
     2   # Thanks to Watael at bashscripts.org: Sandbox
       
     3   # set shell option
     4   shopt -s nullglob
     5   # because if /Archive/Logs is empty, "${LogFiles[@]}" would expand to
     6   #      literally '/Archive/Logs/*', instead of nothing.
       
     7   #set Logs directory
     8   logs="/Archive/Logs"
       
     9   # set log file name to date
    10   fn="$logs/$(date +%Y%m%d-%H%M).log"
       
    11   # set maximum number of log files to keep
    12   maxLogs=30
       
    13   # list of directories to backup
    14   # Dirlist is an array of directory names
    15   DirList=( bash bin C C++ CDIndexes Documents Linux .thunderbird UsefulFiles )
       
    16   # scan through the list of directory names
    17   for dir in "${DirList[@]}"
    18   do
    19       echo -e "\n\n***** From Directory: $HOME/$dir" >> $fn
    20       rsync -auv --progress -s "$HOME/$dir" "/Archive" >> "$fn"
    21   done
       
    22   # LogFiles is an array of log file names
    23   LogFiles=( "$logs"/* )
       
    24   # scan through the list of log file names
    25   for (( x=0; x<${#LogFiles[@]}-maxLogs; x++))
    26   do
    27       # toDeleteFiles is an array of log files to be deleted
    28       toDeleteFiles+=( "${LogFiles[x]}" )
    29   done
       
    30   # delete all files listed in the array toDeleteFiles if the list is non-zero
    31   if [ ${#toDeleteFiles[@]} -gt 0 ] ; then
    32       rm "${toDeleteFiles[@]}"
    33   fi
       
    34   echo -e "\n\n***** End of Backup" >> $fn


I have added several comments to remind myself of what is going on. I am not used to the fact that a variable can be used as an array without specifically defining it as such as in C, C++, BASIC and so on.

In your code "Archive/${dir%/*}" produced a new directory within the destination directory (eg /Archive/bin/bin) instead of just copying the files to the existing directory (/Archive/bin). So I changed it as in the code above in line 20.

Also if the number of logs was less than the maximum then toDeleteFiles became "" which the command rm disliked. So I added the test in line 31.

Thanks for your help. I have a lot to learn!

Alan


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP