Skip to content

Instantly share code, notes, and snippets.

@Ironlenny
Created July 17, 2019 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ironlenny/75ecce8d7d5c71db7446c51a3e2b66a6 to your computer and use it in GitHub Desktop.
Save Ironlenny/75ecce8d7d5c71db7446c51a3e2b66a6 to your computer and use it in GitHub Desktop.
Backup files to Google Drive
#!/bin/sh
# Author: Jacob Riddle
# Email: useafterfree.consulting@gmail.com
#
# This script will backup a file to Google Drive. Each time this script is
# called it will create a compressed archive of the path, given as an argument,
# in /tmp. Each archive will have an accompanying checksum file for integrety
# verification. It will name the archive according to the following scheme:
#
# For daily backups:
# $basename-backup-(Mon|Tues|Wed|Thru).tar.gz
#
# For weekly backups:
# $basename-backup-w(0-7).tar.gz
#
# Backups will occur nightly with files being over-written as needed. Daily
# backups are on a five(5) day rotation. Weekly ones are on a eight(8) week
# rotation. Each archive will have an accompanying checksum file for integrety
# verification.
#
# Example usage:
#
# $> backup.sh /var/www/wiki
#
# This script requires rclone (rclone.org) to be installed.
# set -x # Debug
path=$1
# Day of week
day=$(date +"%a")
filename="$(basename $path)-backup"
backup_location="$(mktemp -d)"
tar_cmd="tar --selinux -czf" # To extract run 'tar --selinux -xvf'
# Number of weeks between rotations
rotate_week=8
# Create daily archive
create_arc () {
local file=$1
local time=$2
cd $backup_location
$tar_cmd $file-$time.tar.gz $path
sha256sum $file-$time.tar.gz > $file-$time.sha256
}
# Creates weekly archive.
create_week_arc () {
# Current week
# local week=$(date +%U)
let "week= $(date +%U) % $rotate_week"
local file=$1
local dow=$2
if test $dow = "Fri"; then
cd $backup_location
mv $file-$dow.tar.gz $file-$week.tar.gz && mv $file-$dow.sha256 $file-$week.sha256
fi
}
# Copy files to Google Drive
copy() {
local dir=$1
cd $dir
for i in *; do
rclone copy $i backup:/
done
}
create_arc $filename $day
create_week_arc $filename $day
copy $backup_location
# Clean up
rm -r $backup_location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment