-1

I'm currently setting up a Linux machine that needs to sync files to a Google Drive. I use Grive to achieve this.

I currently found 2 bash scripts which already do almost but not quite what I want.

Files

#!/bin/bash
# Config BEGIN
# =====================================================================

# Directory to backup
BACKUPDIRFILES=/path/to/backup

# Google Drive directory
GRIVEROOT=/mnt/GoogleDrive

# Directory target in remote
TARGETDIR=backup

# =====================================================================
# Config END

# Create backup dir if not exists
echo Creating ${GRIVEROOT}/${TARGETDIR} if needed
    if [ ! -d "${GRIVEROOT}/${TARGETDIR}" ]; then 
        mkdir ${GRIVEROOT}/${TARGETDIR};
    fi

# Moving to Gdrive Dir
echo Entering ${GRIVEROOT}
    cd ${GRIVEROOT}

# Initial sync
echo Initial Google Drive Sync
    grive

# Coping new content
echo Copying from ${BACKUPDIRFILES}/* to ${GRIVEROOT}/${TARGETDIR}/
    cp -R ${BACKUPDIRFILES}/* ${GRIVEROOT}/${TARGETDIR}/

# Showing files copied
echo Files to sync
    find ${GRIVEROOT}/${TARGETDIR}/

# Final sync
echo Final Google Drive Sync
    grive

SQL

#!/bin/bash

USER=`cat /etc/mysql/debian.cnf | grep -m1 user | awk '{print $3}'`
PASS=`cat /etc/mysql/debian.cnf | grep -m1 pass | awk '{print $3}'`

SYSNAME=`hostname`
GRIVEROOT="/mnt/GoogleDrive"
BACKUPROOT="$GRIVEROOT/backuptest/$SYSNAME/mysql"
TODAY=$(date +"%d")
BACKUPDIR="$BACKUPROOT/$TODAY"
PASSWDFILE="/path/to/password/file"

GRIVEENABLED=1
LOGENABLED=1

function log_message {
        if [ "$LOGENABLED" -ne 0 ]; then
                echo $1
        fi
}

DATABASES=`mysql -u $USER -p$PASS -Bse 'show databases'`

for DB in $DATABASES; do

    # skip system tables
    if [ "$DB" == "information_schema" -o "$DB" == "performance_schema" -o "$DB" == "mysql" ]; then
        log_message "skipping system table $db"
        continue
    fi

    # create backup dir
    if [ ! -d $BACKUPDIR ]; then
        log_message "creating backup dir $BACKUPDIR"
        mkdir -p $BACKUPDIR
    fi

    # dump and compress SQL database
    SQLFILE="$BACKUPDIR/$DB.gz"
    ENCFILE="$SQLFILE.enc"
    log_message "dumping and compressing database $db"
    mysqldump -u $USER -p$PASS $DB | gzip -9 > $SQLFILE

    # encrypt SQL dump
    log_message "encrypting $SQLFILE"
    openssl des3 -in $SQLFILE -out $ENCFILE -pass file:$PASSWDFILE
    # to decrypt, use the following:
    # echo "openssl des3 -d -in $ENCFILE -out $SQLFILE -pass file:$PASSWDFILE"

    # delete unencrypted dump
    log_message "encryption finished; removing original dump"
    rm $SQLFILE
done

if [ "$GRIVEENABLED" -ne 0 ]; then
        log_message "   encryptions finished; starting Google Drive sync"
        cd $GRIVEROOT
        grive
fi

What I would want it to do:

Compress all the files into a password protected zip file named "Files - hostname- dd/mm/yyyy"and sync that file.

Then make a dump of the SQL database, compress it to a password protected zip file named "SQL - hostname - dd/mm/yyyy", delete the original SQL dump and sync.

If someone could help me out with this I would really appreciate it.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115

1 Answers1

0

After some more fiddling i got it to work.

#!/bin/bash
LOGENABLED=1
function log_message {
if [ "$LOGENABLED" -ne 0 ]; then
      echo $1
fi
}
    SYSNAME=`hostname`
    USER=`cat /etc/mysql/debian.cnf | grep -m1 user | awk '{print $3}'`
    PASS=`cat /etc/mysql/debian.cnf | grep -m1 pass | awk '{print $3}'`
    TODAY=$(date +%d-%m-%Y)
    SQLNAME="${SYSNAME}_SQL_${TODAY}.zip"
    FILENAME="${SYSNAME}_files_$TODAY.zip"
    HOME="/home/techie"
    WWW="/usr/share/nginx/www"
    BOTS="/opt/bin/"
    GRIVEROOT="/mnt/GoogleDrive"
    LOCAL="/mnt/Local"
    GRIVEBACKUP="$GRIVEROOT/${SYSNAME}_backup"
    PASSWORD=password
    EMAIL="email"
GRIVEENABLED=1

log_message "#####################################"
log_message "#REMOVING BACKUPS OLDER THEN 14 DAYS#"
log_message "#####################################"

find $GRIVEBACKUP/* -mtime +14 -exec rm {} \; 

cd $LOCAL

log_message "############"
log_message "#SQL BACKUP#"
log_message "############"

DATABASES=`mysql -u $USER -p$PASS -Bse 'show databases'`
   for DB in $DATABASES; do

        if [ "$DB" == "information_schema" -o "$DB" == "performance_schema" -o "$DB" == "mysql" ]; then
            continue
        fi

        SQLFILE="$DB"_$TODAY.sql
log_message "Dumping and Compressing $DB"
        mysqldump -u $USER -p$PASS $DB > $SQLFILE
    zip -rP $PASSWORD $SQLNAME $SQLFILE >/dev/null
log_message "Compression finished; removing original $DB.sql"
    rm $SQLFILE
    done

log_message "#############"
log_message "#FILE BACKUP#"
log_message "#############"

log_message "Compressing $FILES to $FILENAME"
    zip -rP $PASSWORD $FILENAME $WWW $BOTS $HOME -x "/home/techie/.rvm/*" >/dev/null
log_message "Moving $LOCAL/$FILENAME to $GRIVEBACKUP/$FILENAME"
    mv $LOCAL/$FILENAME $GRIVEBACKUP/$FILENAME
log_message "Moving $LOCAL/$SQLNAME to $GRIVEBACKUP/$SQLNAME"
    mv $LOCAL/$SQLNAME $GRIVEBACKUP/$SQLNAME


    if [ "$GRIVEENABLED" -ne 0 ]; then
     log_message "###############"
     log_message "#SYNC TO DRIVE#"
     log_message "###############"
            cd $GRIVEROOT
            grive
    fi

log_message "################"
log_message "#SENDING E-MAIL#"
log_message "################"
echo "Backup from $TODAY has run" | mail -s "$SYSNAME Google Drive Backup" $EMAIL