backup script

a simple backup script

the goal of this script is to create a local backup in a folder made for that and then upload it to an FTP server.. it is as simple script for a simple task and for me it works just fine.. . of course you can and should modify to suit your needs if necessary.

things you will need :

for this script to work "as is" you will need the following :

  • NCFTP : install ncftp and create a bookmark and save the password.
    IMPORTANT, this should be done with the user that will execute the backup script, in my case the user is root. .. .
  • bzip2 : this thing will be of use for compressing the archives, you can decide to not use it and just make regular ".tar.gz" files instead. . to do that change "tar jcfp" to "tar zcf" in the script

settings

this part should be simple, especially because the script is quite commented (in english yay) but i might be more detailed about some things here.. .

  • BACKUPROOT : this is where the backups will be stored. NOTE: in this folder the script will create a link called "current" that points to the latest backup.
  • DAYS : here you can set the number of days worth of backups to conserve (locally) anything older will be erased upon execution of the script.
  • BACKUPDIRS : this is the list of stuff to backup, you can just put the full path to each folder you want to backup separated by a space
  • NCFTPBOOKMARK : this should be the name of the NCFTP bookmark to use IMPORTANT : to tell ncftp what needs to be done, create a file /root/ftpcommands containing the commands NCFTP should execute, i put an example in the script of what i use. (in my case i connect to the FTP server, erase whatever is already there and then upload the new backup, if you have more space than lol for you :] )

usage

you should put the script in something like, /usr/local/sbin/backup.sh and then just simply execute the script. if you want you can tell crontab to do it for example like this (every day at 5 am) :

echo "0 5 * * * root /usr/local/sbin/backup.sh"

the script

the script is here below if you are the copy paste kind of person, if you are more into the wget mood you can get it from this link

#!/bin/bash # this script creates tar.bz2 archives and places them in a backup folder ordered by date # where the latest is linked to a folder called "current" # DEPENDS: bunzip2 ncftp (for ncftp create a bookmark with saved password) # TODO: - simplify ftp uploading # - do something about scp/rsync maybe # AUTHOR: Emmanuel REVAH manu-AT-manurevah.com http://manurevah.com/ # i'm not sure about a licence so whatever, just don't sue me.. . ... :] # \o/ logger "backup script: started" DATE=$(date +%a_%d_%b_%Y_%Hh%Mm%S) MONTH=$(date +%B_%Y) ########### configuration options ############## # where we keep all the backups BACKUPROOT="/home/backup/mybackup" BACKUP="${BACKUPROOT}/${MONTH}/backup_${DATE}/" # how many days worth of backups do we need to keep ? DAYS="5" # folders to backup: space separated values BACKUPDIRS="/etc /var/log" BACKUPDIRS=`ls -d ${BACKUPDIRS}` # ncftp (create a bookmark with saved password) # you will also need a file with ftp commands in /root/ftpcommands mine contains: # cd backup # rm -rf current # put -r /home/backup/current # quit NCFTPBOOKMARK="ftpbookmark" # either place a file containing ONLY the password in clear text (chmod 600 and chown root of course) # or just replace the password here MYSQL_USER_PASS="--user=root --password=`cat /root/.sqlpasswd`" ############ end of configuration ################ # cleanup up the "current" folder and link the new one rm -f ${BACKUPROOT}/current test -d ${BACKUP} || mkdir -p ${BACKUP}/mysql ln -s ${BACKUP} ${BACKUPROOT}/current # make some archive files for DIRS in ${BACKUPDIRS}; do DIRNAME=`echo ${DIRS}|sed 's/\//_/g'` tar jcfp ${BACKUP}/${DATE}${DIRNAME}.tar.bz2 ${DIRS}; done # mysql: please test this line to make sure it correctly lists the names of the databases MYBASES="`ls -1 /var/lib/mysql |grep -v ib_* |grep -v debian-5.0.flag | grep -v mysql_upgrade_info`" for DBS in ${MYBASES};do mysqldump --databases ${DBS} ${MYSQL_USER_PASS} > ${BACKUP}/${DATE}_${DBS}.sql done logger "backup script: local backup done :]" ncftp ${NCFTPBOOKMARK} < /root/ftpcommands logger "backup script: upload current backup to ${NCFTPBOOKMARK}" # erase older data find ${BACKUPROOT} -type f -mtime +${DAYS} -exec rm -rf {} \; rmdir -p ${BACKUPROOT}/*/*/* logger "backup script: removed backups older than ${DAYS} days" exit
comments:
avatar

Eric

Learned about 'logger' today. Thank you!!
Leave a comment
You may use the following HTML tags: <p> <a> <strong> <b> <em> <i> <cite> <blockquote> <code> <pre>

Your comments WILL NOT be submitted to any third party (not even for anti spam verification).