Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having one ubuntu local server in which we used to have all our development websites. They all are php based sites. I would like to know whether we can have script or something to cron backup the files and database daily to external harddisc ?

Please let me know.

share|improve this question

This is a little script I wrote to backup my apache-configs, webfiles & db to Amazon S3. The sole purpose of the server is a web server where every developer uses his own homedir. So, There is a file in `/home' with a list of users that need to be backed up.

#    2012 Bart De Vos
#    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# location of this script
DIRECTORY=$(cd `dirname $0` && pwd)

# error handling
function error_exit
    echo "Error on line ${1:-'Aborting script.'}" 1>&2
    exit 1

function backup
    # exit on errors
    set -e

    # getting hostname
    HOSTNAME_FQDN=`hostname --fqdn` || error_exit "${LINENO}: Error determening hostname"

    echo "Determening timestamp"
    timestampbackup=`/bin/date +%Y%m%d%H` || error_exit "${LINENO}: Error determening timestamp"

    echo "Create backupdir"
    /bin/mkdir /backup/${timestampbackup} || error_exit "${LINENO}: Error creating backupdir"

    echo "Dump and save MYSQL for all dbs"
    /usr/bin/mysqldump --all-databases --opt -c -e -Q -uScriptRunner -pMySuperSecretPasswordThaIAlmostForgotToRemoveFromTheScript | /bin/gzip > /backup/${timestampbackup}/sqlbackup.sql.gz || error_exit "${LINENO}: Error backing up mysql"

    echo "Itterate users in /home/.backup"
    for username in `awk -F: '{print $1}' /home/.backup`
        echo "Backing up user: $username"
        /bin/tar -czpf /backup/${timestampbackup}/${username}.tar.gz /home/${username} || error_exit "${LINENO}: Error backing up $username"

    echo "Get configs"
    /bin/tar -czpf /backup/${timestampbackup}/httpdconfigs.tar.gz /etc/httpd/conf.d/ || error_exit "${LINENO}: Error backing up httpd conf.d"

    echo "Copy to S3 bucket"
    /usr/bin/s3cmd --config=/backup/.s3cfg put /backup/ s3://${HOSTNAME_FQDN} --recursive || error_exit "${LINENO}: Error with transfer to S3"

    echo "Remove backupdir"
    /bin/rm /backup/${timestampbackup} -rf || error_exit "${LINENO}: Error deleting backupdir"

    echo "All done!"



Before you can use it, you need to install s3cmd and create a .config file with your S3 security credentials. (Will create this upon first use).

Don't forget to check/alter paths because they can differ between distro's/versions.

share|improve this answer
Thanks for the code. Can I have another remote server instead of Amazon S3 ? or, can I use dropbox ? – galtech Aug 1 '12 at 8:01
Sure, just change the s3cmd code with scp and you can push it to another server. DropBox is not really a professional backup solution imo... – Bart De Vos Aug 1 '12 at 8:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.