Linux скрипты: backup чего-нибудь

to Google Drive

Big thanks to Petter Rasmussen !
https://github.com/prasmussen/gdrive

# скачиваем актуальную версию на странице https://github.com/prasmussen/gdrive
wget -O gdrive 
#
mv gdrive /usr/sbin/gdrive  
chmod 755 /usr/sbin/gdrive
# увидим запрос авторизации
gdrive
# Заходим на URL в нужном профиле Google
#
# для использования настроенной авторизации root-а в cron-е нужно четко указывать каталог конфигов
gdrive -c /root/.gdrive/ upload /locat/path/file 

UPD: 2017-02-23: скорость UPLOAD – 3 МБайта/сек

UPD: 2017-10-09: Uploaded at 7.6 MB/s

Вспомогательные функции

UPD: 2017-10-09 – gdrive v1.8.0 – изменены команды (about->quota, опции -i)

# /etc/backup/function.sh
log() {  
    echo `date +"%Y-%m-%d %H:%M:%S"` [email protected] >> ${Log}
} 
# set Arch Suffix
setArchSuffix() {
ArchFile="${Project}_$1.${ArchExt}"
ArchPathFile=${TmpPath}/${ArchFile}
}
#
rmfile() {
if [ -f $1 ]
then
    log "Remove file:" `ls -s -h $1`
    rm -f $1
else 
    log "ERROR: Remove failed. File not exist: " $1
fi
}
#
showfile() {
if [ -f $1 ]
then
    log "File :" `ls -s -h $1`
else 
    log "ERROR: File not exist: " $1
fi
}
#
gdriveinfo() {
    log "Google Drive status:" `gdrive -c /root/.gdrive/ quota | grep -i "free\|used\|total"` 
}
# 
up2gdrive() {
file=$1
if [ -f $file ]
then
    log "Try uploading file to Google Drive :" `ls -s -h $file`
    UploadSize=`ls -l $file | awk '{print $5}'`
    FreeGD=`gdrive -c /root/.gdrive/ quota --bytes | grep Free | awk '{print $2}'`
    if [ ${FreeGD} -gt ${UploadSize} ]
    then
	# Get previuos file ID 
	PrevID=`gdrive -c /root/.gdrive/ list | grep "${file##*/}" | head -1 | awk '{print $1}'`
	log 'GD Looking for previous verion:' ${file##*/} "  Found ID: " ${PrevID}
	log 'GD Upload start'
        gdrive -c /root/.gdrive/ upload $file
        log "GD Upload finished successfuly"
        if [ -n "${PrevID}" ]
        then
    	    log "GD Deleting previous file. FileId : " ${PrevID}
    	    gdrive -c /root/.gdrive/ delete -i ${PrevID}
        fi
    else
	log "GD Upload failed: No enougth free space on GoogleDrive. " `gdrive -c /root/.gdrive/ quota| grep Free`
    fi
    rmfile $file
else
    log "GD Upload failed: file does not exist: " $file
fi
}
#

Бэкапируем что-нибудь не большое, одна копия, перезаписывает себя

#!/bin/sh
#------------------
# Project name
Project="domain.com"
ArchDate=`date "+%Y-%m-%d"`
TmpPath="/var/tmp"
ArchPath="/backup"
MysqlDump="${TmpPath}/mysqldump_${Project}_${ArchDate}.sql"
Log="/var/log/backup/${Project}.log"
ZipCommand="zip -r -q -9"
ArchExt="zip"
DBname1="mysqldbname"
ArchFile="${Project}.${ArchExt}"
ArchPathFile=${ArchPath}/${ArchFile}
#========================
. /etc/backup/function.sh
#========================
log "START backup " ${Project}
rmfile ${ArchPathFile}
#
log "dump MySQL databases" ${DBname1}
/usr/bin/mysqldump -h localhost -u USER -pPASSWORD --databases ${DBname1} > ${MysqlDump}
showfile ${MysqlDump}
#-----------------
log "start make archive"
${ZipCommand} ${ArchPathFile} \
${MysqlDump} \
/var/www/domain/ \
/etc/nginx \
/etc/mysql \
/etc/cron.d/backup_www \
/etc/backup/${Project}  \
/etc/backup/function.sh
# ----------
showfile ${ArchPathFile}
#
rmfile ${MysqlDump}
log "END"
# Report to mail
grep $ArchDate $Log | mail -s "Backup Report" root