LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2012, 01:53 PM   #1
Leroy1990
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Rep: Reputation: Disabled
Writing a bash script


I need some help writing a bash script to backup files from my system and put them into a compressed folder.

I would like to integrate cron with this as well

file1=/home/user/etc

using tar -czf

Basically, I have a folder on my desktop that I would like to have backed up and archived on a schedule or whenever the files within it change. Any help on how to execute this?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-28-2012, 02:11 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
@ Reply

Hi Leroy1990,

Welcome to LQ!!!

Here is how your script should go:

1. It should create an archive using tar -cvf say: tar -cvf foldter.tar.gz folder_name
2. Script should look for folder.tar.gz. If it already exist then does not create a new one just echo that folder existing and continue with the next step.
3. If folder.tar.gz exist then you can append to it using tar -rf folder.tar.gz folder_name

It will be good if you would have pasted the script that you have already prepared.
 
Old 02-28-2012, 02:57 PM   #3
Leroy1990
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by T3RM1NVT0R View Post
Hi Leroy1990,

Welcome to LQ!!!

Here is how your script should go:

1. It should create an archive using tar -cvf say: tar -cvf foldter.tar.gz folder_name
2. Script should look for folder.tar.gz. If it already exist then does not create a new one just echo that folder existing and continue with the next step.
3. If folder.tar.gz exist then you can append to it using tar -rf folder.tar.gz folder_name

It will be good if you would have pasted the script that you have already prepared.
I'm still new to linux in general and bash scripting. I may need to consult other resources to find out the basics of how to write a bash script.

My bash script will be structured as follows,

Code:
#! /bin/bash
echo back up start 'date' >> ~/backuplog
mkdir /home/user/Desktop/backups/' date +%Y%m%d'
tar -czf /home/user/Desktop/backups/' date +%Y%m%d'/data.tar.gz/data
echo this back up has completed ' date' >> ~/backuplog
For whatever reason though, this is currently not working.
 
Old 02-28-2012, 03:20 PM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
@ Reply

Alright. Here is how it should look like:

Code:
#!/bin/bash
echo "Backup has been started"
mkdir /home/user/Desktop/backups/`date +%Y%m%d` 

ls -l /data/data.tar.gz
if [ $? = 2]
then
    echo "Backup archive does not exist. Creating..."
    tar -cvf /data/data.tar.gz /home/user/Desktop/backups/`date +%Y%m%d`
    echo "Backup completed successfully for" `date +%Y%m%d`
else
    echo "Backup archive already exist. Appending..."
    tar -rf /data/data.tar.gz /home/user/Desktop/backups/`date +%Y%m%d`
    echo "Backup completed successfully for" `date +%Y%m%d`
fi
Try the above mentioned script. You can change the location of data.tar.gz as per your requirement. But make sure that you change in all instances.

I would also recommend you to go through these links which will help you with bash scripting:

http://tldp.org/LDP/abs/abs-guide.pdf
http://tldp.org/LDP/Bash-Beginners-G...ners-Guide.pdf
 
Old 02-28-2012, 09:55 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Here's a stylistic alternative, with the same functionality as T3RM1NVT0R's script:
Code:
#!/bin/bash
echo "Backup has been started"

date=$(date +%Y%m%d)  # So the date command is run only once, ensuring OK if script runs over midnight 

archive=/data/data.tar.gz    # So only one place to change it if it changes
backup_dir="/home/user/Desktop/backups/$date"    # So only one place to change it if it changes
mkdir "$backup_dir" 

if [[ -e "$archive ]]; then    # Use a file existence test rather than the ls return code
    echo "Backup archive $archive does not exist. Creating ..."
    tar_opts=-cvf    # So there need be only one tar command in the script
else
    echo "Backup archive $archive already exists. Appending ..."
    tar_opts=-rf
fi

tar $tar_opts "$archive"  "$backup_dir"
# No "successful" message here because it may not have been (in which case error messages from tar tell the story

Last edited by catkin; 02-28-2012 at 09:56 PM. Reason: put [/CODE] in right place
 
2 members found this post helpful.
Old 03-01-2012, 02:57 PM   #6
Leroy1990
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Here's a stylistic alternative, with the same functionality as T3RM1NVT0R's script:
Code:
#!/bin/bash
echo "Backup has been started"

date=$(date +%Y%m%d)  # So the date command is run only once, ensuring OK if script runs over midnight 

archive=/data/data.tar.gz    # So only one place to change it if it changes
backup_dir="/home/user/Desktop/backups/$date"    # So only one place to change it if it changes
mkdir "$backup_dir" 

if [[ -e "$archive ]]; then    # Use a file existence test rather than the ls return code
    echo "Backup archive $archive does not exist. Creating ..."
    tar_opts=-cvf    # So there need be only one tar command in the script
else
    echo "Backup archive $archive already exists. Appending ..."
    tar_opts=-rf
fi

tar $tar_opts "$archive"  "$backup_dir"
# No "successful" message here because it may not have been (in which case error messages from tar tell the story
I'm getting a syntax error in line 10

Code:
backup1.sh: line 10: syntax error in conditional expression
backup1.sh: line 11: syntax error near `archive'
 
Old 03-01-2012, 04:40 PM   #7
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
@ Reply

Put the double quotes as:

Code:
if [[ -e "$archive" ]]; then    # Use a file existence test rather than the ls return code
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need some help writing a bash script... trist007 Programming 8 04-15-2009 11:31 AM
looking for help in writing a bash script onepostonly Linux - General 3 10-05-2008 07:42 PM
Writing a bash script. lebabyg Linux - General 2 03-31-2007 12:39 PM
Bash (help writing script) lebabyg Linux - General 7 07-04-2006 06:22 PM
writing bash script ankitgdit Programming 4 08-19-2003 07:47 AM


All times are GMT -5. The time now is 01:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration