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

Share what code you use this time because I tested on my system and it works, you must have an mistake elsewhere.
#!/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
 


Never mind I made a copy paste error, try this.
Bash:
#!/bin/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

if [ $? -eq 0 ]
then
      echo "Backup Finish"
else
      echo "Backup Failed"
fi
 
Last edited:
And then to add what @JasKinasis said to have it check if the backuppath exists.
Bash:
#!/bin/bash

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

#Check if backuppath exists if not create it
if [ ! -d "$backuppath" ] ; then
  mkdir "$backuppath"
fi

#Create backup and display information about backup
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $filename $datapath

#Check if backup sucesfully completed.
if [ $? -eq 0 ]
then
      echo "Backup Finish"
else
      echo "Backup Failed"
fi
 
Last edited:
And then to add what @JasKinasis said to have it check if the backuppath exists.
Bash:
#!/bin/bash

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

if [ ! -d "$backuppath" ] ; then
  mkdir "$backuppath"
fi

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

if [ $? -eq 0 ]
then
      echo "Backup Finish"
else
      echo "Backup Failed"
fi
Same error still
 

Attachments

  • er2.png
    er2.png
    81.4 KB · Views: 134
Show the code you used?

#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $filename $datapath
if [ $? -eq 0 ]
then
echo "Backup Finish"
else
echo "Backup Failed"
fi
 
It works for me, run the script like this and share the output as text.
Code:
bash -x script-name.sh
 
It works for me, run the script like this and share the output as text.
Code:
bash -x script-name.sh

Error:

/home/lisa/.gnome2/nautilus-scripts/ /home/lisa/.gnome2/panel2.d/

/home/lisa/.gnome2/panel2.d/default/

/home/lisa/.gnome2/panel2.d/default/launchers/ /home/lisa/.bash_logout

/home/lisa/Templates/

/home/lisa/.xauth6vyTL2

/home/lisa/.pulse-cookie

/home/lisa/Pictures/

/home/lisa/.esd_auth

/home/Danish/

/home/Danish/.mozilla/

/home/Danish/.mozilla/plugins/ /home/Danish/.mozilla/extensions/

/home/Danish/.bash_history

/home/Danish/.bashrc

/home/Danish/.bash_profile /home/Danish/.gnome2/

/home/Danish/.bash_logout tar:

Exiting with failure status due to previous errors

+ [ 2 -eq 0 '1' ]
+ echo 'Backup Failed'

Backup Failed
 
As what user are you running the script and can you share the output of the following?
Code:
ls -l /home
ls -l / | grep var
 
Try commenting out a part and then run this.
Code:
#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $filename $datapath
#if [ $? -eq 0 ]
#then
echo "Backup Finish"
#else
#echo "Backup Failed"
#fi
What happens when you run it now? You should run it as root because a normal user doesn't have access to other users home directories. It seems to be something with your setup because when I run the script it successfully completes.
 
I am running as both root as well as normal user
Try commenting out a part and then run this.
Code:
#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $filename $datapath
#if [ $? -eq 0 ]
#then
echo "Backup Finish"
#else
#echo "Backup Failed"
#fi
What happens when you run it now? You should run it as root because a normal user doesn't have access to other users home directories. It seems to be something with your setup because when I run the script it successfully completes.
Try commenting out a part and then run this.
Code:
#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
tar -cvpzf $filename $datapath
#if [ $? -eq 0 ]
#then
echo "Backup Finish"
#else
#echo "Backup Failed"
#fi
What happens when you run it now? You should run it as root because a normal user doesn't have access to other users home directories. It seems to be something with your setup because when I run the script it successfully completes.

After running as root also I am getting that error
 
Change your script to this and then run it and share the output.
Code:
#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
echo "tar -cvpzf $filename $datapath"
#if [ $? -eq 0 ]
#then
echo "Backup Finish"
#else
#echo "Backup Failed"
#fi
 
Change your script to this and then run it and share the output.
Code:
Same error with different echo condition

#!/bin/bash
datapath="/home"
backuppath="/var/backup"
filename="$backuppath/$(date +%u-%a-%Y%m%d)-fullbackup.tar.gz"
if [ ! -d "$backuppath" ] ; then
mkdir "$backuppath"
fi
echo "Creating tar of location $datapath to $backuppath"
echo "tar -cvpzf $filename $datapath"
#if [ $? -eq 0 ]
#then
echo "Backup Finish"
#else
#echo "Backup Failed"
#fi
/home/lisa/.gnome2/nautilus-scripts/ /home/lisa/.gnome2/panel2.d/

/home/lisa/.gnome2/panel2.d/default/

/home/lisa/.gnome2/panel2.d/default/launchers/ /home/lisa/.bash_logout

/home/lisa/Templates/

/home/lisa/.xauth6vyTL2

/home/lisa/.pulse-cookie

/home/lisa/Pictures/

/home/lisa/.esd_auth

/home/Danish/

/home/Danish/.mozilla/

/home/Danish/.mozilla/plugins/ /home/Danish/.mozilla/extensions/

/home/Danish/.bash_history

/home/Danish/.bashrc

/home/Danish/.bash_profile /home/Danish/.gnome2/

/home/Danish/.bash_logout tar:

Exiting with failure status due to previous errors

+ [ 2 -eq 0 '1' ]
+ echo 'Backup Finish'

Backup Finish
 
/home/lisa/.gnome2/nautilus-scripts/ /home/lisa/.gnome2/panel2.d/

/home/lisa/.gnome2/panel2.d/default/

/home/lisa/.gnome2/panel2.d/default/launchers/ /home/lisa/.bash_logout

/home/lisa/Templates/

/home/lisa/.xauth6vyTL2

/home/lisa/.pulse-cookie

/home/lisa/Pictures/

/home/lisa/.esd_auth

/home/Danish/

/home/Danish/.mozilla/

/home/Danish/.mozilla/plugins/ /home/Danish/.mozilla/extensions/

/home/Danish/.bash_history

/home/Danish/.bashrc

/home/Danish/.bash_profile /home/Danish/.gnome2/

/home/Danish/.bash_logout tar:

Exiting with failure status due to previous errors

+ [ 2 -eq 0 '1' ]
+ echo 'Backup Finish'

Backup Finish
This shouldn't be showing because I turned the off tar part of by replacing it with an echo. Can you share the code you used?
 
Can you please stop posting the output as a screenshot but post it as text because it's annoying to read text output from a screenshot.
Yes that is correct, I let you place the echo to help debug it, can you run
the following line.
Code:
tar -cvpzf /var/backup/3-Wed-20211103-fullbackup.tar.gz /home
Share the output as text.
 
Can you please stop posting the output as a screenshot but post it as text because it's annoying to read text output from a screenshot.
Yes that is correct, I let you place the echo to help debug it, can you run
the following line.
Code:
tar -cvpzf /var/backup/3-Wed-20211103-fullbackup.tar.gz /home
Share the output as text.

[root@localhost tabish]# bash -x mpp.sh

+ datapath=/home

+ backuppath=/var/backup

++ date +u-%a-%Y%m%d

+ filename=/var/backup/3-Wed-20211103-fullbackup.tar.gz + '[' '!' -d /var/backup ]'

+ echo Creating tar of location /home to /var/backup'

Creating tar of location /home to /var/backup

+ echo 'tar -cvpzf /var/backup/3-Wed-20211103-fullbackup.tar.gz /home' tar -cvpzf /var/backup/3-Wed-20211103-fullbackup.tar.gz /home

+ echo 'Backup Finish'

Backup Finish
 

Members online


Latest posts

Top