# What to backup.That sounds a lot like a homework question.
Which part of the script are you stuck on?
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
#!/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"
datapath
and one called backuppath
and one called filename
and you have assigned values to them.tar
command, you are using an empty/undeclared variable called fullbackup
- that is a problem.tar
command is a little off.f
option you must specify the output file name.$backuppath/$filename
will be the output file.$fullbackup.tar.gz-g
will be the list of files/directories to back up.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?somevariable="$somePath/$someFilename-$(somecommand)"
#!/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"
#!/usr/bin/env bash
to specify bash
as the correct interpreter for the script.datapath
and backuppath
are the same as your script.filename
variable has been set-up to contain the entire output file-name.$backuppath
, your date
command plus the string "-fullbackup.tar.gz"/var/backup/3-Wed-20211103-fullbackup.tar.gz
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/
.$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.tar
command:if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
/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.You explanations are always better and more detailed so OP will learn more from your reply moreDammit - @f33dm3bits got in there whilst I was writing my essay! Ha ha!
@f33dm3bits:You made a syntax error using the tar command, it should be:
And you could add the tar.gz into the variable filename, so that would like like this for your tar script.Code:tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
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"
$backuppath
as the list of files/direcories to include in the tar......$datapath
at the end of your tar
command shouldn't it?!I am still getting the same error.You made a syntax error using the tar command, it should be:
And you could add the tar.gz into the variable filename, so that would like like this for your tar script.Code:tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
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 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@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 yourtar
command shouldn't it?!
Ha ha!
#!/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
#!/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"
#!/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
Getting this error@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
@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
@harun2k can you please post your output as text and/or in code tags, it's not very fun to read text as screenshots.echo "Creating tar of location $datapath to $backuppath"
Additionally - In your screenshot of your code - you have a typo in your
if
statement.[
.if [$? -eq 0 ]
if [ $? -eq 0 ]
bash
thinks that [$?
is a command - which it isn't, so it's throwing the "command not found" error.echo "Creating tar of location $datapath to $backuppath"
Still this errorAdditionally - In your screenshot of your code - you have a typo in yourif
statement.
You're missing a space after the opening square brace[
.
Screenshot says this:
It should be:Bash:if [$? -eq 0 ]
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!
#!/bin/bash datapath='/home'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
Getting this error nowYou 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