UNIX Shell Script Backup Directory on Specific day
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Basically this is the question im trying to answer
2) Write a shell script (fridaybackup.sh) which will, if run on Friday, backup all files in user2 home directory. The script will create a backup (tar) file named user2backup.tar and zip file named user2backup.zip
This is what I have so far
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Monday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
But I get this result
student@solaris:~/user2$ sh fridaybackup.sh
Do you want to create a full backup (Y=Yes, N=No) : Y
tar: /export/home/student/backupfolder/user2backup10-02-17.tar.gz: No
such file or directory
student@solaris:~/user2$ gedit fridaybackup.sh
Any help would be great and thanks in advance
shell scripting tar zip
add a comment |Â
up vote
0
down vote
favorite
Basically this is the question im trying to answer
2) Write a shell script (fridaybackup.sh) which will, if run on Friday, backup all files in user2 home directory. The script will create a backup (tar) file named user2backup.tar and zip file named user2backup.zip
This is what I have so far
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Monday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
But I get this result
student@solaris:~/user2$ sh fridaybackup.sh
Do you want to create a full backup (Y=Yes, N=No) : Y
tar: /export/home/student/backupfolder/user2backup10-02-17.tar.gz: No
such file or directory
student@solaris:~/user2$ gedit fridaybackup.sh
Any help would be great and thanks in advance
shell scripting tar zip
1
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Basically this is the question im trying to answer
2) Write a shell script (fridaybackup.sh) which will, if run on Friday, backup all files in user2 home directory. The script will create a backup (tar) file named user2backup.tar and zip file named user2backup.zip
This is what I have so far
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Monday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
But I get this result
student@solaris:~/user2$ sh fridaybackup.sh
Do you want to create a full backup (Y=Yes, N=No) : Y
tar: /export/home/student/backupfolder/user2backup10-02-17.tar.gz: No
such file or directory
student@solaris:~/user2$ gedit fridaybackup.sh
Any help would be great and thanks in advance
shell scripting tar zip
Basically this is the question im trying to answer
2) Write a shell script (fridaybackup.sh) which will, if run on Friday, backup all files in user2 home directory. The script will create a backup (tar) file named user2backup.tar and zip file named user2backup.zip
This is what I have so far
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Monday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
But I get this result
student@solaris:~/user2$ sh fridaybackup.sh
Do you want to create a full backup (Y=Yes, N=No) : Y
tar: /export/home/student/backupfolder/user2backup10-02-17.tar.gz: No
such file or directory
student@solaris:~/user2$ gedit fridaybackup.sh
Any help would be great and thanks in advance
shell scripting tar zip
shell scripting tar zip
edited Oct 2 '17 at 12:52
asked Oct 2 '17 at 10:58
Taylor Cairns
32
32
1
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05
add a comment |Â
1
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05
1
1
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
there seems to be a copy/paste problem,
variable must be set using
day=$(date +%A)
I believe you use backtick, that dissapear under formating, as "Monday" error shows.
there is a syntax error near if
error Monday: not found
prove that setting was correct, as in line 14 bash encounter :
( Monday = friday )
this forked a sub shell, running Monday
as program name and = Friday
as argument. This is not what you intend.
In shell test are done using
if [ "$day" = "Friday" ]
or
if test "$day" = "Friday"
thus correct shell look like :
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Friday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
add a comment |Â
up vote
0
down vote
Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT
instead of your:
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.
This would be the best and most safe way to declare your filename
variable:
filename="user2backup$time.tar.gz"
add a comment |Â
up vote
0
down vote
The description does not require asking the user. It could do it automatically:
#!/bin/sh
if [ $(date +%A) = Friday ]; then
TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
tar -cpf $TARFILE /export/home/student/user2
zip $TARFILE.zip $TARFILE
fi
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
there seems to be a copy/paste problem,
variable must be set using
day=$(date +%A)
I believe you use backtick, that dissapear under formating, as "Monday" error shows.
there is a syntax error near if
error Monday: not found
prove that setting was correct, as in line 14 bash encounter :
( Monday = friday )
this forked a sub shell, running Monday
as program name and = Friday
as argument. This is not what you intend.
In shell test are done using
if [ "$day" = "Friday" ]
or
if test "$day" = "Friday"
thus correct shell look like :
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Friday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
add a comment |Â
up vote
1
down vote
accepted
there seems to be a copy/paste problem,
variable must be set using
day=$(date +%A)
I believe you use backtick, that dissapear under formating, as "Monday" error shows.
there is a syntax error near if
error Monday: not found
prove that setting was correct, as in line 14 bash encounter :
( Monday = friday )
this forked a sub shell, running Monday
as program name and = Friday
as argument. This is not what you intend.
In shell test are done using
if [ "$day" = "Friday" ]
or
if test "$day" = "Friday"
thus correct shell look like :
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Friday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
there seems to be a copy/paste problem,
variable must be set using
day=$(date +%A)
I believe you use backtick, that dissapear under formating, as "Monday" error shows.
there is a syntax error near if
error Monday: not found
prove that setting was correct, as in line 14 bash encounter :
( Monday = friday )
this forked a sub shell, running Monday
as program name and = Friday
as argument. This is not what you intend.
In shell test are done using
if [ "$day" = "Friday" ]
or
if test "$day" = "Friday"
thus correct shell look like :
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Friday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
there seems to be a copy/paste problem,
variable must be set using
day=$(date +%A)
I believe you use backtick, that dissapear under formating, as "Monday" error shows.
there is a syntax error near if
error Monday: not found
prove that setting was correct, as in line 14 bash encounter :
( Monday = friday )
this forked a sub shell, running Monday
as program name and = Friday
as argument. This is not what you intend.
In shell test are done using
if [ "$day" = "Friday" ]
or
if test "$day" = "Friday"
thus correct shell look like :
#!/bin/sh
echo
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
day=$(date +%A)
time=$(date +%m-%d-%y)
filename=user2backup$time.tar.gz
srcdir='/export/home/student/user2'
desdir='/export/home/student/backupfolder'
case $INPUT in
N) echo "Bye." ;;
Y)
if [ "$day" = "Friday" ]
then
tar -cpzf $desdir/$filename $srcdir
else
echo "A full backup is done on Fridays only!!!"
fi ;;
*) echo "Error" ;;
esac
edited Oct 2 '17 at 11:20
answered Oct 2 '17 at 11:13
Archemar
19.1k93366
19.1k93366
add a comment |Â
add a comment |Â
up vote
0
down vote
Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT
instead of your:
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.
This would be the best and most safe way to declare your filename
variable:
filename="user2backup$time.tar.gz"
add a comment |Â
up vote
0
down vote
Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT
instead of your:
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.
This would be the best and most safe way to declare your filename
variable:
filename="user2backup$time.tar.gz"
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT
instead of your:
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.
This would be the best and most safe way to declare your filename
variable:
filename="user2backup$time.tar.gz"
Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT
instead of your:
echo "Do you want to create a full backup (Y=Yes, N=No) : c"
read INPUT
EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.
This would be the best and most safe way to declare your filename
variable:
filename="user2backup$time.tar.gz"
edited Oct 2 '17 at 11:13
answered Oct 2 '17 at 11:08
Godvil
337
337
add a comment |Â
add a comment |Â
up vote
0
down vote
The description does not require asking the user. It could do it automatically:
#!/bin/sh
if [ $(date +%A) = Friday ]; then
TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
tar -cpf $TARFILE /export/home/student/user2
zip $TARFILE.zip $TARFILE
fi
add a comment |Â
up vote
0
down vote
The description does not require asking the user. It could do it automatically:
#!/bin/sh
if [ $(date +%A) = Friday ]; then
TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
tar -cpf $TARFILE /export/home/student/user2
zip $TARFILE.zip $TARFILE
fi
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The description does not require asking the user. It could do it automatically:
#!/bin/sh
if [ $(date +%A) = Friday ]; then
TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
tar -cpf $TARFILE /export/home/student/user2
zip $TARFILE.zip $TARFILE
fi
The description does not require asking the user. It could do it automatically:
#!/bin/sh
if [ $(date +%A) = Friday ]; then
TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
tar -cpf $TARFILE /export/home/student/user2
zip $TARFILE.zip $TARFILE
fi
answered Oct 2 '17 at 13:02
hschou
1,66349
1,66349
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395622%2funix-shell-script-backup-directory-on-specific-day%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A)
â Raman Sailopal
Oct 2 '17 at 11:05