LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Creating a back up shell script (https://www.linuxquestions.org/questions/programming-9/creating-a-back-up-shell-script-596935/)

keef12345 11-03-2007 05:03 PM

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

JBull 11-03-2007 05:13 PM

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.

reddazz 11-03-2007 05:31 PM

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.

JBull 11-03-2007 06:54 PM

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."


angrybanana 11-03-2007 09:15 PM

Quote:

Originally Posted by keef12345 (Post 2947230)
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

JBull 11-03-2007 09:29 PM

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

angrybanana 11-03-2007 09:36 PM

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 :)

ghostdog74 11-03-2007 10:29 PM

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
     
}'



All times are GMT -5. The time now is 11:17 PM.