FAQ  •  Register  • 
  • Login
  • Linux Scripting

    <<

    Knightwolf654

    User avatar

    Respected Member
    Respected Member

    Posts: 508

    Joined: Sat Feb 5, 2011

    Division: Battlefield 4

    BF3 Soldier: knightwolf654

    BF4 Soldier: knightwolf654

    Reputation: 21

    Post Wed Oct 16, 2013 2:17 am

    Linux Scripting

    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.
    • 1

    i7 3930k @ 4.2 Ghz
    Gigabyte R9 Fury X :icon_mrgreen:
    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
    <<

    λndersony †

    User avatar

    Respected Member
    Respected Member

    Posts: 310

    Joined: Fri Sep 24, 2010

    Location: Alpine, WY

    Division: Half-Life 2 Deathmatch

    Steam Name: (TKC) λndersony †

    BF4 Soldier: Anders0ny

    Reputation: 25

    Post Wed Oct 16, 2013 5:28 am

    Re: Linux Scripting

    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 ....
    • 0

    .:- λndersony †
    .:- Division Leader of Flushed Items -:.


    AMD Ryzen 1700X (8-core@overclock 4.0Ghz)
    Asus ROG Crosshair 6 Hero
    Nvidia GeForce 1080
    G.Skill 16GB ddr4-3600Mhz
    Kingston HyperX M.2 Predator PCIe 240Gb SSD
    Corsair 1200i Watt PSU
    Corsair H100i water cooling
    Voodoo Blackbird Case
    <<

    Knightwolf654

    User avatar

    Respected Member
    Respected Member

    Posts: 508

    Joined: Sat Feb 5, 2011

    Division: Battlefield 4

    BF3 Soldier: knightwolf654

    BF4 Soldier: knightwolf654

    Reputation: 21

    Post Sun Oct 20, 2013 12:53 am

    Re: Linux Scripting

    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
    • 1

    i7 3930k @ 4.2 Ghz
    Gigabyte R9 Fury X :icon_mrgreen:
    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

    Return to PC Hardware

    Who is online

    Users browsing this forum: No registered users and 28 guests

    cron
    x

    #{title}

    #{text}