Add feature: Delete Googole Drive's remote file

This commit is contained in:
Teddysun 2016-09-08 13:31:09 +09:00 committed by GitHub
parent 42fc873ff5
commit aec772a574

View File

@ -41,6 +41,12 @@ MYSQL_DATABASE_NAME[0]=""
# if you want not to be backed up, leave it blank. # if you want not to be backed up, leave it blank.
BACKUP[0]="" BACKUP[0]=""
# Number of days to store daily local backups
LOCALAGEDAILIES="7"
# Delete Googole Drive's remote file flag(true:delete, false:not delete)
DELETE_REMOTE_FILE_FLG=true
# Date & Time # Date & Time
BACKUPDATE=$(date +%Y%m%d%H%M%S) BACKUPDATE=$(date +%Y%m%d%H%M%S)
@ -50,8 +56,6 @@ TARFILE="${LOCALDIR}""$(hostname)"_"${BACKUPDATE}".tgz
# Backup MySQL dump file name # Backup MySQL dump file name
SQLFILE="${TEMPDIR}mysql_${BACKUPDATE}.sql" SQLFILE="${TEMPDIR}mysql_${BACKUPDATE}.sql"
# Number of days to store daily local backups
LOCALAGEDAILIES="7"
### END OF CONFIG ### ### END OF CONFIG ###
@ -173,9 +177,11 @@ fi
# For i386: wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-386; chmod +x /usr/bin/gdrive # For i386: wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-386; chmod +x /usr/bin/gdrive
if [ ! "$(command -v "gdrive")" ]; then if [ ! "$(command -v "gdrive")" ]; then
GDRIVE_COMMAND=false
log "gdrive is not installed" log "gdrive is not installed"
log "File transfer skipped. please install it and try again" log "File transfer skipped. please install it and try again"
else else
GDRIVE_COMMAND=true
log "Tranferring backup file to Google Drive" log "Tranferring backup file to Google Drive"
if ${ENCRYPTFLG}; then if ${ENCRYPTFLG}; then
gdrive upload --no-progress ${TARFILE}.enc >> ${LOGFILE} gdrive upload --no-progress ${TARFILE}.enc >> ${LOGFILE}
@ -205,6 +211,17 @@ getFileDate() {
return 1 return 1
} }
deleteRemoteFile() {
local FILENAME=$1
if ${DELETE_REMOTE_FILE_FLG} && ${GDRIVE_COMMAND}; then
local FILEID=$(gdrive list -q "name = '${FILENAME}'" --no-header | awk '{print $1}')
if [ -n ${FILEID} ]; then
gdrive delete ${FILEID} >> ${LOGFILE}
log "Google Drive's old backup file name:${FILENAME} has been deleted"
fi
fi
}
AGEDAILIES=${LOCALAGEDAILIES} AGEDAILIES=${LOCALAGEDAILIES}
DAY=$(date +%d) DAY=$(date +%d)
MONTH=$(date +%m) MONTH=$(date +%m)
@ -227,6 +244,7 @@ do
if [[ ${FILEAGE} -gt ${AGEDAILIES} ]]; then if [[ ${FILEAGE} -gt ${AGEDAILIES} ]]; then
rm -f ${f} rm -f ${f}
log "Old backup file name:${f} has been deleted" log "Old backup file name:${f} has been deleted"
deleteRemoteFile ${f}
fi fi
fi fi
done done