This is based on a script I use for another Control Panel.
What this script does is backup everything in "/backup" folder (admin backups), and upload it to google drive in tar.gz format, this script will run after the CWP admin backup script has ran.
To use; get the gdrive binary for your OS (
https://github.com/gdrive-org/gdrive#downloads).
Centos 6/7 64bit:
wget https://drive.google.com/uc?id=1Ej8VgsW5RgK66Btb9p74tSdHMH3p4UNb&export=download
Centos 6 32-bit
wget https://drive.google.com/uc?id=1eo9hMXz0WyuBwRxPM0LrTtQmhTgOLUlg&export=download
Make it executable:
chmod +x gdrive
Move it to /usr/sbin
mv gdrive /usr/sbin/
Setup:
gdrive about
Follow the link and get the verification code by authenticating with the google account for the drive you want access to.
Add the script into /root directory named:
GDbackup.sh:
nano /root/GDbackup.sh
Copy and paste the below script into the new file using (CTRL C / CTRL V).
And then save the file:
CTRL + X + Y
Script:
#!/bin/bash
IFS=$'\n'
# Copy the folder you want to store your file in at Google Drive (eg. https://drive.google.com/.../folders/...).
DRIVEFOLDERID=
# Days in which to delete the old backups from google drive (set 0 to disable).
DELETEDAYS=180
echo "#############################################"
echo " ---Google Drive Backup--- "
echo "#############################################"
gdrive about
echo "#############################################"
DATE=$(date '+%d-%m-%Y')
TS=$(date +'%H%M%S')
tar -czvf /tmp/$HOSTNAME.$DATE.$TS.backup.tar.gz -C / backup > /dev/null
BAKFILE=/tmp/$HOSTNAME.$DATE.$TS.backup.tar.gz
if [ -e "$BAKFILE" ] ; then
if [ "$DELETEDAYS" > 0 ]; then
upTime=$(date --date="$DELETEDAYS days ago" '+%Y-%m-%dT%T')
gdrive list -q "name contains 'backup.tar.gz' and name contains '$HOSTNAME' and modifiedTime < '$upTime'" >> drive.txt
filename='drive.txt'
# Read the list file
while read -r line
do
theId=$(cut -c-2 <<< "$line")
#basically skip the first line...
if [ "$theId" != "Id" ]; then
echo "Deleting old backup file: "
gdrive delete $(cut -c-33 <<< "$line")
fi
done < $filename
# Remove temp list file
rm -rf drive.txt
fi
gdrive upload -p "$DRIVEFOLDERID" --delete "$BAKFILE"
# ensures removal of backup file from /tmp
rm -rf "$BAKFILE"
echo "Backup to Google Drive is complete..."
echo "#############################################"
fi
exit 0
Run this command to activate the automated cron job:
echo 'sh /root/GDbackup.sh' >> /etc/cron.daily/cwp
You will see the backup information in the email that CWP sends when the backup cron has ran, you can also run the script manually:
sh /root/GDbackup.sh
and enjoy your free space...! Hope this helps others..