ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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."
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"
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 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.