Script help - beginner



I'm new to linux & looking to write a script to copy files from a directory across a network for back-up.
I would like to create a script that would do this on a regular basis, say once a week.
I've seen a few scripts online, however, I would like to learn and understand what I am doing. I know the path directories and most of the basic commands; how do I go about creating a robust script for this purpose?



I've tried looking at the thread linked above, as well as Googling various backup scripts. However, like I said before, I would like to understand what I am doing and feel I have went in a bit too deep. I've tried to simplify things a bit and created the following script with errors attached:

#script to backup essential program files

#Time stamp info
DATE='date +%d,+%m,+%Y'

#Source & Destination pathnames

#Mount network & archive files
mount $BKDIR

tar cfz "$BKDIR_$DATE.tgz" $DBASE

Error messages as follows:
mount: can't find /mnt/network/backups in /etc/fstab or /etc/mtab
tar: Removing leading '/' from member names

Apologises for any glaring errors. Like I say, I am new to this. I don't understand the mount problem as I've added the network to /etc/fstab and I am able to access the files??


Assuming your backup folder / path is actually mounted ...

Try adding a trailing / to your BKDIR variable.
With the variable the way you have it your backup destination expands to
/mnt/network/backupsDATE.tar.gz $DBASE so you're trying to write to a folder / path that doesn't exist.

Also, shouldn't the tar command be tar czf and not tar cfz ?[/PHP]


That's the hard way. Just rsync the files over the network. You'll need the destination set up with an ssh server (use openssh).

Then in your script:
rsync -auv path_to_source/ [email protected]:path_to_dest

This will do incremental backups - it won't move anything that hasn't changed, and will only move changed segments in large files that have changed. It will also handle compression/decompression on it's own.

Finally, the destination directory will have the files accessible without needing to extract them. If you want them in tar format, simply tar them into a directory like /tmp, then use rsync to move to destination, but that may take longer.

If there's some kind of stupid symbol between host and path_to_dest, ignore it, the brain-dead forum software's doing it.


To do this on a regular basis, get your original script and put it on the cron tables. Then, you do not need to modify your script. When programming, remember KISS - Keep It Simple Stupid. :D

I cannot believe no one recommended cron to @StJude1982 :eek:

When scheduling or making a regular event, use cron.

cron = schedule


Thanks for all the suggestions. Have to say keeping it simple is all I can do for now. I do think for the application I'm looking for cron will be the easiest to implement. Thanks again


I've tried to go down the route of using cron, and I thought that it was going to be easy enough, however I am enountering some problems.

When I try to set up my crontab I'm unable to enter anything into it. I get something like this:

>crontab -e
"/tmp/crontab.XXXXVDOISL"OL, OC

I've looked up various articles for help and seem to be getting nowhere. Here is a list of the folders I have - is there something I missing?

/etc/init.d => rc.d/init.d

/var/spool/cron - The folder contents can't be displayed - don't have permission. This is even after changine the permissions.

drwxrwx--- 2 root 4096 Dec 4 03:35 cron

Log out and then log in with Root

/var/spool/cron/ - The directory was empty. I've tried copying crontab into the directory, however, I'm still having problems.

Can anyone help?


When you run the command you are given the file to edit in VIM. I would start by looking up a tutorial on how to edit files using VIM

Staff online

Latest posts