时间:2021-07-01 10:21:17 帮助过:18人阅读
#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by
# http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
##备份本件本地目录##
DIRS="/home/xxxx"
BACKUP=/tmp/backup.$$
##日期格式 可以自己调节##
NOW=$(date +"%Y-%m-%d")
INCFILE="/home/xxxx"
DAY=$(date +"%u")
##星期天完全备份 可以自己调节##
FULLBACKUP="7"
### MySQL Setup ###
MUSER="chaos"
MPASS="password"
MHOST="host"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
##增量备份路径##
FTPD="/backup/xxxx/incremental"
FTPU="chaos"
FTPP="password"
FTPS="xxx.xxx.com"
### Other stuff ###
EMAILID="chaos@diablo.net"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
##完全备份路径##
FTPD="/backup/xxxx/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
##备份所有数据库 可自行调整 或备份指定数据库##
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using lftp
##ubuntu上可以使用ncftp 自行调节##
#ncftp -u"$FTPU" -p"$FTPP" $FTPS<
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
rm -f $BACKUP/*
else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup failed" >>$T
mail -s "BACKUP FAILED" "$EMAILID" <$T
rm -f $T
fi
改一下里面的一些路径和账号密码等信息就可以了,希望对大家有所用处。