LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Writing a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/writing-a-bash-script-931773/)

Leroy1990 02-28-2012 01:53 PM

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?

T3RM1NVT0R 02-28-2012 02:11 PM

@ 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.

Leroy1990 02-28-2012 02:57 PM

Quote:

Originally Posted by T3RM1NVT0R (Post 4614344)
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.

T3RM1NVT0R 02-28-2012 03:20 PM

@ 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

catkin 02-28-2012 09:55 PM

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


Leroy1990 03-01-2012 02:57 PM

Quote:

Originally Posted by catkin (Post 4614633)
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'


T3RM1NVT0R 03-01-2012 04:40 PM

@ Reply
 
Put the double quotes as:

Code:

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


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