I'm having an issue with a problem
A problem with this backup script is that if you backup the same file twice, you may get a warning message because you're overwriting an existing file. You could suppress the warning message, but a better solution is to save a series of backups distinguished by numbers. The first time you type backup foo.c, it copies it to foo.c.1. Then you make some changes to foo.c and type backup foo.c again; the script notices that foo.c.1 is already there, so it copies foo.c to foo.c.2 instead. The third time, you get foo.c.3, and so on
Here is the code I have so far:
#!/bin/csh -f
file ($*)
set num=1
while (-e $file.$num)
@ num = $num +1
do
cp ${file} ${file}.$num
if anyone could fill in the gaps or tell me where I'm going wrong here.. it would be greatly appreciated
Here is a ksh script. This script will need a file name to backup. It will backup only if the file has changed (content wise) compared to the last backed-up version.
[/tmp]$ cat backup.ksh
#! /bin/ksh
[ -z "$1" ] && echo "No file to backup" && exit 1 || FILE="$1"
VER=$(ls -l $FILE.* 2>/dev/null | wc -l)
VER=$(($VER))
cmp -s "$FILE" "$FILE.$VER"
if [ $? -gt 0 ] ; then
VER=$(($VER + 1))
cp "$FILE" "$FILE.$VER"
echo "Backed up $FILE to $FILE.$VER"
else
echo "No backup taken."
fi ;
[/tmp]$ ls -l foo.c*
-rw-r--r-- 1 -------- g900 19 Nov 5 02:52 foo.c
[/tmp]$ ./backup.ksh
No file to backup
[/tmp]$ ./backup.ksh foo.c
Backed up foo.c to foo.c.1
[/tmp]$ ./backup.ksh foo.c
No backup taken.
[/tmp]$ ls -l foo.c*
-rw-r--r-- 1 -------- g900 19 Nov 5 02:52 foo.c
-rw-r--r-- 1 -------- g900 19 Nov 5 02:57 foo.c.1