在 Ubuntu 服务器中备份带有数据库的网站

在 Ubuntu 服务器中备份带有数据库的网站

我有一台 ubuntu 本地服务器,我们以前所有的开发网站都放在这个服务器上。它们都是基于 php 的网站。我想知道我们是否可以有脚本或其他东西来每天将文件和数据库备份到外部硬盘上?

请告诉我。

答案1

这是我编写的一个小脚本,用于将我的 apache-configs、webfiles 和 db 备份到 Amazon S3。该服务器的唯一用途是作为 Web 服务器,每个开发人员都使用自己的主目录。因此,在“/home”中有一个文件,其中包含需要备份的用户列表。

#!/bin/bash
#    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)
INSTALLER_FILENAME=`basename $0`

# 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`
    do
        echo "Backing up user: $username"
        /bin/tar -czpf /backup/${timestampbackup}/${username}.tar.gz /home/${username} || error_exit "${LINENO}: Error backing up $username"
    done    

    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!"

}

backup

在使用它之前,您需要安装 s3cmd 并.config使用您的 S3 安全凭证创建一个文件。(首次使用时将创建此文件)。

不要忘记检查/修改路径,因为它们在不同的发行版/版本之间可能有所不同。

相关内容