
Respected Member
![]() |
![]() |
![]() |
![]() |
Posts: 508
Joined: Sat Feb 5, 2011
Division: Battlefield 4
BF3 Soldier: knightwolf654
BF4 Soldier: knightwolf654
Linux Scripting

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.
- 1
Gigabyte R9 Fury X

32gb ddr3-1866
3x 27" LG IPS displays + 1 23" IPS display
Samsung 850 pro 512GB
Samsung 830 pro 256GB
WD SE 2TB
1200 watt Corsair PSU
H100 water cooling
8TB FreeNas Server