LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2007, 05:03 PM   #1
keef12345
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
Post Creating a back up shell script


hi guys,

i need a backup script that has some options in it for my family to use and i need some help with the coding.
The script needs to do the following for us:

1.The utility will ask the user which directory to backup
2.The utility will ask for the fullpathname of the backup file to create.
Also I need the utility to ask any user if they would require another backup? if no the user can enter a n=no. If no comment the backup utility will loop back and repeat steps 1 and 2

I need help writing the code for this utility any help will be greatly appreciated
 
Old 11-03-2007, 05:13 PM   #2
JBull
Member
 
Registered: Jul 2005
Location: Dallas, TX, USA
Distribution: Debian Etch
Posts: 37

Rep: Reputation: 15
script

Python script?

I might be able to help if Python is OK and you have it installed.

I'm not very good with bash scripts.
 
Old 11-03-2007, 05:31 PM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-03-2007, 06:54 PM   #4
JBull
Member
 
Registered: Jul 2005
Location: Dallas, TX, USA
Distribution: Debian Etch
Posts: 37

Rep: Reputation: 15
Here's a start. There are several things that will need to change such as what name you'd like to give the backup file and where to place it. Let me know if you are interested in finishing this. It takes theee input directory and makes a tar.gz compressed archive.

Code:
#! /usr/bin/env python
import sys,os

archivename='archive.tar'
directory = raw_input("Enter the full path of the directory to back up: ")
cmd = 'tar -cf '+archivename+' '+directory+'/*'

ask = raw_input("Directory "+directory+" will be compressed to an archive.  Continue? (y or n)")
check=os.path.exists(directory)
if (check& (ask=='y')):	
	status = os.system(cmd)
	cmd = 'gzip '+archivename
	status = os.system(cmd)
	cmd = 'gzip -l '+archivename+'.gz'
	status = os.system(cmd)
if (check == 0):
	print "Directory "+directory+" does not exist.  Not archived.  Please check directory name."
 
Old 11-03-2007, 09:15 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by keef12345 View Post
The script needs to do the following for us:

1.The utility will ask the user which directory to backup
2.The utility will ask for the fullpathname of the backup file to create.
Also I need the utility to ask any user if they would require another backup? if no the user can enter a n=no. If no comment the backup utility will loop back and repeat steps 1 and 2
Here's another python script, this one uses the python "tarfile" module instead of external commands. (I try to avoid using external programs with python as much as possible)
Code:
#! /usr/bin/env python
import os
import tarfile

while True:
        #step 1: ask user which dir to backup, checks if exists
        dir = raw_input("Enter the full path of the directory to back up: ")
        if not os.path.exists(dir):
                print 'Directory "%s" does not exist. ' \
                        'Please check directory name.' % dir
                continue

        #step 2 asks for full path of backup file, checks if exists
        backup_file = raw_input("Enter the full path of the archive file: ")
        #asks if user wants to overwrite hit enter to overwrite
        while os.path.exists(backup_file):
                new_name = raw_input('Archive "%s" exists, '
                        'choose another name [blank to overwrite]: '
                        % backup_file)
                if not new_name.strip():
                        break
                backup_file = new_name

        #writes compressed file change "w:gz" to "w:bz2" for bzip2
        tar = tarfile.open(backup_file, "w:gz")
        tar.add(dir)
        tar.close()
        print '"%s" created' % backup_file

        #step 3: asks if user wants to make more
        another = raw_input("Would you like to make another backup[y/n]? ")
        #any answer that's not 'n' or 'no' is assumed to be yes
        if another.lower() in ('n','no'):
                break
I tried to document it as much as possible, if you need help understanding any of it just ask. Also, I wasn't sure if you wanted to compress the files or not, or which compression options to use. Change this line to what you'd like"
Code:
tar = tarfile.open(backup_file, "w:gz")
"w:gz" : tar.gz file
"w:bz2" : tar.bz2 file
"w" : uncompressed tar file

Last edited by angrybanana; 11-03-2007 at 09:29 PM.
 
Old 11-03-2007, 09:29 PM   #6
JBull
Member
 
Registered: Jul 2005
Location: Dallas, TX, USA
Distribution: Debian Etch
Posts: 37

Rep: Reputation: 15
Thats pretty good Mr Banana. I didn't know Python had a tar module. Your programming style is more concise also. I'm beginner/intermediate with Python. Interesting to see how someone with more experience approaches it.

May I also suggest that the OP does this as a cron job. Put the script in your crontab and run it daily or weekly. That saves the hassle of having to do it daily and saves your a$$ if you get lazy and forget to backup.



Thanks,
JB
 
Old 11-03-2007, 09:36 PM   #7
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Thanks JBull, python is my favorite language by far, always fun to program in it. This board is very good for learning, answering questions and reading other peoples answers is the best way to learn in my opinion.

PS: I wasn't sure if it had a tar module until I checked Python Library Reference
 
Old 11-03-2007, 10:29 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
here's an awk script...
Code:
awk 'BEGIN{
     while (1){
       close(cmdtestdir)
       printf "Enter a directory to backup: "
       getline dirbak < "-"
       cmdtestdir = "test -d "dirbak
       r= system(cmdtestdir) 
       if (r) { 
          print dirbak " does not exists"; 
          continue
       }
       else {
          while(1){
            close(cmdtestf)
            printf "What is the full path name of backup file?: "
            getline fullpath < "-"
            cmdtestf = "test -f "fullpath
            f =  system(cmdtestf)
            if (f != 1) { 
               print fullpath " exists"; 
               continue
            }
            else {
               print "Backing up dir " dirbak
               sub(".gz",systime()".gz",fullpath)
               cmdtar = "tar zcvf "fullpath"-"systime()" "dirbak
               n=system(cmdtar)
               break
            }
          }
     
       }
       printf "Backup again? (Nn)o to quit"
       getline choice < "-"
       if (choice ~ /^[Nn]$/) break
      }  #outermost while
      
}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a back up shell script keef12345 Linux - General 1 11-03-2007 05:24 PM
Help with creating a shell script windisch Programming 66 10-07-2005 06:26 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
Back-up Shell Script apoc013 Linux - Newbie 7 01-23-2004 01:40 PM
Help creating a directory back up shell script WarriorWarren Linux - General 6 04-06-2003 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration