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.