Script help - beginner

Discussion in 'General Linux' started by StJude1982, Nov 26, 2013.

  1. StJude1982

    StJude1982 New Member

    Messages:
    5
    Likes Received:
    7
    Trophy Points:
    3
    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?

    careprost and DevynCJohnson like this.
  2. arochester

    arochester Active Member

    Messages:
    622
    Likes Received:
    193
    Trophy Points:
    43
  3. StJude1982

    StJude1982 New Member

    Messages:
    5
    Likes Received:
    7
    Trophy Points:
    3
    careprost likes this.
  4. StJude1982

    StJude1982 New Member

    Messages:
    5
    Likes Received:
    7
    Trophy Points:
    3
    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:

    #!/bin/bash
    #script to backup essential program files

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

    #Source & Destination pathnames
    DBASE="/home/directory"
    BKDIR="/mnt/network/backups"

    #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??
    careprost likes this.
  5. GlennJohnson

    GlennJohnson New Member

    Messages:
    6
    Likes Received:
    9
    Trophy Points:
    3
    Assuming your backup folder / path is actually mounted ...

    Try adding a trailing / to your BKDIR variable.
    [
    PHP:
    BKDIR="/mnt/network/backups/"
    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]
    careprost likes this.
  6. GrumpyOldMan

    GrumpyOldMan Member

    Messages:
    75
    Likes Received:
    27
    Trophy Points:
    18
    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/ user@host: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.
    careprost likes this.
  7. DevynCJohnson

    DevynCJohnson Super Moderator Staff Member Staff Writer

    Messages:
    980
    Likes Received:
    859
    Trophy Points:
    93
    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
    careprost likes this.
  8. StJude1982

    StJude1982 New Member

    Messages:
    5
    Likes Received:
    7
    Trophy Points:
    3
    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
    careprost and DevynCJohnson like this.
  9. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    910
    Likes Received:
    290
    Trophy Points:
    63
    @DevynCJohnson yeah, surprised CRON was not mentioned. Then again depending on the distro, using SYSTEMD is easy also. ;)
    DevynCJohnson likes this.
  10. StJude1982

    StJude1982 New Member

    Messages:
    5
    Likes Received:
    7
    Trophy Points:
    3
    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/cron.d
    /etc/cron.daily
    /etc/cron.hourly
    /etc/cron.monthly
    /etc/cron.weekly
    /etc/init.d => rc.d/init.d
    /etc/crontab
    /etc/cron.deny

    /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?
    DevynCJohnson likes this.
  11. ryanc

    ryanc New Member

    Messages:
    11
    Likes Received:
    6
    Trophy Points:
    3
    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

Share This Page