You are not logged in.
Here is my first real attempt at bash scripting. The first is a simple script to back up some of my files and configs everytime my comp is shutdown:
#!/bin/bash
#Archive and compress files
tar -czf /home/casey/ArchBoxBak.tar.gz /home/casey/.bashrc /home/casey/.conkyrc /home/casey/.xinitrc /home/casey/scripts /home/casey/.keytouch2 /home/casey/.config
#Copy to Server
scp /home/casey/ArchBoxBak.tar.gz casey@192.168.1.200:/home/casey
#Clean up
rm /home/casey/ArchBoxBak.tar.gz
The second is a semi-automated install script to be run after a fresh install from a cd:
#!/bin/bash
#Script to install programs and set themes
#Must be run as root
echo ; clear
#Check that the user updated the kernel and restarted before proceeding
echo -n "Have you already updated the kernel and restarted? (Y/n) "
read Keypress1
case "$Keypress1" in
[Nn])
pacman -S kernel26
echo
echo "Now please reboot and re-run this script. Exiting... "
exit
;;
*)
break
;;
esac
#Install yaourt
echo
echo -n "Install Yaourt? (REQUIRED to continue) (Y/n) "
read Keypress2
case "$Keypress2" in
[Nn])
echo
echo "This script cannot continue without Yaourt. Exiting... "
exit
;;
*)
echo
echo >> /etc/pacman.conf
echo "[archlinuxfr]" >> /etc/pacman.conf
echo "#yaourt repo" >> /etc/pacman.conf
echo "Server = http://repo.archlinux.fr/i686" >> /etc/pacman.conf
;;
esac
echo
echo -n "Verify the right archlinux.fr repo was added? (Y/n) "
read Keypress3
case "$Keypress3" in
[Nn])
break
;;
*)
nano /etc/pacman.conf
echo ; clear
;;
esac
echo
echo "Continuing to install Yaourt... "
echo
pacman -Sy
pacman -S yaourt
#Install powerpill
echo "Powerpill is a wrapper for Pacman that increases the speed "
echo "of package downloads by using aria2c as a download accelerator "
echo "and is REQUIRED for this script to continue. "
echo
echo -n "Install Powerpill? (Y/n) "
read Keypress4
case "$Keypress4" in
[Nn])
echo
echo "This script cannot continue without Powerpill. Exiting... "
exit
;;
*)
yaourt -S powerpill
;;
esac
#Update packages installed from cd
echo
echo -n "Update ALL installed packages? (Y/n) "
read Keypress4a
case "$Keypress4a" in
[Nn])
break
;;
*)
powerpill -Syu
;;
esac
#Install user defined programs
echo
echo -n "Install extra programs? (Y/n) "
read Keypress5
case "$Keypress5" in
[Nn])
break
;;
*)
pacman -S xorg
powerpill -S openbox obconf obmenu firefox gstreamer0.10-ffmpeg 0.10.4-1 flashplugin gstreamer0.10-ffmpeg gstreamer0.10-ugly-plugins medit thunar sakura xmms meld gimp gpicview feh pidgin xmms vlc abiword galculator gnumeric xpdf obconf aspell-en conky gpicview scrot openbox-themes
yaourt -S phatch
;;
esac
#Install extra fonts
echo
echo -n "Install additional fonts? (Y/n) "
read Keypress6
case "$Keypress6" in
[Nn])
break
;;
*)
powerpill -S ttf-bitstream-vera ttf-dejavu ttf-ms-fonts
;;
esac
echo
echo "All programs and packages are updated and installed. "
#Import config files
echo
echo -n "Import configuration file backups? (Y/n) "
read Keypress7
case "$Keypress7" in
[Nn])
echo
echo "This script is complete. Exiting... "
exit
;;
*)
ping 192.168.1.200 -c 1 > /dev/null #2>&1
if [ $? -eq 0 ] ; then
echo
echo "Connecting to server... "
else
echo
echo "The server was not found at 192.168.1.200. "
echo "Please re-run script when server is connected "
echo "or set configurations manually. Exiting... "
exit
fi
esac
echo "Downloading ArchBoxBak.tar.gz... "
scp casey@192.168.1.200:ArchBoxBak.tar.gz /
echo
echo -n "Install config files automatically? (Y/n) "
read Keypress8
case "$Keypress8" in
[Nn])
echo
echo "This script is complete. Exiting... "
exit
;;
*)
echo "Extracting ArchBoxBak.tar.gz... "
tar -xzf /ArchBoxBak.tar.gz
;;
esac
echo
echo "Please verify the files were extracted correctly. "
echo "Use <ctrl><alt><f2-f7> to open a new console. "
echo "Press any key to continue. "
read Keypress9
case "$Keypress9" in
*)
break
;;
esac
echo
echo -n "Remove ArchBoxBak.tar.gz? (Y/n)"
read Keypress10
case "$Keypress10" in
[Nn])
break
;;
*)
rm /ArchBoxBaks.tar.gz
;;
esac
echo
echo "This install is complete."
exit
Please, if you feel so inclined, take a look and give some feedback. Both scripts are working but I do have an appreciation for doing things the "right" way, so if you see something that could be improved let me know. I think the next step will be to ask for inputs for file name, location, things like that so that it will be usable for anyone.
-monstermudder78
Offline
Looks pretty good to me. I would recommend looking at some basic option parsing and error checking. Google for TLDP's advanced bash scripting guide.
[git] | [AURpkgs] | [arch-games]
Offline
Can anyone tell me where I've gone wrong? I'm trying to backup a few files to my usbkey. Thank you.
backup.sh
#!/bin/bash
tar -czf /home/joe/ArchBoxBak.tar.gz /home/joe/.conkyrc /home/joe/.archnotify.py /home/joe/.xinitrc
home/joe/scripts /home/joe/.config/openbox /home/joe/.Xdefaults
cp /home/joe/ArchBoxBak.tar.gz /mnt/usbkey
rm /home/joe/ArchBoxBak.tar.gz
[joe@jdesk scripts]$ sudo ./backup.sh
tar: Removing leading `/' from member names
[joe@jdesk scripts]$ cd /mnt/usbkey
[joe@jdesk usbkey]$ ls
ArchBoxBak.tar.gz
[joe@jdesk usbkey]$ tar -xvzf ArchBoxBak.tar.gz
home/joe/.conkyrc
tar: home/joe/.conkyrc: Cannot open: No such file or directory
home/joe/.archnotify.py
tar: home/joe/.archnotify.py: Cannot open: No such file or directory
home/joe/.xinitrc
tar: home/joe/.xinitrc: Cannot open: No such file or directory
home/joe/scripts/
tar: home/joe/scripts: Cannot mkdir: No such file or directory
home/joe/scripts/backup.sh
tar: home/joe/scripts/backup.sh: Cannot open: No such file or directory
home/joe/scripts/backup.sh~
tar: home/joe/scripts/backup.sh~: Cannot open: No such file or directory
home/joe/.config/openbox/
tar: home/joe/.config/openbox: Cannot mkdir: No such file or directory
home/joe/.config/openbox/autostart.sh~
tar: home/joe/.config/openbox/autostart.sh~: Cannot open: No such file or directory
home/joe/.config/openbox/.Xdefaults
tar: home/joe/.config/openbox/.Xdefaults: Cannot open: No such file or directory
home/joe/.config/openbox/rc.xml
tar: home/joe/.config/openbox/rc.xml: Cannot open: No such file or directory
home/joe/.config/openbox/menu.xml
tar: home/joe/.config/openbox/menu.xml: Cannot open: No such file or directory
home/joe/.config/openbox/autostart.sh
tar: home/joe/.config/openbox/autostart.sh: Cannot open: No such file or directory
home/joe/.Xdefaults
tar: home/joe/.Xdefaults: Cannot open: No such file or directory
tar: Error exit delayed from previous errors
[joe@jdesk usbkey]$
EDIT: Yes, I had my key mounted improperly. Working fine now, thanks.
Last edited by jcs (2008-11-12 02:20:58)
Offline
just a wild guess... does user has write permission to the disk?
Offline
Make the backup script shorter, faster and save having the temporary holding file...
#!/bin/bash
tar cvzf - /home/casey/.bashrc /home/casey/.conkyrc /home/casey/.xinitrc /home/casey/scripts /home/casey/.keytouch2 /home/casey/.config | ssh casey@192.168.1.200 'cat - > /home/casey/ArchBoxBak.tar.gz'
Just a note - in your original script, you're calling tar with the cvf options, (ie, without the z option), but you're naming the file with a .gz extension. Either use the 'z' option to compress it, or just name it .tar extension
Also, IMHO, it's good to use variables and absolute paths. Personally, I'd expand the script to something like this:
#!/bin/bash
USERNAME='casey'
SERVER='192.168.1.200'
SERVER_FILE='/home/casey/ArchBoxBak.tar.gz'
TAR='/bin/tar'
SSH='/usr/bin/ssh'
$TAR cvzf - /home/casey/.bashrc /home/casey/.conkyrc /home/casey/.xinitrc /home/casey/scripts /home/casey/.keytouch2 /home/casey/.config | $SSH ${USERNAME}@${SERVER} "cat - > ${SERVER_FILE}"
Doesn't really offer any great advantages in this instance (unless the PATH var isn't set properly at run time), but if you expanded the script out to do multiple things with the file on the server (chmod for example), then using the variables means you only have to update it in one place if you want to change the filename in the future.
Overall, pretty good for a first shot
Last edited by fukawi2 (2008-11-11 22:08:05)
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Make the backup script shorter, faster and save having the temporary holding file...
#!/bin/bash tar cvzf - /home/casey/.bashrc /home/casey/.conkyrc /home/casey/.xinitrc /home/casey/scripts /home/casey/.keytouch2 /home/casey/.config | ssh casey@192.168.1.200 'cat - > /home/casey/ArchBoxBak.tar.gz'
Just a note - in your original script, you're calling tar with the cvf options, (ie, without the z option), but you're naming the file with a .gz extension. Either use the 'z' option to compress it, or just name it .tar extension
I think either I am not understanding something or you might have missed it, but in my post I think I am using 'z':
#!/bin/bash
tar cvzf - /home/casey/.bashrc
Also, IMHO, it's good to use variables and absolute paths. Personally, I'd expand the script to something like this:
#!/bin/bash USERNAME='casey' SERVER='192.168.1.200' SERVER_FILE='/home/casey/ArchBoxBak.tar.gz' TAR='/bin/tar' SSH='/usr/bin/ssh' $TAR cvzf - /home/casey/.bashrc /home/casey/.conkyrc /home/casey/.xinitrc /home/casey/scripts /home/casey/.keytouch2 /home/casey/.config | $SSH ${USERNAME}@${SERVER} "cat - > ${SERVER_FILE}"
Doesn't really offer any great advantages in this instance (unless the PATH var isn't set properly at run time), but if you expanded the script out to do multiple things with the file on the server (chmod for example), then using the variables means you only have to update it in one place if you want to change the filename in the future.
Thanks for the pointer. This was an idea that crossed my mind but I didn't know how to do it easily and then I moved on.
Overall, pretty good for a first shot
Thanks for taking the time to look it over
Offline
I think either I am not understanding something or you might have missed it, but in my post I think I am using 'z':
My bad. I don't know where I go confused. I'm putting it down to early morning coding
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
REALLY GREAT thread that I have ever seen for Im a newbie in bash scripting!
Awesome!
Offline