backing up file everytime script run and saving other backups


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers backing up file everytime script run and saving other backups
# 1  
Old 01-28-2007
backing up file everytime script run and saving other backups

how would i go about creatng a backup of a file everytime a script is run so that the next time it is run the old backup gets pushed back and the most recent backup is 1st.
(new)backup.1
(old)backup.2
that kind of idea??
# 2  
Old 01-28-2007
Code:
if [ $i -le $c ]
        then
                echo "Creating Backups..."

                if [ -f $f.1 ]
                then
                        cp -v $f.$i $f.$j
                        rm $f.$i
                        cp -bv usermon $f.$i
                        sleep $s
                        ((i=i+1))
                else
                        cp -bv usermon $f.$i
                        sleep $s
                        ((i=i+1))
                fi
        fi

this is what i have so far...but obviously it only works for 2 backups..i need to be able to have an endless number
# 3  
Old 01-29-2007
Quote:
Originally Posted by knc9233
if [ $i -le $c ]
then
echo "Creating Backups..."

if [ -f $f.1 ]
then
cp -v $f.$i $f.$j
rm $f.$i
cp -bv usermon $f.$i
sleep $s
((i=i+1))
else
cp -bv usermon $f.$i
sleep $s
((i=i+1))
fi
fi
From where values of variables i, c, j are comming, please post a complete copy of your script.
# 4  
Old 01-29-2007
Something like this might help:
Code:
$ cat saver
#! /usr/bin/ksh

max=10
i=$max
while ((i>0)) ; do
        ((i=i-1))
        ((i2=i+1))
        echo "[[ -f somelog.$i ]] && cp somelog.$i somelog.$i2"
done
echo "cp somelog somelog.0"
exit 0
$ ./saver
[[ -f somelog.9 ]] && cp somelog.9 somelog.10
[[ -f somelog.8 ]] && cp somelog.8 somelog.9
[[ -f somelog.7 ]] && cp somelog.7 somelog.8
[[ -f somelog.6 ]] && cp somelog.6 somelog.7
[[ -f somelog.5 ]] && cp somelog.5 somelog.6
[[ -f somelog.4 ]] && cp somelog.4 somelog.5
[[ -f somelog.3 ]] && cp somelog.3 somelog.4
[[ -f somelog.2 ]] && cp somelog.2 somelog.3
[[ -f somelog.1 ]] && cp somelog.1 somelog.2
[[ -f somelog.0 ]] && cp somelog.0 somelog.1
cp somelog somelog.0
$

# 5  
Old 01-29-2007
You could just put a date stamp on the end of the file in YMDHMS order.

mv filename to filename.`date +%Y%m%d%H%M%S`
When you do ls -l it should sort in date order.

To drop the older files by date, use the find command

find /directory -filename* -mtime -30 -exec rm {} \;

If you want to keep n number of copies:

cd directory
VAL=`ls -l -1 -t filename* | wc -l`
VAL=`expr ${VAL} - 738`
FILES=`ls -1 -t filename* | tail -${VAL}`

for file in $FILES
do
echo "Deleting file $file...."
rm -f $file
done
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. UNIX for Dummies Questions & Answers

Expect Script for backing up files on different servers

Hello, I am trying to create an expect script that backups up data from multiple servers with the same username, but with different passwords for each server. For example, I have two servers with the "myuser" account with different passwords for that account for each server. I went ahead and... (2 Replies)
Discussion started by: richw92201
2 Replies

3. Shell Programming and Scripting

Saving variables to a .sh file and last successful run parm.

I had a script that uses a fair number of variables. My manager just let me know that these variables have to be set in one .sh file. How do I do that? I'm use bash shell. Also At the end of my script I need to save the last successful run. so that when I run it next time, I just this parm to... (3 Replies)
Discussion started by: MJCreations
3 Replies

4. UNIX for Dummies Questions & Answers

shell script : log to txt and insert new line everytime

Hi, I have this script, while do ps ax|grep 5060 > log.txt echo " " sleep 1 done } I want to actually put a new line everytime the loop is executed in log.txt , but I do not know how to "embed" the echo " " inside the log.txt. (so to say... (1 Reply)
Discussion started by: peuceul
1 Replies

5. Shell Programming and Scripting

executing a shell script everytime the terminal is opened

the problem statement is Write a shell script, which gets executed the moment the user opens a terminal. It should display the message "Hello your_name, welcome to 172.16.4.120 server” How to get around this? (2 Replies)
Discussion started by: arindamlive
2 Replies

6. UNIX for Dummies Questions & Answers

Script run everytime a new terminal window is opened

I created a script called title #!/bin/sh echo "^0;$*^G" It will change the terminal window titlebar to what ever I type after the script (title BIG would change titlebar to BIG instead of terminal) Is there a way to make it run so it will work on every terminal window that gets opened.... (1 Reply)
Discussion started by: amason0508
1 Replies

7. Shell Programming and Scripting

How to make a script to run everytime a new file is copied into a directory??

Hi folks I have a unix script script1 in a directory folder1 and also I have few input log files in this directory. My input log files will be copied into this directory folder1 from the portable thumb drive. Now what I want is I need to run this script1 whenever any new file is copied... (2 Replies)
Discussion started by: ks_reddy
2 Replies

8. UNIX for Advanced & Expert Users

Backing up Log file

Hi, Iam trying to create a back up file for a log file which is being continously updated. Is there a way to backup without loosing the log, while the back up is also up to date? If i try to replace the log with an empty file, I loose the log for some seconds. If I try to create a... (2 Replies)
Discussion started by: baanprog
2 Replies

9. Shell Programming and Scripting

Need to run source .bashrc everytime

hi all, I've included some variables in my .bashrc file. But everytime i'm logging in i need to source my .bashrc file to make effect of my variables. i'm having an account on a highly secured production environment. I've the following in my ~/.bashrc file alias cls=clear How to make... (2 Replies)
Discussion started by: kalyanraj
2 Replies

10. UNIX for Dummies Questions & Answers

Run process to resynchronize mirrors during database backups?? What does it mean???

does anyone have any idea of what this means and if you do, please elaborate more on what you know about this and the importance it carries along. by the way, we dealing with Linux and Solaris 8 systems here (3 Replies)
Discussion started by: TRUEST
3 Replies
Login or Register to Ask a Question