Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Easy Way To Backup Your System!! (Bash Script)

  1. #1
    Join Date
    Mar 2010
    Beans
    40

    Question Easy Way To Backup Your System!! (Bash Script)

    WARNING: NO CODE IS FLAWLESS, AND THIS IS NO DIFFERENT.
    THINGS MIGHT GO WRONG WHEN USING THIS METHOD, SO I SUGGEST TAKING A HARD-COPY (ON CD/DVD) OF ANYTHING THAT YOU REALLY CAN'T AFFORD TO LOSE.
    THIS IS MEANT ONLY AS A STARTING POINT, AND YOU SHOULD LEARN A BETTER WAY AS YOU GET MORE INTO USING UBUNTU.
    THIS SCRIPT COMES WITH NO WARRANTY OR GUARANTEE OF ANYTHING. THOUGH I WILL HELP ANYONE WHO ASKS FOR IT NICELY IN ANY WAY I CAN.

    Hello Everyone.
    I understand how frustrating it can be to come from an OS like Windows, to something as excellent as Ubuntu.
    Your so used to Windows that it all seems very new and frightening.
    Well, Don't give up!!!
    It gets easier, and along with that it gets BETTER than Windows ever got.
    You'll never want to go back...kinda like...nahh i better not

    Anyway...
    In this article, I'm going to show you how you can backup your entire system.
    Not only that, but I'm going to show you how you can create a BASH script to do it for you, and how to add said script to the system PATH so that all you need to do is-

    1. Open A Terminal
    2. Type in " backup " (without the quotes)
    3. Hit Enter
    4. Giggle as Ubuntu does all the crazy stuff for you

    So...How can it be THAT easy you might ask?
    Well, First off lets give a little to bash scripts.
    Bash scripts are basically just bits of code that are run in bash (command-line).
    When a bash script is run, it runs the commands as if the user were typing them in his or her -self. Line-by-line.
    Bash scripts can be anything from backing up the system, to wiping it out
    But there are plenty of tutorials on the word wide web that will give plenty info regarding them.
    I'm here to show you a practical use for bash scripting, while showing you how to backup your system.
    I will also add in a restore script later on, in this thread. SO KEEP YOUR EYES OPEN on this one, cause I'll be adding updates and stuff to make her better (yes, my script is a she)
    Okay, so....

    What you need to do first is make the script.
    I have taken the liberty of writing the script(which I use for myself when I need to backup my system for whatever reason) and here she is-

    Code:
      #!/bin/bash
      # Print Pre-text Containing Script Version Info & Creator Info (please leave current credits intact and add a separate line if you modify the script & pass it on)
      clear
      echo "Backup Script"
      echo ""
      echo "V.1.3"
      echo ""
      echo "Created by Lonewaster@gmail.com"
      echo "VIVA LA LINUX!!!"
      echo ""
      read -p "Please Press ENTER To Continue..." waitForEnter
      # Alert The User & Wait 3 Clicks
      echo "BACKUP Initiating..."
      echo ""
      sleep 3
      # Ask The User For Their Current Username (For Backup File Placement & Permissions)
      read -p "What username are you currently logged into? (must be IDENTICAL to username you are currently using!!): " backupUser
      # Backup Entire System To the Specified User's homedir/backup.tgz (../../ included so script can be put in subdirectory)
      sudo tar cvpzf ../../home/$backupUser/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /
      # Upon Completion Of Backup, Alert The User
      echo ""
      echo "BACKUP Complete!"
      echo ""
      # Then Set backup.tgz File Privilidges So That The Specified USER Can Read, Write & Execute
      echo "Setting Backup File Privilidges..."
      echo ""
      # Set The File's Group As the Specified $backupUser
      chgrp $backupUser ../../backup.tgz
      # Set Read, Write & Execute Privilidges For The Owner (root)
      chmod u+rwx ../../backup.tgz
      # Set Read, Write & Execute Privilidges For The Specified Group ($backupUser)
      chmod g+rwx ../../backup.tgz
      # Alert The User, Wait 3 Clicks & Terminate The Script
      echo "TERMINATING..."
      echo ""
      sleep 3
      # End Of Backup.sh Script
    Now, I don't really need to explain much because the comments in the script (anything on a line after a # ) tells you what is happening as it is happening.
    But for anyone who wants a little bit more info, here are the BASH commands explained.

    #!/bin/bash - tells the computer that the following code is bash script. so that the computer knows how to deal with it.

    clear - clears the terminal window of all text so that it looks nice and clean and organised.

    echo - this function tells the computer to print something on the screen. in the terminal window.
    Code:
    echo "Hello World!"
    The above code would show "Hello World!" on the screen.

    read - this funtion prints some text and then takes some input and stores it in a variable.
    the first time our script uses it is just to wait for the user to press enter. There is probably a better way to do this, but I used this way for simplicity.

    Code:
    read -p "What is your name? " strName
    The above code will ask the user for their name, and then store it in the variable strName. Then, if you wanted the program (or script) to print the person's name, you would put this after the above code-
    Code:
    echo "Hello $strName"
    Code:
    read -p "What username are you currently logged into? (must be IDENTICAL to username you are currently using!!): " backupUser
    We use this bit to get the username that the user is currently logged into and stores it in a variable (backupUser) so that we can place the backup.tgz file in the correct directory and set the user's file permissions. Once again, there is probably a better way to do this (and I would like to invite anyone to give it forth) but I just went with this for simplicity.

    Code:
    sudo tar cvpzf ../../home/$backupUser/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /
    This is the good part. This tells the computer to basically copy the entire file system into a file in the given username's home folder called backup.tgz and compress it.
    The extra parts at the end of the command (--exclude=/media ... etc ...) tells the program we are using NOT to copy certain folders. This is because we don't need to backup these folders.
    For example, we don't need to backup /media because that is CDs,DVDs, etc.. That are in the computer.

    Code:
    chgrp $backupUser ../../backup.tgz
    This little snippet changes backup.tgz's group to the username that was inputted earlier, allowing us to run the following command to give that user Read, Write & Execute privilidges.

    Code:
    chmod g+rwx ../../backup.tgz
    This bit sets the file permissions to allow the file's group to Read, Write & Execute.
    The group has already been changed with our chgrp command (shown above this bit of code).
    I'm not sure that this bit is necessary but I chose to add it in anyway.
    If anyone of more experience in this field has any say for this part, then please- go ahead.
    At the very least, this gives newer users an introduction to useful bash commands...though they are all useful at some point.

    Code:
    chmod u+rwx ../../backup.tgz
    This bit sets the file permissions to allow root to Read, Write & Execute. This isn't really necessary...but neither is drinking whisky- but I still love to do it!!

    Finally, We need to edit environment variables so that we can just type "backup" in a terminal (no quotes) to run our script.
    Firstly, type the following in a terminal-
    Code:
    gedit /etc/environment
    This will open the text editor with your PATH environment variable.
    Now, my path looked like the following(and yours should be something similar)-
    Code:
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
    This contains a list of places that contain binary programs and scripts.
    For example, when you type in gedit file.txt ubuntu checks in these folders for the program called gedit and then (if it finds it) uses that program to process file.txt.

    Now, you need to add in the directory your script is in for this to work.
    What I have done on my laptop is make a folder in /home/billy/ called bin (/home/billy is my home directory, yours will be /home/yourUsername/ ).
    So click n PLACES in your toolbar, then click on HOME FOLDER. This is where you need to make your personal BIN folder. You can call this folder whatever you want, but I would call it bin for convention.
    Add the following to the end of your PATH variable that you have open in your text editor. Add it after all the other folders, but before the closing "-
    Code:
    :/home/username/bin
    Where username is the username you use to log in to your ubuntu account when you turn on your computer & bin is the folder you just created in your home directory.
    The file that you have open in your text editor should now look something like this-
    Code:
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/username/bin"
    Once again replacing username with your login username & bin with the name of the folder you have just created in your home directory.

    Now, finally, all you need to do is edit your .bashrc file.
    Type the following in a NEW terminal window-
    Code:
    gedit .bashrc
    This will open a new text editor window with the contents of your .bashrc file.

    All you need to do is add the following to the bottom of the open file-
    Code:
     # set PATH so it includes user's private bin if it exists
     if [ -d ~/bin ] ; then
         PATH=~/bin:"${PATH}"
     fi
    -EDIT-
    To make sure the script can be run, open a terminal and cd to the directory that the backup script is in. Run the following command to make it executable.
    Code:
    sudo chmod +x backup
    You will need to set +x for all scripts you make, as this is to set it as an EXECUTABLE FILE.
    -EDIT-

    Now SAVE & CLOSE the text editor and any other windows you might still have open from the previous exercise.

    This means that whenever you wish to backup your system, all you have to do is open a terminal window and type the following before hitting ENTER-
    Code:
    backup
    VOILA!!
    Test it out!
    Hopefully, once the script has finished running, there will be a file (of significant size) called backup.tgz in your home folder (which, in my case, is /home/billy/ ).

    And that's it!
    Now just run backup whenever you wish to back up your system.
    You can also now add a button to your system toolbar that is linked to this script, just by making it run "backup" !!!

    For more information on these commands, check out the forum and the rest of the ubuntu community for tutorials, examples and other helpful stuff.

    I hope that this has been of some use to someone...
    Please feel free to comment and criticise
    As I said before, I will write a restore script and put it in this thread, along with a similar explanation of it line-by-line.

    For those who wish to know, I plan to add the ability to let the user choose which folders (if any) to exclude.

    Anyway, Thanks for reading and I hope it helps you with your endeavours.

    !!! VIVA LA LINUX !!!

    lonewaster
    Last edited by lonewaster; March 7th, 2010 at 09:14 PM. Reason: addition of chmod +x section

  2. #2
    Join Date
    Mar 2010
    Beans
    40

    Arrow Re: Easy Way To Backup Your System!! (Bash Script)

    I know it's only been a few hours, but I got bored so I added the ability for the user to choose the exclusions(folders that he/she DOESN'T want added to the backup.tgz).

    I will explain and then provide you with a copy of the entire script, so you can either learn how to do it yourself, or just copy & paste into a file...but that's the lazy way!

    WARNING: IF YOU DO NOT UNDERSTAND WHICH FOLDERS SHOULD.SHOULDN'T BE INCLUDED IN A BACKUP, THEN JUST STICK WITH V.1.3 (THE SCRIPT IN MY FIRST POST ABOVE THIS ONE) AS IT DOES IT ALL FOR YOU!
    -BACKING UP FOLDERS THAT AREN'T REALLY NEEDED CAN RESULT IN UNFORSEEN PROBLEMS & AN EXCESSIVELY BIG BACKUP.TGZ FILE!!-

    Okay, so...
    Remember the following line from our old V.1.3 ?

    sudo tar cvpzf ../../home/$backupUser/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /

    Well, all we need to do is replace that with the following-
    Code:
    # Backup Entire System To the Specified User's homedir/backup.tgz (../../ included so script can be put in subdirectory)
    echo "Enter any directories you want to exclude from the backup.tgz"
    echo "Syntax: --exclude=/folder"
    read -p "Please enter them now, each separated with a space: " strExclusions
    # Ask the user for a list of folders in the root directory to exclude from the backup.tgz
    # And then backup the system, excluding all the folders the user chose (which are saved in $strExclusions)
    sudo tar cvpzf ../../home/$backupUser/backup.tgz $strExclusions /
    Now, although I have explained this in the comments (anything on a line after a # ), I will go into more depth.........

    Okay, well firstly I have already explained the echo function, and it just prints stuff on the screen
    I also explained the use of read in the last post. All it does is prints something on the screen(just like echo) and then places whatever the user types in in the specified variable(in this case $strExclusions).
    What I have done is use echo to explain to the user HOW to specify what folders to exclude.
    This is saved in the $strExclusions variable, which is then passed to the computer in the backup process-
    Code:
    sudo tar cvpzf ../../home/$backupUser/backup.tgz $strExclusions /
    Well...that's it!
    If you are having trouble with any of the commands used here, then check my first post and if you are still finding it hard then either google it, check the rest of the forums or reply to this thread and I will try and help!

    Finally, Here is the entire script for those of you who came here for a copy & paste job.

    Code:
    #!/bin/bash
    # Print Pre-text Containing Script Version Info & Creator Info (please leave current credits intact and add a separate line if you modify the script & pass it on)
    clear
    echo "Backup Script"
    echo ""
    echo "V.2"
    echo ""
    echo "Created by Lonewaster@gmail.com"
    echo "VIVA LA LINUX!!!"
    echo ""
    read -p "Please Press ENTER To Continue..." waitForEnter
    # Alert The User & Wait 3 Clicks
    echo "BACKUP Initiating..."
    echo ""
    sleep 3
    # Ask The User For Their Current Username (For Backup File Placement & Permissions)
    read -p "What username are you currently logged into? (must be IDENTICAL to username you are currently using!!): " backupUser
    # Backup Entire System To the Specified User's homedir/backup.tgz (../../ included so script can be put in subdirectory)
    echo "Enter any directories you want to exclude from the backup.tgz"
    echo "Syntax: --exclude=/folder"
    read -p "Please enter them now, each separated with a space: " strExclusions
    # Ask the user for a list of folders in the root directory to exclude from the backup.tgz
    # And then backup the system, excluding all the folders the user chose (which are saved in $strExclusions)
    sudo tar cvpzf ../../home/$backupUser/backup.tgz $strExclusions /
    # Upon Completion Of Backup, Alert The User
    echo ""
    echo "BACKUP Complete!"
    echo ""
    # Then Set backup.tgz File Privilidges So That The Specified USER Can Read, Write & Execute
    echo "Setting Backup File Privilidges..."
    echo ""
    # Set The File's Group As the Specified $backupUser
    chgrp $backupUser ../../backup.tgz
    # Set Read, Write & Execute Privilidges For The Owner (root)
    chmod u+rwx ../../backup.tgz
    # Set Read, Write & Execute Privilidges For The Specified Group ($backupUser)
    chmod g+rwx ../../backup.tgz
    # Alert The User, Wait 3 Clicks & Terminate The Script
    echo "TERMINATING..."
    echo ""
    sleep 3
    # End Of Backup.sh Script
    REMINDER: If you are just copying & pasting you will still have to run the chmod +x backup.tgz command to make it executable.
    That's all...

    I will no doubt make more additions and improvements, but for now I need to get some sleep. Got a date tomorrow! (and we all know a date for a geek is somewhat of a rare occurrence )

    Until then.........

    !!! VIVA LA LINUX !!!

    lonewaster

    -EDIT-
    Remember that if this helped you AT ALL, then PLEASE reply and let me know!
    Also give me any ideas or criticism you have, too.
    I welcome it all.
    Cheers!
    -EDIT-
    Last edited by lonewaster; March 6th, 2010 at 03:29 AM.

  3. #3
    Join Date
    Mar 2010
    Beans
    7
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Easy Way To Backup Your System!! (Bash Script)

    I have been looking for a wile now on how to back up ubuntu.
    with no luck, after formating the wrong drive
    I was thinking ubuntu needs a backup.

    How do I restore the backups.


    I have been looking a backup
    in case I do some thing well stupid.


    And thanks for taking the time to
    help us all and share your code.

  4. #4
    Join Date
    Apr 2009
    Location
    .ca
    Beans
    192
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Easy Way To Backup Your System!! (Bash Script)



    This is excellent for someone like me! Thanks lonewaster!

  5. #5
    Join Date
    Sep 2009
    Location
    Bangkok, Thailand
    Beans
    228
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Easy Way To Backup Your System!! (Bash Script)

    Quote Originally Posted by hoopoo View Post
    How do I restore the backups.
    It depends how much you want to restore. If you accidentally delete a file or directory, it's very easy to restore. If you follow lonewaster's detailed instructions, you'll have a file called backup.tgz. That is a compressed file, like windows .zip files. Open backup.tgz using 'fileroller', find the directory or file that you want to restore, and extract them to wherever you want to put them.

    If you have a more serious error and screw up your whole system, lose all your files, your harddrive, whatever, it's a bit harder, but not too hard! Find an Ubuntu installation CD, make a fresh installation. You then just need to extract the entire contents of that backup.tgz file to your new installation.


    On a related note (and I don't want to detract from the great instructions given here), there is a way to emulate Apple's 'time machine'. It will take snapshot backups of your harddrive whenever you choose. It does not require backuping up every single file every time, but it only looks for the files that have been changed since your last backup. Full details here: http://blog.interlinked.org/tutorial...e_machine.html

  6. #6
    Join Date
    Apr 2009
    Location
    .ca
    Beans
    192
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Easy Way To Backup Your System!! (Bash Script)

    Please I need this answered. Will this back-up my Filesystem as well as my LOCAL DISK(which has my C:\ and D:\ drives)?

    I can back up to an external hard drive and restore on a new disk if I have a hard drive failure? It will copy my partitions?

    Thanks!

  7. #7
    Join Date
    Aug 2009
    Location
    Under the stairs.
    Beans
    1,408
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Easy Way To Backup Your System!! (Bash Script)

    Thank you for this brilliant script!
    Dell Inspiron 1764 Laptop, Intel CoreTM i5 520M), 4GB Shared Dual Channel DDR3 at 1066MHz, 512MB ATI Mobility RadeonTM HD4330 Integrated Intel HD.

  8. #8
    Join Date
    Mar 2010
    Beans
    7
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Easy Way To Backup Your System!! (Bash Script)

    Thanks oldmankit for the replay and the help.
    lonewaster your script seams to be jest
    what I was looking for.

    If some thing like this was built into ubuntu
    it might help so many others to fix there
    systems and save frustrations not only
    for the end user but the ones helping
    in the forums.
    I know she (lonewaster script)
    would have saved alot if my hair from being
    riped out by the root trying to get a "VM"
    up and running again a couple of days ago.

    PS. keep the help coming lonewaster

  9. #9
    Join Date
    Aug 2006
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Easy Way To Backup Your System!! (Bash Script)

    This work is not original inspired by another howto, and the OP should give credit where it's due.

    http://ubuntuforums.org/showthread.php?t=35087

    ------------------------------------------------------------------------------------------------------------

    Quote Originally Posted by isee View Post
    Please I need this answered. Will this back-up my Filesystem as well as my LOCAL DISK(which has my C:\ and D:\ drives)?

    I can back up to an external hard drive and restore on a new disk if I have a hard drive failure? It will copy my partitions?

    Thanks!
    No. The mount directories are excluded so that the backup will not go into Windows. Furthermore, restoring to another hdd takes more then just copying the files. I recommend you look at Clonezilla.
    Last edited by mikewhatever; March 6th, 2010 at 08:26 PM.

  10. #10
    Join Date
    Oct 2005
    Location
    Al Ain
    Beans
    8,123

    Re: Easy Way To Backup Your System!! (Bash Script)

    Hmmm, it is mostly fresh ex-Windows users that are looking for backup scripts. Dervish and Rbackup are pretty good.

    However, old hands realize that it is a waste of time backing up everything since after disaster striked, you would probably want to install the latest version of Ubuntu, not a 3 year old multiple updated semi messed up system from a months old backup...

    Soooo, how do old hands do it? Read the rsync man page (there are examples at the end) and then craft a one liner in a desktop launcher to backup your data only - forget about the rest of the system, it doesn't matter.

Page 1 of 4 123 ... 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
  •