Skip to content

Instantly share code, notes, and snippets.

@Silvenga
Last active February 25, 2023 10:45
Show Gist options
  • Save Silvenga/6835237 to your computer and use it in GitHub Desktop.
Save Silvenga/6835237 to your computer and use it in GitHub Desktop.
For daily systemwide encrypted backups of Ubuntu servers to remote host of Google Drive (free 15GB of storage).
#!/bin/bash
# Place in /usr/share/backup/
# and make executable
# chmod 0744 dup-backup.sh
# install:
# apt-get install duplicity python-gdata python-gobject-2 python-paramiko
## Remeber to change Google drive user name and Google drive folder
## And change Email
# Must run as root for system wide backups
# su -u root
# For cron job:
#su -l root # login to root to edit root's cron jobs
#crontab -e
#0 1 * * * /usr/share/backup/dup-backup.sh # will run at 00:00 every day
# GPG Password
# Use for encrypting all data on remote storing host
# No spaces
export PASSPHRASE=very hard password
# GDrive Password
# Password for logging into Google drive
# No spaces
export FTP_PASSWORD=password
echo "Started At: `date`." >>/var/log/duplicity/backup.log
echo "Starting Backup." >>/var/log/duplicity/backup.log
echo "" >>/var/log/duplicity/backup.log
# Make exceptions to back up. Add anything you want.
# Made with the assumptions that we care about only the data
# that we can restore on any machine running the current OS
#
# "- /file/path"
# Add more if errors out
cat > /usr/share/backup/filelist.txt <<EOF
- /dev
- /proc
- /lost+found
- /media
- /mnt
- /run
- /tmp
- /boot
- /selinux
- /root/.cache
- /sys
EOF
echo "Dumping databases..." >>$LOGLOC
USER="root"
PASSWORD="password"
OUTPUTDIR="/var/db-backups"
mkdir -p $OUTPUTDIR
rm "$OUTPUTDIR/*bak" > /dev/null 2>&1
databases=`mysql --user=$USER --password=$PASSWORD \
-e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
echo "Dumping databse: $db">>$LOGLOC
mysqldump --force --opt --user=$USER --password=$PASSWORD \
--databases $db 1> "$OUTPUTDIR/$db.bak" 2>NUL
done
echo "Done." >>$LOGLOC
echo "" >>$LOGLOC
# Loading the day of the month
date=`date +%d`
# Check to see if we're at the first of the month.
# If so then run a full backup.
# If not, then run an incremental backup
# duplicity with blank options will run incremental, if fails, then run full
# using duplicity full to force full backup
if [ $date = 01 ]
then
echo "Doing Full Backup..." >>/var/log/duplicity/backup.log
duplicity full --exclude-filelist /usr/share/backup/filelist.txt / gdocs://username/folder >>/var/log/duplicity/backup.log
else
echo "Doing Incremental Backup..." >>/var/log/duplicity/backup.log
duplicity --exclude-filelist /usr/share/backup/filelist.txt / gdocs://username/folder >>/var/log/duplicity/backup.log
fi
echo "Done." >>/var/log/duplicity/backup.log
echo "" >>/var/log/duplicity/backup.log
# Delete old backups
# One full backup each month means will remove backups
# older than 3 months
echo "Cleaning Backups..." >>/var/log/duplicity/backup.log
duplicity remove-all-but-n-full 3 --force gdocs://[email protected]/vpn01 >>/var/log/duplicity/backup.log
echo "Done." >>/var/log/duplicity/backup.log
echo "" >>/var/log/duplicity/backup.log
# Destroying passwords saved to memory
unset PASSPHRASE
unset FTP_PASSWORD
# Email log
echo "Backup Finished, Emailing Log..." >>/var/log/duplicity/backup.log
# If you want beautiful HTML emails, requires ansifilter found on sourceforge
#ansifilter -i /var/log/duplicity/backup.log -o /var/log/duplicity/backup.log.html -H
#mail -a 'Content-Type: text/html' -s 'Daily Backup Report' [email protected] </var/log/duplicity/backup.log.html
#rm /var/log/duplicity/backup.log.html
mail -s 'Daily Backup Report' root@localhost </var/log/duplicity/backup.log
# Remove old backup log, and backup current log
rm /var/log/duplicity/backup.log.bak
mv /var/log/duplicity/backup.log /var/log/duplicity/backup.log.bak
# Done!
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment