Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey, im very new to shell scripting and Linux in general.
I have a cron job for a backup script that runs once a night and I want to improve that shell script and add inputs and if statements and other stuff.
I did some reading and I couldn't find what I wanted so I'm going to post a mock script with the process I want to run.
Code:
#!/bin/bash
FOLDER1SRC="/home/username/directory/foldertobackup1"
FOLDER1DEST="/home/username/directory/foldersbackuped1"
FOLDER2SRC="/home/username/directory/foldertobackup2"
FOLDER2DEST="/home/username/directory/foldersbackedup2"
FILENAME1=foldername-folder1-bkp-$(date +%-Y%-m%-d)-$(date +%-T).tgz
FILENAME2=foldername-folder2-bkp-$(date +%-Y%-m%-d)-$(date +%-T).tgz
STOP=quit
CONTINUE=yes
NEXT=no
echo "If this is a manual run, type yes"
wait 10s for an input, if no input skip to AUTOBACKUP
while :
do
read INPUT_STRING
case $INPUT_STRING in
yes)
echo "Hi there! Welcome to my backup script!"
sleep 1s
echo "Are we backing up folder1?"
if $CONTINUE
do
echo "This could take over an hour"
cd $WORLDDIR
echo "Archiving files to $FOLDER1DEST"
tar --exclude='./sub_folder1' --exclude='./sub_folder2' --exclude='./sub_folder3' --exclude='./sub_folder3' --exclude='./sub_folder4' -zcvf $FOLDER1SRC$FILENAME1 $FOLDER1DEST
echo "Archiving finished and stored in $FOLDER1SRC, would you like to upload this archive to google?"
if $CONTINUE
do gsutil rsync -r /home/folder/directory gs://folder-directory/folder1
else done
echo "upload finished, would you like to remove any old backups?"
if $CONTINUE
echo "deleting backups from $FOLDER1DEST"
rm -r /home/username/directory/folder1dest/*
else done
echo "backups deleted, would you like to back up FOLDER2 as well?"
if $CONTINUE
continue to no)
else exit
no)
echo "Are we backing up folder2?"
if $CONTINUE
do
cd $FOLDER2DIR
echo "Archiving files to $FOLDER2DEST"
tar -zcvf $FOLDER2SRC$FILENAME2 $FOLDER2DEST
echo "Archiving finished and stored in $FOLDER2DEST, would you like to upload this archive to google?"
if $CONTINUE
do gsutil rsync -r /home/folder/directory gs://folder-directory/folder2
else done
echo "upload finished, would you like to remove any old backups?"
if $CONTINUE
echo "deleting backups from $FOLDER2DEST"
rm -r /home/username/directory/folder2dest/*
sleep 1s
echo "files deleted, script will now close"
exit
else exit
AUTOBACKUP)
cd $FOLDER2DIR
tar -zcvf $FOLDER2SRC$FILENAME2 $FOLDER2DEST
do gsutil rsync -r /home/folder/directory gs://folder-directory/folder2
sleep 10m
rm -r /home/username/directory/folder2dest/*
exit
Please keep in mind, I have next to no idea what im doing. So I don't expect my mock-up to work at all but its an example of what I'd like to accomplish, could someone guide me?
A cron script has to be autonomous. It can not ask user inputs.
You can add a config file and modify this config file to make it do something else.
Or, still using cron, launch it several times with different parameters at different times.
echo "If this is a manual run, type yes"
wait 10s for an input, if no input skip to AUTOBACKUP
Let me just mention that you can't use inputs in a cron script.
What strikes me: After the prompts, you don't read any answer. And the read at the beginning of the while loop has no prompt. STOP and NEXT are not used. CONTINUE is always "yes".
I don't quite understand why you need a loop. Does the script continuously backup FOLDER1 and FOLDER2?
Syntax errors: The ifs have neither then nor fi. while and case are not closed. else done won't work.
The OP stated they were new to programming and it was just a mock up i.e. pseudo code. I assume the idea was to use the same script if running from cron or manually via the command line. bash read does have a timeout option. I agree the infinite while loop serves no purpose.
Instead of asking a bunch of questions you could use a few command line arguments. Using getopts would make it easier to parse the arguments.
The OP stated they were new to programming and it was just a mock up i.e. pseudo code. I assume the idea was to use the same script if running from cron or manually via the command line. bash read does have a timeout option. I agree the infinite while loop serves no purpose.
Instead of asking a bunch of questions you could use a few command line arguments. Using getopts would not make it easier to parse the arguments.
Using getopts would not make it easier to parse the arguments.
Hmm. I think you understood what I was trying to write and that getopts would make it easier to parse the command line arguments. Using none or a specific option could be used to setup an auto backup when running via cron.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.