[problem] making a backup files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [problem] making a backup files
# 1  
Old 03-13-2009
[problem] making a backup files

Hi guys,

I'm a little stuck on this problem, I've tried googling it and some trial and error but can't seem to get it working.

Basically I need the script to:

1) create a directory called "mybackups", if it doesn't exist
2) go through all the .sh files in the current directory, and copy them into the
"mybackups" directory.
3) add the date to the end of each filename.

This is what I've got so far:

Code:
#!/bin/bash

if [test -e mybackups]
then
	mkdir mybackups
	echo "Created backup directory"
else
	echo "Backup directory found"
fi

for file in (man ls)
do
	if[${f: -3} == '.sh']
	then
		cp $file $file|date "+%Y%m%d"
	fi
done

I think the problem is with the cp $file $file|date "+%Y%m%d" line.
But any help would be awesome, I'm a complete newby to this :s

Last edited by chazij; 03-13-2009 at 05:20 AM..
# 2  
Old 03-13-2009
Quote:
Originally Posted by chazij
Hi guys,

I'm a little stuck on this problem, I've tried googling it and some trial and error but can't seem to get it working.

Basically I need the script to:

1) create a directory called "mybackups", if it doesn't exist
2) go through all the .sh files in the current directory, and copy them into the
"mybackups" directory.
3) add the date to the end of each filename.

This is what I've got so far:

Code:
#!/bin/bash

if [test -e mybackups]
then
    mkdir mybackups
    echo "Created backup directory"
else
    echo "Backup directory found"
fi

for file in (man ls)
do
    if[${f: -3} == '.sh']
    then
        cp $file $file|date "+%Y%m%d"
    fi
done

I think the problem is with the cp $file $file|date "+%Y%m%d" line.
But any help would be awesome, I'm a complete newby to this :s

Hello there,

Here is a KornShell script, that does the job (if you change the first line with
#!/bin/bash it also works but will give a warning message).

Code:
#!/bin/ksh

backup_directory="mybackups"

if [[ -a $backup_directory ]]
then
    if [[ ! -d $backup_directory ]]
    then
        print -u2 "error: $backup_directory is not a directory"
        exit 1
    fi
else
    mkdir $backup_directory
    print "The directory $backup_directory was created successfully."
fi

for iterator in *
do
    if [[ -f $iterator ]]
    then
        current_filename=$iterator
        if [[ $current_filename == *.sh ]]
        then
            cp $iterator $backup_directory
            mv "$backup_directory/$current_filename" "$backup_directory/$current_filename-$(date +%m-%d-%y)"
        fi
    fi
done

I hope this can help,

Regards,
Smilie
# 3  
Old 03-13-2009
Just a question: did you try your code yet?
Code:
#!/bin/bash

#if [test -e mybackups]
if [[ ! -w mybackups ]] # Extra test not required, space needed to seperate the [ from the content, check if the directory is writeable
then
	mkdir mybackups
	echo "Created backup directory"
else
	echo "Backup directory found"
fi

#for file in (man ls)
for file in $( ls *.sh ) # Saves you the test below as the loop will only see the .sh files
do
	#if[${f: -3} == '.sh']
	#then
	#cp $file $file|date "+%Y%m%d"
	cp $file mybackups/${file}$( date "+%Y%m%d" )
	#fi
done

HTH
# 4  
Old 03-13-2009
cp filename filename`date '+%Y%m%d'`
# 5  
Old 03-13-2009
Thanks loads!

works like a charm Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

UNIX Network Programming making files problem

I have started reading the book Unix Network Programming, Volume 1: The Sockets Networking API. I downloaded all the source code and the readme says I must make the files. zuro@zuro:~/book/unpv12e$ cd lib zuro@zuro:~/book/unpv12e/lib$ make gcc -g -O2 -D_REENTRANT -Wall -c -o... (5 Replies)
Discussion started by: xuro
5 Replies

2. Solaris

Making HDD backup in sun Solaris system

Hello to all, I have Fujitsu Celzius M470 workstation which has Sun solaris system installed and want to make a complete backup of the hard drive. The existing HDD is SATA II, 500 GB. I don't have much experience working with SUN solaris systems (not at all) but have some experience with... (5 Replies)
Discussion started by: Mick
5 Replies

3. Shell Programming and Scripting

Problem in making a list with awk

Hi bodies,I am doing a list from a .txt file with awk commands but something is wrong. The .txt file looks like: 32782 28 18 32783 02 18 32784 01 18 32785 29 18 32786 25 23 32787 25 18 32788 00 18 32789 25 26 32790 02 23 32791 29 26 ... (2 Replies)
Discussion started by: Behrouzx77
2 Replies

4. UNIX for Dummies Questions & Answers

Making backup to .doc file .... sh

im tring to make backup to the files end with "doc" extension but the code give me error ... what is the problem!!!! #! /bin/sh if then for name in `ls *.DOC` do kk= $(printf "%s_temp" $name) cp $name $kk done else echo "error" (9 Replies)
Discussion started by: maga6610
9 Replies

5. Solaris

Making backup with ufsdump to remote mounted filesystems

Hi Unix members. I am trying to make a backup to remote mounted filesystems : fileraseing:/vol/oracliente_revap01 10G 1.3G 8.7G 13% /orarev_ap01 fileraseing:/vol/bobje 10G 5.9G 4.1G 60% /bobje But I am receiving the follow message when use the ufsdump command: :$PWD>ufsdump 0fu... (8 Replies)
Discussion started by: aggadtech08
8 Replies

6. Shell Programming and Scripting

Problem in making itration

Hi, I have a file which keeps on appending the data continuously, i that i am looking for a particular pattern, if i dont find that pattern i want to wait for 30 seconds to check it again. can we use it like this ----------------------------- until do cat $line |... (1 Reply)
Discussion started by: Prateek007
1 Replies

7. Shell Programming and Scripting

problem in making sftp script

Dear all I am bit new to shell scripting . I am implemented autossh between two sun solaris machines , so that when I use sftp it will not ask for the password. Now I need to make shell script in which I have to transfer files from one server to another server automatically through root... (8 Replies)
Discussion started by: girish.batra
8 Replies

8. Shell Programming and Scripting

Problem in making shell script

Dear all Dear Brother I am bit new to programming or shell scripting. I have given one shell script which is regarding combining all the 240 or less files in a particular folderwhich is related to one hour of the day. There will be 24 these kind of folders related to a day . It means there... (4 Replies)
Discussion started by: girish.batra
4 Replies

9. Programming

DDD making problem

Hi Everybody, I am trying to make the ddd-3.3.9 debugger. I installed all dependencies. this is what i get: # make Making all in themes make: Entering directory `/space/atoulan/ddd-3.3.9/themes' make: Nothing to be done for `all'. make: Leaving directory... (0 Replies)
Discussion started by: azazel11998
0 Replies
Login or Register to Ask a Question