Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Having trouble with my first shell script...very simple

  1. #1

    Default Having trouble with my first shell script...very simple

    I'm writing my first shell script ever. It's supposed take my Work folder at /home/user/Documents/Work, copy it over to /home/user/Documents/Backup, then tar it into a tar.gz file.

    My script does make a compressed tar file for me, but when I extract it, it has a folder inside a folder inside a folder. So I have to navigate through the folders home-->user-->Documents-->Backup-->Work just to see my backed-up files. Is there a way I can just not preserve the whole directory tree and just have the files all in one spot once I extract the tar file?

    Here's my script:

    #!/bin/bash
    cp -r /home/user/Documents/Work /home/user/Documents/Backup
    OF=/home/user/Documents/Backup/backup.tar.gz
    tar -czf $OF /home/user/Documents/Backup/Work/
    rm -r /home/user/Documents/Backup/Work/

    I'm going to put this script in /etc/cron.hourly so that it makes hourly backups of my Work folder. It won't fill up my hard drive because it removes the old files and overwrites the backup tar file.

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Having trouble with my first shell script...very simple

    The problem is this line:

    Code:
    tar -czf $OF /home/user/Documents/Backup/Work/
    Although this will contain the directory you want, it's an absolute path and so will contain all the intervening directories. You can either do this:

    Code:
    cd /home/user/Documents/Backup
    tar -czf $OF Work/
    (but remember that you are now in that directory for the following commands)

    or

    Code:
    tar -czf $OF -C /home/user/Documents/Backup Work/
    which does the chdir inside the tar.

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,775

    Default Re: Having trouble with my first shell script...very simple

    Can you explain me why
    Code:
    #!/bin/bash
    cd /home/user/Documents/
    tar cfz /home/user/Documents/Backup/backup.tar.gz Work
    would not do the trick?
    Henk van Velden

  4. #4

    Default Re: Having trouble with my first shell script...very simple

    I don't know if that would work or not. I'm still scratching the surface of shell scripting. I used your first idea, ken_yap, and now my script looks like this:

    Code:
    #!/bin/bash
    current_date="`date '+%a'`"
    cp -r /home/user/Documents /home/user/Backup
    OF=/home/user/Backup/$current_date.backup.tar
    cd /home/user/Backup
    tar -cf $OF Documents/
    rm -r /home/user/Backup/Documents
    It backs up the entire Documents folder to my Backup folder. It adds the day of the week to the beginning of the backup.tar file, so that I end up with backup files such as Mon.backup.tar. I put this script in /etc/cron.daily. That way, it should keep 7 days' worth of backups. Every day, it overwrites the last week's backup, so it won't fill up my hard drive, and I'll always have a week's worth of backups.

    I encountered a problem, though. I can't untar the file. It's 15 GB in size, and when I try to untar it, it says "an error occurred while trying to open the archive"

  5. #5

    Default Re: Having trouble with my first shell script...very simple

    Oh, ok. Apparently, tar only works with files that are 2 GB or smaller. I will have to find some other way to back up an entire 15 GB directory.

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,775

    Default Re: Having trouble with my first shell script...very simple

    Well, I hoped that by looking in what I provided you you what ask yourselg why you make a tar from a copy of thedata. Why not directly from the data?
    The intermediate Backup file is useless imho.

    And you better post exactly (by copy/past) what you do so we can see the tar statement and the output of the error message as it is. You have less to explain and we can decide for ourselves what is important in what you do.
    Henk van Velden

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,775

    Default Re: Having trouble with my first shell script...very simple

    Sorry, missed your last post by 3 minutes.

    When you are right about the 2GB that is a pity.

    A bit strange as tar is in fact a Tape ARchiver, and even the old openreels could have bigger files then 2GB.

    Does piping tar (without the z of course) throug gzip help?
    Henk van Velden

  8. #8
    Join Date
    Jun 2008
    Location
    Friesland
    Posts
    207

    Default Re: Having trouble with my first shell script...very simple

    if you are set on writing you own script instead of a free linux tool, which is very fun to do and makes one learn a lot you could choose to split your files to 2gb portions like so:

    Code:
    tar -cvzf - /path/to/backup | split -b2000m - backup.gz
    of course you would have to change the stuff to match your setup and "cd" into /data/to/ to forgo the absolute paths.

    stefan
    "The day Microsoft makes something that doesn't suck is the day they make a vacuum cleaner"
    OS: Fedora9, RHEL5, CENTOS5, Arch, OpenSuSe11
    Hardware: Dell Precision M65

  9. #9

    Default Re: Having trouble with my first shell script...very simple

    I changed my script to include your recommendation:

    #!/bin/bash
    current_date="`date '+%a'`"
    cd /home/user/Backup
    tar -cvzf - /home/user/Documents/ | split -b2000m - backup.gz


    Now all I need to do is figure out how to extract all the 2GB files it created.

  10. #10
    ab@novell.com NNTP User

    Default Re: Having trouble with my first shell script...very simple

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Where did you find that? I've made bigger TAR files than just 2 GB on
    several occasions and never had a problem with it. The biggest
    limitation is usually the filesystem (fat32 if it's anything small like
    this).

    Good luck.





    BNG22908 wrote:
    | Oh, ok. Apparently, tar only works with files that are 2 GB or smaller.
    | I will have to find some other way to back up an entire 15 GB
    | directory.
    |
    |
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFIYCRM3s42bA80+9kRAnJgAJ4+FiwJCmr+f7SlL78+GlO971UtAQCdFBbV
    1PSnRSKZB4Q/NGJ7epWL6NY=
    =8/PA
    -----END PGP SIGNATURE-----

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •