Page 1 of 1

Linux Scripting

PostPosted: Wed Oct 16, 2013 2:17 am
by Knightwolf654
here are a few backup scripts i have written if you guys are interested. i'm looking to see what you guys think (if you can understand them) or if you interested in learning. they are quite simplistic then some i have seen but they get the job done the way i want them too :)
opinions and questions are welcome!

This first one is to automate a web server backup, although now that i look at it i did not do it as secured as i would like. everything references a Environment variable to make it easy to add your settings without having to change the actual code.
this script also creates a log file of what it did. so say someone managed to hack and install malware into the default html directory, if you check the log it will tell you what files changed since the last backup which makes removing those pesky things easier.

  Code:
#!/bin/bash
# WebServer backup script V1.0
#script written by Knightwolf654
#script assumes that /backup/webbackup/www directory already exist. the mysql directory
#is created and removed by this script because i don't like keeping database dumps on the server.
# **REQUIRES ncftp TO RUN!!**
#this script is designed to be used in crontab, you should also store it in the /root directory for security reasons
#I <3 comments
#moo

LOG=/backup/backupreport.log

#Output time of backup start for logging
echo Time backup started = $(date +%T) > $LOG
before="$(date +%s)"

#create temporary directory's
mkdir /backup/webbackup/mysql
mkdir /tmp/webbackup

#ENVIRONMENT VARIABLES
WWWBACKUP=/backup/webbackup/www
MYSQLBACKUP=/backup/webbackup/mysql
WWW=/var/www
BACKUP=/backup/webbackup
FTPS=[server ip]
FTPD=[ftp directory]
FTPU=[ftp username]
FTPP=[ftp password]
MYSQLU=[mysql username]
MYSQLP=[mysql password]
DB1=[name]
#DB2=[name]
#DB3=[name]


#rsync web files to backup directory
rsync -av --delete $WWW $WWWBACKUP >> $LOG

#mysql database dumps to backup directory
#duplicate below command when backing up multiple databases if you want to keep them seperate

mysqldump -u $MYSQLU -p$MYSQLP --databases $DB1 > $MYSQLBACKUP/[databasename]_`date '+%m-%d-%Y'`.sql
#mysqldump -u $MYSQLU -p$MYSQLP --databases $DB2 > $MYSQLBACKUP/[databasename]_`date '+%m-%d-%Y'`.sql
#mysqldump -u $MYSQLU -p$MYSQLP --databases $DB3 > $MYSQLBACKUP/[databasename]_`date '+%m-%d-%Y'`.sql

#tar backupfiles folder, this command will also add the date of the backup
tar -zcvf /tmp/webbackup/webbackup_`date '+%m-%d-%Y'`.tar.gz $BACKUP

#transfer backup file to external ftp server
ncftpput -u $FTPU -p $FTPP $FTPS $FTPD /tmp/webbackup/*.tar.gz

#removes temporary directory's
rm -rf /tmp/webbackup
rm -rf /backup/webbackup/mysql

#output time backup completed
echo Time backup finished = $(date +%T) >> $LOG

#calculates time taken to complete and outputs
after="$(date +%s)"
elapsed="$(expr $after - $before)"
hours=$(($elapsed / 3600))
elapsed=$(($elapsed - $hours * 3600))
minutes=$(($elapsed / 60))
seconds=$(($elapsed - $minutes * 60))
echo Time taken: "$hours hours $minutes minutes $seconds seconds" >> $LOG

#end


this one i have not tested and have no idea if it even remotely functions, i meant to put it into place but instead took my web-server offline and turned it into a HTPC. i have two more i am working on. the first one is meant to go along side the one above and create a log file with a bunch of useful information about the server and updates and even imports the log created by the above script and email's it. the second one i have which i now have working and in place sync's a network share to a backup driver over the network. those i will try and get up later this week.

Re: Linux Scripting

PostPosted: Wed Oct 16, 2013 5:28 am
by λndersony †
Nice... I'll keep that handy, never know when you might need one :icon_cool:

about the only time I get excited about scripts was bash'n a couple to manipulate cube rotation, automate arrow movement and selection, and cause some special effects on our wall displays that monitor circuits (with Compi)z. I have to say, it was pretty neat to watch -- and it did impress upper management, got me some points there ... Thanks Linux :icon_twisted:

Gave ya a point -- anything Linux gets a point from me :icon_gotcha: Now if I could just automate that in these forums ....

Re: Linux Scripting

PostPosted: Sun Oct 20, 2013 12:53 am
by Knightwolf654
dude :icon_eek:
you win lol! although i have been using linux for 3-4 years now and actually have it on my main desktop at work, i have yet to do anything that complex. I still role in the minor leagues only have 3 servers running linux at work but i'm pushing to move more :) . here is another script i put together, i am working on a whole how-to for setting a network share backup up, but i have a few other things going on and i needed to get my NAS backing up quickly (its starting to act funky). so for me i now have my desktop backing up to my Freenas storage server which has a pool of 4- 2TB disks in Raidz, which now backs up to my HTPC which just has a single 3TB WD red drive.

Image

here is the script, i used the same concept from before and to keep you from having to change the code itself i used Environmental Variables. the script syncs two directory's (one being your storage drive and the other being your network share) it creates a log file of the changes and how long the backup takes to complete and then email's it to you. now their is a lot more too this then just running the script, you need to mount you backup drive and shares to directory's which is why i was doing the how-to but i will have that up at a later date when i get some time.

  Code:
#!/bin/bash
#Rsync script FreeNAS to Ubuntu
#requires rsync and mailutils
#by: knightwolf654

#Environment Variables
LOG=[LOG Location]/Backuplog_`date '+%m-%d-%Y'`.txt
SOR=[source directory]
DES=[destination directory]
EMAIL=[email address you want logfile to go to]


#Output time of backup start for logging
echo Time backup started = $(date +%T) > $LOG
before="$(date +%s)"

# Sync shares to backup drive
echo -e "\n**Start of blank share backup:\n" >> $LOG
rsync -av -delete $SOR $DES >> $LOG


#output time backup completed
echo Time backup finished = $(date +%T) >> $LOG

#calculates time taken to complete and outputs
after="$(date +%s)"
elapsed="$(expr $after - $before)"
hours=$(($elapsed / 3600))
elapsed=$(($elapsed - $hours * 3600))
minutes=$(($elapsed / 60))
seconds=$(($elapsed - $minutes * 60))
echo Time taken: $hours hours $minutes minutes $seconds seconds >> $LOG
echo "END"

#email log file
mail -s "Backup Log" $EMAIL < $LOG

#End of script