Help: Backfile if backup does not exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help: Backfile if backup does not exist
# 1  
Old 07-25-2012
Help: Backfile if backup does not exist

I am trying to work on a script that will first check to see if a backup of a file exists, and if it does it will prompt the user to ask if he/she wants it replace. Of course, if the user says no then the file should be skipped.

Here is the code I have written. Does it look as if it is working correctly?

Code:
#! /bin/bash
echo "Beginning backup of files..."
# Check to see if files exist
if [ -f /home/user/Documents/backup.tar.gz ]; then
    read -p "Backup file already exists. Replace (y/n)?"
    [ "$REPLY" != "y" ] || tar -czf /home/user/Documents/backup.tar.gz /home/user/Documents/files
else
    tar -czf /home/user/Documents/backup.tar.gz /home/user/Documents/files
fi
echo "Backup complete."

# 2  
Old 07-25-2012
How about seting up a nice function that ask a y/n question and sets the return code, something like this:

Code:
#!/bin/bash
function yorn
{
   yn=""
   while [ "${yn}:" = ":" ]
   do
       printf "%s (y/n)? " "$1"
       read yn
       case $yn in
           [yY]|[Yy][Ee][Ss]) true;;
           [nN]|[nN][oO]) false;;
           *) yn="";;
       esac
  done
}
 
if [ ! -f /home/user/Documents/backup.tar.gz ] || yorn "Backup file already exists. Replace"
then
   tar -czf /home/user/Documents/backup.tar.gz /home/user/Documents/files
fi
echo "Backup complete."

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

2. Shell Programming and Scripting

rsync backup mode(--backup) Are there any options to remove backup folders on successful deployment?

Hi Everyone, we are running rsync with --backup mode, Are there any rsync options to remove backup folders on successful deployment? Thanks in adv. (0 Replies)
Discussion started by: MVEERA
0 Replies

3. UNIX for Advanced & Expert Users

backup a file and keep every version of the backup

I am trying to backup my .bash_history and I want to keep every version of the backup. I am thinking to put one of these in my crontab. 0 0 * * 0,3 cat .bash_history > boo 0 0 * * 0,3 cp .bash_history boo I would like the backups to be called boo1, boo2, boo3, etc. I would like to keep... (7 Replies)
Discussion started by: cokedude
7 Replies

4. Shell Programming and Scripting

if pid exist ?

Hi I need help to whit a script that start a program if a nother program is started first . I thought something like this . if then start program 2 Thanks (4 Replies)
Discussion started by: pelle
4 Replies

5. SCO

Backup to SCSI Tape Backup aborts

I am trying to make a full backup of my system using the cpio command. The Tape Unit is a SCSI DDS. The process started fine but after about 30 minutes, it just stopped and showed the following message: 1755 Signal 31 - Core dumped Any idea of what is causing this and how to fix it? ... (4 Replies)
Discussion started by: zionpc
4 Replies

6. UNIX for Dummies Questions & Answers

Check backup file size on backup tape

Hi, I performed backup on tape and I want to append more files to my previous backup on the same backup tape. But before I do that I need to know the backup file size of the first backup I performed so that I know the available size on the backup tape. Can someone help me what command I will use... (0 Replies)
Discussion started by: ayhanne
0 Replies

7. UNIX for Dummies Questions & Answers

su: user x does not exist

somehow my user names and groups on one of our machines are changed to numbers 700, 701, etc. thus, i can only ssh into this machine as root. is there a way to migrate the user names and groups to this machine? thanks! (3 Replies)
Discussion started by: user23
3 Replies

8. UNIX for Advanced & Expert Users

Showing Device Does Not Exist While Taking Backup

Friends, while taking backup on /dev/rmt/0cn it is showing device does not exists. I have checked /dev/rmt 0cn is present there with link file created in /devices/pci@8,700000/scsi@5/st@5,0:cn I have checked cd /devices/pci@8,700000/scsi@5 but st@5,0:cn is not there. But I found st@3,0:cn. ... (3 Replies)
Discussion started by: ailnilanjan
3 Replies

9. UNIX for Dummies Questions & Answers

does it exist in linux?

Is there a jumpstart equivalent tool in the linux environment? (1 Reply)
Discussion started by: pbonilla
1 Replies
Login or Register to Ask a Question