#!/bin/bash ## RSFBS - Really Simple File Backup Script ## ## Version 0.5 ## ## Variables go here! # General base backup directory, All the subdirectories will be # created under here. basedir=/backup # Let's name our backup file, the date will be appended to this backupname=domain-files # Network Attached Storage Directory. This could be # anywhere, but we keep it under the basedir to keep Tengu sane. # If you don't have a NAS or NFS drive, leave this blank. nasdir=$basedir/nas # Target Directory to backup backupdir=/home/domain/public_html # How many backups do we want to keep? This should be in the format # days, weeks, months, etc. Example: keep="7 days" keep="7 days" # Archiver to use (bzip2, gzip or zip) archiver=bzip2 # Some stuff to properly name the files suffix=$(date +%F) olddir=$(date --date "$keep ago" +%F) ## Now we get into the meat of the program, You probably ## don't need to edit below this line unless you're changing ## something drastic. # Create the proper extension for our archiver if [ "$archiver" == "bzip2" ]; then archext=bz2 elif [ "$archiver" == "gzip" ]; then archext=gz elif [ "$archiver" == "zip" ]; then archext=zip fi ## Here we check to see if the NAS is mounted if NAS is enabled. if [ "$nasdir" != "" ]; then checkmount=$(mount | awk -v mnt=$nasdir '{ if ($3 == mnt) print $0 }') ## NAS isn't mounted! Mount it, but good god, don't fsck it! if [ "$checkmount" == "" ]; then echo "NAS not mounted, mounting..." mount $nasdir else echo "NAS Found..." fi fi ## let's create the directories we'll need if they don't already exist. # First check to see if the $basedir exists. if [ ! -d $basedir ]; then echo "ERROR! BASE DIRECTORY DOES NOT EXIST!" exit 1; fi # The work directory if [ ! -d $basedir/work ]; then echo "Creating $basedir/work..." mkdir $basedir/work fi # The local backup directory if [ ! -d $basedir/local ]; then echo "Creating $basedir/local..." mkdir $basedir/local fi # and the NAS file directory if [ ! -d $nasdir/files ]; then echo "Creating $nasdir/files..." mkdir $nasdir/files fi ## Ok, now lets start copying, eh? Prepare for some hot disk I/O Action! # We copy with nice because it's nice! nice -n 19 cp -Rf $backupdir $basedir/work echo "Copying files to work directory..." # Oh hey, the work directory is full of what we need to back up, isn't # it? cd $basedir/work # let's start compressing, nicely! if [ "$archiver" == "zip" ]; then echo "zipping files..." nice -n 19 zip $backupname-$suffix.zip * else echo "Tarring files..." nice -n 19 tar -cf $backupname-$suffix.tar * archext="tar.$archext" echo "Compressing files with $archiver..." nice -n 19 $archiver -9f $backupname-$suffix.tar fi # Now that that's over with, let's move the tarball to a local storage # folder mv $backupname-$suffix.tar.$arcext $basedir/local ## and then, finally, we'll copy it to the NAS. echo "Copying tarball to NAS..." cp $basedir/local/$backupname-$suffix.tar.$archext $nasdir/files ## Delete the old stuff! if [ -d $basedir/local/$backupname-$olddir.$archext ]; then echo "Deleting old data..." rm -rf $basedir/local/$backupname-$olddir.$arcext fi if [ -d $nasdir/files/$backupname-$olddir.$arcext ]; then echo "Deleting more old data..." rm -rf $nasdir/files/$backupname-$olddir.$arcext fi ## Oh hay! We're all done! Let's clean up and exit gracefully! rm -rf $basedir/work echo "Done!" exit 0;
Backup script
Leave a Comment
Previous Post
« backup server configuration-Rsync setup Next Post
Rsync backup script »
« backup server configuration-Rsync setup Next Post
Rsync backup script »