Write a script that automates tar so the person executing the script always uses the desired options (cvp) and backup destination

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
Can someone help me with this script please.
Write a script that automates tar so the person executing the script always uses the desired options (cvp) and backup destination
 


OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
That sounds a lot like a homework question.
Which part of the script are you stuck on?
# What to backup.
datapath='/home'

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
filename=$(date +%u-%a-%Y%m%d)
tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g

# Print end status message.

echo "Backup finished"

I had done this much and getting this error
 

Attachments

  • 1635925244766.png
    1635925244766.png
    12.8 KB · Views: 44

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
You made a syntax error using the tar command, it should be:
Code:
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
And you could add the tar.gz into the variable filename, so that would like like this for your tar script.
Bash:
#!/bin/bash

# What to backup.
datapath='/home'

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
#filename=$(date +%u-%a-%Y%m%d)
filename=$(date +%u-%a-%Y%m%d).tar.gz

#tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g
tar -cvpzf $backuppath/$filename $backuppath

# Print end status message.
echo "Backup finished"
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,377
Reaction score
1,947
Credits
9,109
OK - a couple of things there.
1. You have declared three variables.
One called datapath and one called backuppath and one called filename and you have assigned values to them.
This is fine!

But in your tar command, you are using an empty/undeclared variable called fullbackup - that is a problem.

Also, it looks like your ordering of the tar command is a little off.
Immediately after using the f option you must specify the output file name.
Then you list the directories/files to include in the archive.
In your command - $backuppath/$filename will be the output file.
and $fullbackup.tar.gz-g will be the list of files/directories to back up.

Also, looking at your datapath variable - you appear to be backing up the entire /home/ directory - which will back up the home directories of all users on the system - is this correct?

Another thing that is worth bearing in mind is:-
When assigning values to string variables using values from command substitutions, or from other string variables - in order to avoid problems with special characters - you should enclose the entire value in double quotes - completely containing any substitutions, or values from other variables.
e.g.
somevariable="$somePath/$someFilename-$(somecommand)"

So cleaning up what you have so far:
Bash:
#!/usr/bin/env bash

datapath='/home'
backuppath='/var/backup'
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"

tar cvpzf "$filename" "$datapath"
echo "Backup finished"
In the above:
1. Always use a shebang #! to specify the interpreter to use for the script.
That way you can (optionally) save your script with no file-extension (e.g. 'myscript' instead of 'myscript.sh') and the shell will always run it using the specified interpreter. So as this is a bash script, I've used #!/usr/bin/env bash to specify bash as the correct interpreter for the script.

2. The variables datapath and backuppath are the same as your script.

3. The filename variable has been set-up to contain the entire output file-name.
So we have combined the values from $backuppath, your date command plus the string "-fullbackup.tar.gz"
So, assuming I've read your date command correctly - the path/filename of the output file will be saved as something like /var/backup/3-Wed-20211103-fullbackup.tar.gz

4. Finally, the tar command uses the same flags as your script (cvpzf). But now we reference the $filename variable for our output filename and the datapath variable as the list of files/direcories to back-up - which in this case will be the entirety of /home/.

Additionally, you should also check that the directory in $backuppath exists before performing your tar command. Otherwise tar will be unable to create the backup file at that location and will throw an error message at you.
So you might want to add something like this BEFORE your tar command:
Bash:
if [ ! -d "$backuppath" ] ; then
  mkdir "$backuppath"
fi

As you're writing to /var/ AND you're backing up the /home/ directories of ALL users - I'm assuming that your script will be running as root. So the above code should work.

[edit]
Dammit - @f33dm3bits got in there whilst I was writing my essay! Ha ha!
[/edit]
 
Last edited:

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,377
Reaction score
1,947
Credits
9,109
You made a syntax error using the tar command, it should be:
Code:
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
And you could add the tar.gz into the variable filename, so that would like like this for your tar script.
Bash:
#!/bin/bash

# What to backup.
datapath='/home'

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
#filename=$(date +%u-%a-%Y%m%d)
filename=$(date +%u-%a-%Y%m%d).tar.gz

#tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g
tar -cvpzf $backuppath/$filename $backuppath

# Print end status message.
echo "Backup finished"
@f33dm3bits:
You made an error in your code too.
You used $backuppath as the list of files/direcories to include in the tar......
That should be $datapath at the end of your tar command shouldn't it?!
Ha ha!
 
Last edited:
OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
You made a syntax error using the tar command, it should be:
Code:
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
And you could add the tar.gz into the variable filename, so that would like like this for your tar script.
Bash:
#!/bin/bash

# What to backup.
datapath='/home'

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
#filename=$(date +%u-%a-%Y%m%d)
filename=$(date +%u-%a-%Y%m%d).tar.gz

#tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g
tar -cvpzf $backuppath/$filename $backuppath

# Print end status message.
echo "Backup finished"
I am still getting the same error.
Here is my code
1635928364836.png
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
@f33dm3bits:
You made an error in the above code too.
You used $backuppath as the list of files/direcories to include in the tar......
That should be $datapath at the end of your tar command shouldn't it?!
Ha ha!
@JasKinasis Strange I tested the code and it worked when I tried it or it must that I'm not awake yet or something else LOL :D

@harun2k You can also throw in an if statement to that.
Bash:
#!/usr/bin/env bash

datapath='/home'
backuppath='/var/backup'
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"

echo "Creating tar of location $datapath to $backuppath"
tar cvpzf "$filename" "$datapath"

# Print end status message.
if [ $? -eq 0 ]
then
      echo "Backup finished"
else
     echo "Backup failed"
fi
 
Last edited:

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
@JasKinasis
You are correct, I think I miss read the script so it should have been like this in my other post. I changed the post of where I was testing and I misread it when posting it back here. Thanks for the heads up! :)
Bash:
#!/bin/bash

# What to backup.
datapath="/home"

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
#filename=$(date +%u-%a-%Y%m%d)
filename=$(date +%u-%a-%Y%m%d).tar.gz

#tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g
tar -cvpzf $backuppath/$filename $datapath

# Print end status message.
echo "Backup finished"
Then adding to my other reply when adding an if it would look like this.
Bash:
#!/bin/bash

# What to backup.
datapath="/home"

# Where to backup to.
backuppath="/var/backup"

# Backup the files using tar.
#filename=$(date +%u-%a-%Y%m%d)
filename=$(date +%u-%a-%Y%m%d).tar.gz

#tar -cvpzf $backuppath/$filename $fullbackup.tar.gz-g
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $backuppath/$filename $datapath

# Print end status message.
if [ $? -eq 0 ]
then
      echo "Backup finished"
else
     echo "Backup failed"
fi
 
Last edited:
OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
@JasKinasis Strange I tested the code and it worked when I tried it or it must that I'm not awake yet or something else LOL :D

@harun2k You can also throw in an if statement to that.
Bash:
#!/usr/bin/env bash

datapath='/home'
backuppath='/var/backup'
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"

echo "Creating tar of location $datapath to $backuppath"
tar cvpzf "$filename" "$datapath"

# Print end status message.
if [ $? -eq 0 ]
then
      echo "Backup finished"
else
     echo "Backup failed"
fi
Getting this error
1635929343329.png

1635929414919.png
 

Attachments

  • 1635929324270.png
    1635929324270.png
    41.4 KB · Views: 41

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
You are missing an " which I forgot to add in my post.
echo "Creating tar of location $datapath to $backuppath"
@harun2k can you please post your output as text and/or in code tags, it's not very fun to read text as screenshots.

P.S @JasKinasis is the scripting guru here, I'm just a Linux-noob ;)
 
Last edited:

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,377
Reaction score
1,947
Credits
9,109
Additionally - In your screenshot of your code - you have a typo in your if statement.
You're missing a space after the opening square brace [.
Screenshot says this:
Bash:
if [$? -eq 0 ]
It should be:
Bash:
if [ $? -eq 0 ]

Without the space there, bash thinks that [$? is a command - which it isn't, so it's throwing the "command not found" error.
Also, instead of posting screenshots of your code, you could just use code tags and copy/paste your code!
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
Also missing " at the end in the line I made a mistake in and the correct afterwards.
echo "Creating tar of location $datapath to $backuppath"
 
OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
Additionally - In your screenshot of your code - you have a typo in your if statement.
You're missing a space after the opening square brace [.
Screenshot says this:
Bash:
if [$? -eq 0 ]
It should be:
Bash:
if [ $? -eq 0 ]

Without the space there, bash thinks that [$? is a command - which it isn't, so it's throwing the "command not found" error.
Also, instead of posting screenshots of your code, you could just use code tags and copy/paste your code!
Still this error
1635931224768.png
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
Share your most recent code as text?
 
OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
Share your most recent code as text?
#!/bin/bash datapath='/home'

backuppath='/var/backup'

filename="backuppath/$(date+%u-%a-%Y%m%d)-fullbackup.tar.gz"
echo "Creating tar of location Sdatapath to $backuppath"

tar -cvpzf $filename $datapath
if [ $? -eq 0 ]

then

echo "Backup Finish"

else

"Backup Failed"

fi
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
You missed a $ when using the backuppath variable in filename, also between date and + there should be a space.
Bash:
#!/bin/bash

datapath='/home'
backuppath='/var/backup'
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"

echo "Creating tar of location Sdatapath to $backuppath"
tar -cvpzf $filename $datapath

if [ $? -eq 0 ]
then
echo "Backup Finish"
else
"Backup Failed"
fi
 
OP
H

harun2k

Member
Joined
Nov 3, 2021
Messages
58
Reaction score
6
Credits
387
You missed a $ when using the backuppath variable in filename.
Bash:
#!/bin/bash

datapath='/home'
backuppath='/var/backup'
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"

echo "Creating tar of location Sdatapath to $backuppath"
tar -cvpzf $filename $datapath

if [ $? -eq 0 ]
then
echo "Backup Finish"
else
"Backup Failed"
fi
Getting this error now
 

Attachments

  • ern.png
    ern.png
    73.3 KB · Views: 40

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,119
Reaction score
2,871
Credits
29,739
Share what code you use this time because I tested on my system and it works, you must have an mistake elsewhere.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top