Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Can someone help me with my backup script?

  1. #1
    Join Date
    Oct 2009
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Can someone help me with my backup script?

    Hi, I've been working on a backup script that I'd run every night before I went to sleep. What the script should do is backup my files, clean out some unnecessary stuff, and then shut itself down. I think I've got it right but it would be so much appreciated if you guys could look over it for me, suggestions are awesome too! I'm messing with "rm -r" so I'm being ultra-paranoid I've got it right!


    Code:
    #!/bin/bash
    
    # Backs up files, cleans out the junk, then turns off the computer
    
    ##### Backup #####
    rm -r /home/dillan/bak/* &&
    cp -r /media/files/* /home/dillan/bak/ &&
    
    ##### Clean #####
    apt-get autoclean &&
    apt-get clean &&
    
    ##### Shutdown #####
    shutdown -h now
    NOTE: I keep all my files on an external hard drive, and I'm using the computer's hard drive as backup. And of course this script would be run with "sudo".

  2. #2
    Join Date
    Dec 2007
    Location
    /home
    Beans
    483

    Smile Re: Can someone help me with my backup script?

    maybe

    Code:
    #!/bin/bash
    
    ##### Backup #####
    rm -r /home/dillan/bak &
    cp -r /media/files/ /home/dillan/bak/ &
    
    ##### Clean #####
    sudo apt-get autoclean &
    sudo apt-get clean &
    
    ##### Shutdown #####
    sudo shutdown -h now

    shutdown cannot be run by you, unless you mingle with /etc/sudoers


    jbrown96 covered what you could to about /etc/sudoers here.

    just make sure, if you are going to edit the sudoers file, to do it with visudo.
    Last edited by angry_johnnie; December 27th, 2009 at 04:33 AM.
    Don't eff with The Cult...

  3. #3
    Join Date
    Jan 2009
    Beans
    Hidden!

    Re: Can someone help me with my backup script?

    instead of removing and copying all the files daily, you should synchronize the 2 folder.

    you could give rsync a try.

    try
    Code:
    rsync -ruv --delete /media/files /home/dillan/bak 

    note: try rsync with the --dry-run switch first, to confirm that everything works as expected.

    this will copy newer files to the destination, and delete any files not found on the source folder. rest seems fine.

    also, you could make a cronjob for the script, instead of manually doing it. though, then the shutdown line should be removed from the script.

  4. #4
    Join Date
    Oct 2009
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Can someone help me with my backup script?

    Thanks for the reply!
    I can see I did in fact mess up a few things

    So it's only one "&" instead of "&&"?
    And "shutdown -h now" needs to be behind sudo?
    Hmm...would it work the same if I ran the script in sudo?
    The name of the script is "backup", so if I did

    Code:
    sudo backup
    Then typed in my password would it still shutdown at the end?

    I'm trying to make the whole thing automated,
    So when I'm ready to retire for the night I just type "sudo backup"
    Then I can just walk away and let it finish

  5. #5
    Join Date
    Dec 2007
    Location
    /home
    Beans
    483

    Re: Can someone help me with my backup script?

    Quote Originally Posted by dillandriskell View Post
    Thanks for the reply!
    would it work the same if I ran the script in sudo?
    The name of the script is "backup", so if I did

    Code:
    sudo backup
    Then typed in my password would it still shutdown at the end?

    Yes, it would.
    Don't eff with The Cult...

  6. #6
    Join Date
    Oct 2009
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Can someone help me with my backup script?

    Quote Originally Posted by x33a View Post
    instead of removing and copying all the files daily, you should synchronize the 2 folder.

    you could give rsync a try.
    rsync sounds like a much better alternative!
    In fact the constant removing/copying was my workaround to synchronizing.
    Now so far my script now looks like this;

    Code:
    #!/bin/bash
    
    # Backs up files, cleans out the junk, then turns off the computer
    
    ##### Backup #####
    rsync -ruv --delete /media/files /home/dillan/bak &
    
    ##### Clean #####
    apt-get autoclean &
    apt-get clean &
    
    ##### Shutdown #####
    shutdown -h now
    And I've considered doing it through Cron, but I'm too bad at keeping a strict schedule to do that. I'm always staying up late to finish some things up or something like that.

  7. #7
    Join Date
    Oct 2008
    Beans
    3,509

    Re: Can someone help me with my backup script?

    For an easy to follow Gui based on rsync and allows to schedule have a look at
    luckyBackup

  8. #8
    Join Date
    Oct 2009
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Can someone help me with my backup script?

    Alright, I finally got everything in order and decided to test the script!
    But ahk! I was met with failure.

    I first tested rsync which x33a gave me, first by itself, both a --dry-run and a full run, absolute success!

    So then I tried running the script itself, and my computer proceeded to shutdown? What I'm guessing is happening is it's trying to run all the commands at once. I remember reading about this when I was studying BASH a while ago, I'm thinking I need to replace "&" with something else. I think the proper command is either ";" or ";;".

    What's happening, if I remember correctly, is "&" tells the computer to run the command before and after the symbol. Where the ";" or ";;" counterpart tells the computer to run the command following the symbol after the first command is run. Which is exactly what I want.

    Unfortunately my Google searches have been inconclusive on the subject at best. Does anyone by chance have any light to shed on the situation? I'll continue to try Googling it, but this is one of those things with very difficult search terms on a subject already lacking online documentation.

  9. #9
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can someone help me with my backup script?

    I must have done something differently when I wrote my script. I basically strung a bunch of commands together using "&&"

    Like this:

    Code:
    sleep 5
    #Create array directory and change owner and permissions
    echo "Creating /array and setting owner and permissions..." && sleep 2
    mkdir /array && chown charles:raid /array &&chown charles:raid /array/ && chmod 770 /array && echo "Completed." && sleep 5
    #Create DVD backup directory and change owner and permissions
    echo "Creating /dvdback and setting owner and permissions..." && sleep 2
    mkdir /dvdback && chown charles:charles /dvdback && chown charles:charles /dvdback && chmod 770 /dvdback && echo "Completed." && sleep 5
    #Create Doc backup directory and change owner and permissions
    echo "Creating /docback and setting owner and permissions..." && sleep 2
    mkdir /docback && chown charles:charles /docback && chown charles:charles /docback && chmod 770 /docback && echo "Completed." && sleep 5
    I didn't use "&" at all.

    I'd probably rewrite your script like this:

    Code:
    #!/bin/bash
    
    # Backs up files, cleans out the junk, then turns off the computer
    
    ##### Backup #####
    rsync -ruv --delete /media/files /home/dillan/bak
    
    ##### Clean #####
    apt-get autoclean && apt-get clean
    
    ##### Shutdown #####
    shutdown -h now
    Last edited by CharlesA; December 27th, 2009 at 07:25 AM.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  10. #10
    Join Date
    Oct 2009
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Can someone help me with my backup script?

    Quote Originally Posted by CharlesA View Post
    I must have done something differently when I wrote my script. I basically strung a bunch of commands together using "&&"
    That did the trick, thanks! Looks like I was overcompensating

    I tested it, and everything worked...theoretically.

    It transferred all the files fine, sure. But since the script has to be run as root, when I attempt to access the files under my normal user account later, all the files were locked. Only accessible by one with root privileges. That makes sense, the files were created by the root user.

    Easy enough, I thought. I'll just add sudo chown dillan /home/dillan/bak/* after the file transfer. So it would now looks like this;

    Code:
    #!/bin/bash
    
    # Backs up files, cleans out the junk, then turns off the computer
    
    ##### Backup #####
    rsync -ruv --delete /media/files /home/dillan/bak
    chown dillan /home/dillan/bak/*
    
    ##### Clean #####
    apt-get autoclean && apt-get clean
    
    ##### Shutdown #####
    shutdown -h now
    However, here's the problem; it only gives the top directories permissions back to me. Everything beneath it is still root! Certainly you can see how long it would take to change the permissions in every directory of over 25 GB of files. So now I'm trying to find a way around that.

    The way I see it there's two options to find a fix. Either there's some way to make chown change the permissions of every directory when I use "*", or there's a way to make rsync -ruv --delete /media/files /home/dillan/.bak (the file transfer/backup command) run as me and not root. Both of those would fix my problem. Unfortunately I can't run the whole thing as me, since shutdown -h now needs to be sudo. And if I specify in the script itself to run (sudo shutdown -h now) it would no longer be automated, as I'd have to manually put in the password after all the other steps were complete.

    I'm trying to find the answers on Google as well, but if anyone knows the way to achieve either of those workarounds it'd be very appreciated it you helped me out!

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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