Register
It is currently Mon Sep 01, 2014 7:44 am

Back up script extended


All times are UTC - 6 hours


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 PostPosted: Mon Sep 04, 2006 6:38 am   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
in the last thread i made, you guys helped me make a simple back up script which worked great...for about 3 days (until i realised how much space i would need after say...a year) so i decided to revise the script. all the information is covered in the script(i think).

the problem is whenever i execute the script it says i have a syntax error, i don't know enough about bash to know where the error is, infact i'm not even sure i'm going about writing it logicly correct.

anyway have a read, give some (constructive) critisism if you can

Code:
#!/bin/bash

#Every monday of the month a full backup will be made
#and every other day a recursive back up will be made
#until the end of the month, then the backup cycle will
#start over (a new recursive file will be made,
#corrosponding with the current month

CurrentMMyy="$(date +%dd_%mm_%Y)";
CurrentDay="$(date +%a)";
CurrentMonth="$(date +%b)"; 
UsbDisk="/media/usbdisk/";
BackupLo="/home/mikus/Backup/$CurrentMMyy/";
if $CurrentDay == Mon; #i'm not sure if this is the correct way to check what day it is
then
mkdir $BackupLo
cp -R $UsbDisk $BackupLo
tar cvpzf "FULL_$CurentMMyy.tar"
rm -r $BackupLo #is there an easier way to remove the directory after it's been archived?

elif $CurrentDay != Mon;
mkdir $CurrentMonth
cp -R $UsbDisk $CurrentMonth
tar cvpzf "$CurrentMonth.tar" #i'm not sure if i should put quotes around this
rm -r $CurrentMonth
fi
exit 0


Last edited by mikus on Tue Sep 05, 2006 3:14 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Sep 04, 2006 7:32 am   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
mikus wrote:
Code:
if $CurrentDay == Mon; #i'm not sure if this is the correct way to check
elif $CurrentDay != Mon;


That should be

Code:
if [ "$CurrentDay" == "Mon" ]
then
     blah blah
elif [ "$CurrentDay" != "Mon" ]
then
   blah blah
fi


but why elif and not else?

Code:
if [ "$CurrentDay" == "Mon" ]
then
     blah blah
else
   blah blah
fi


Top
 Profile WWW ICQ  
 PostPosted: Mon Sep 04, 2006 7:50 am   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
Code:
#!/bin/bash

#Every monday of the month a full backup will be made
#and every other day a recursive back up will be made
#until the end of the month, then the backup cycle will
#start over (a new recursive file will be made,
#corrosponding with the current month

CurrentMMyy="$(date +%dd_%mm_%Y)";
CurrentDay="$(date +%a)";
CurrentMonth="$(date +%b)";
UsbDisk="/media/usbdisk/";
BackupLo="/home/mikus/Backup/$CurrentMMyy/";
#i'm not sure if this is the correct way to check what day it is
if [ $CurrentDay == "Mon" ];
then
tar cvpzf $BackupLo/FULL$CurrentMMyy.tar.gz $UsbDisk
else [ $CurrentDay != "Mon" ];
tar cvpzf  $BackupLo/FULL$CurrentMonth.tar.gz $UsbDisk
fi
exit 0


fixed up the errors you pointed out, and also i'm making tar work strait from my usbdisk so i don't have to make a new dir

EDIT: a friend just pointed out how and why the logic of this is bad, sould be able to fix it up though.. (i only need backups for every day..)
heres some pseudo for the script that i'll flesh out tomorow (it's midnight here in australia)

if mon

fullbackup

else

partialbackup


Top
 Profile  
 PostPosted: Mon Sep 04, 2006 8:27 am   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
mikus wrote:
BackupLo="/home/mikus/Backup/$CurrentMMyy/";

tar cvpzf $BackupLo/FULL$CurrentMMyy.tar.gz $UsbDisk

else [ $CurrentDay != "Mon" ];
tar cvpzf $BackupLo/FULL$CurrentMonth.tar.gz $UsbDisk



You should use curly braces around your variables when you use them inside strings of characters. If you don't bash will have a hard time picking them up.

Code:
BackupLo="/home/mikus/Backup/${CurrentMMyy}/"
tar cvpzf $BackupLo/FULL${CurrentMMyy}.tar.gz $UsbDisk
tar cvpzf  $BackupLo/FULL${CurrentMonth}.tar.gz $UsbDisk


Did you know tar has an update option? On your non-full days you probably want to use that.

Code:
tar upzf $BackupLo/INCR${CurrentMonth}.tar.gz $UsbDisk


Tar will then append changed files to the archive. Effectively giving you an incrimental backup.


Top
 Profile WWW ICQ  
 PostPosted: Mon Sep 04, 2006 3:36 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
thanks, this is going to be a breeze to write now (i hope)


Top
 Profile  
 PostPosted: Mon Sep 04, 2006 6:11 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
this script is wrong, as in it won't run. but am i on the right track?

Code:
#!/bin/bash
CurrentDay="$(date +%a)";
BackupLo="/home/mikus/Backup";
UsbDisk="/media/usbdisk/";
if [ $CurrentDay == "Mon" ]
then
tar cvpzf $BackupLo/FULL${CurrentDay}.tar.gz $UsbDisk
else
tar upzf $BackupLo/INCR${CurrentDay}.tar.gz $UsbDisk
exit 0


Top
 Profile  
 PostPosted: Mon Sep 04, 2006 9:11 pm   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
mikus wrote:
this script is wrong, as in it won't run. but am i on the right track?

Code:
tar upzf $BackupLo/INCR${CurrentDay}.tar.gz $UsbDisk
exit 0


All looks good, the only thing I can see that's wrong is you're missing a fi at the end of the if block. In bash you write if statements

Code:
if [test]
then
   do something
elif [ test ]
   do something
else
   do something
fi


Add a fi before your exit and you're good to go.


Top
 Profile WWW ICQ  
 PostPosted: Mon Sep 04, 2006 10:24 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
tar is having a bit of trouble

tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

this is really annoying, cause i changed it so it's not compressing it..
heres what i got from man tar:

Quote:
-c, --create
create a new archive
-v, --verbose
verbosely list files processed
-p, --same-permissions, --preserve-permissions
ignore umask when extracting files (the default for root)
-f, --file [HOSTNAME:]F
use archive file or device F (default "-", meaning stdin/stdout)
-u, --update
only append files that are newer than copy in archive


i can't see how this can possibly be wrong, given the above information, please enlighten me


Code:
#!/bin/bash
CurrentDay="$(date +%a)";
BackupLo="/home/mikus/Backup";
UsbDisk="/media/usbdisk/";
if [ $CurrentDay == "Tue" ]
then
#note that i removed compression
tar cvpf $BackupLo/FULL${CurrentDay}.tar $UsbDisk
else
tar upf $BackupLo/INCR${CurrentDay}.tar $UsbDisk
fi
exit 0


Top
 Profile  
 PostPosted: Tue Sep 05, 2006 2:46 am   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
Are you sure you didn't at any point create a compressed archive with that name? Run file against that archive and see what it says it is. If tar thinks it's compressed it probably is, you should delete it and then run the script again.

As tar can't update compressed archives you could change the else to:

Code:
else
    [ -f $BackupLo/INCR${CurrentDay}.tar.gz ] && gunzip $BackupLo/INCR${CurrentDay}.tar.gz
    tar upf $BackupLo/INCR${CurrentDay}.tar $UsbDisk
    gzip $BackupLo/INCR${CurrentDay}.tar
fi


This will zip and unzip the archive before and after updating.


Top
 Profile WWW ICQ  
 PostPosted: Tue Sep 05, 2006 3:14 am   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
worked like a charm, thanks. after i ammended your fix there was also an issue with if the folder existed which is an easy fix, so anyway heres the completed script:

Code:
#!/bin/bash

##############################
# the following will back up
# all the folders on your usb
# disk in a 7 day cycle.
# Mondays will make a full
# backup all other days will
# only increment.
# Credits:    
# TuxInvader: for writing the script
# crous: for helping with the original script 
# mikus: for writing up the draft
##############################
CurrentDay="$(date +%a)";
# Change this to where you want you files to be copied to
BackupLo="/home/${USER}/Backup";
#if this isn't the location of your usb stick set it acordingly
UsbDisk="/media/usbdisk/";
mkdir $BackupLo
if [ $CurrentDay == "Mon" ]
then
tar cvpf $BackupLo/FULL_${CurrentDay}.tar $UsbDisk
else
[ -f $BackupLo/INCR_${CurrentDay}.tar.gz ] && gunzip $BackupLo/INCR_${CurrentDay}.tar.gz
tar upf $BackupLo/INCR_${CurrentDay}.tar $UsbDisk
gzip $BackupLo/INCR_${CurrentDay}.tar
fi
exit 0


Last edited by mikus on Tue Sep 05, 2006 4:42 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Tue Sep 05, 2006 9:57 am   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
Mikus, you are too kind. You wrote it, I just helped you debug it.

And you seem to have left yourself out of the credits all together?

It needs fixing! :)


Top
 Profile WWW ICQ  
 PostPosted: Tue Sep 05, 2006 4:41 pm   

Joined: Sun Aug 27, 2006 7:27 pm
Posts: 10
thats allright, i did write the draft, so i'll take cred for that.

anyway i have an update coming soon for it :lol:


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 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:  
cron


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