Skip to content

Instantly share code, notes, and snippets.

@valeriansaliou
Last active January 30, 2020 22:16
Show Gist options
  • Save valeriansaliou/5496769e02562f2ac3242bb7752ee599 to your computer and use it in GitHub Desktop.
Save valeriansaliou/5496769e02562f2ac3242bb7752ee599 to your computer and use it in GitHub Desktop.
Personal server backup script, ran every Sunday in the early morning. It's 100% pipe-based, which means it never writes to disk as to buffer backup files before uploading them to S3. Useful to preserve SD card write cycles on a Raspberry Pi.
#!/bin/bash
BACKUP_DATE=$(date +"%Y-%m-%d_%H-%M-%S")
AWS_CONTAINER="s3://xxx-backup/xxx_backup"
AWS_DESTINATION="$AWS_CONTAINER/$BACKUP_DATE"
[email protected]
[email protected]
function upload_secure() {
gpg --trust-model=always --encrypt -r $GPG_RECIPIENT -o - | aws s3 cp - "$AWS_DESTINATION/$1.gpg"
}
function flush_older() {
aws s3 rm "$AWS_CONTAINER" --recursive --exclude "$BACKUP_DATE/*"
}
tar_gz_etc() {
tar -cz /etc/
}
tar_gz_usr_local_bin() {
tar -cz /usr/local/bin/
}
tar_gz_var_lib_prosody() {
tar -cz /var/lib/prosody/
}
tar_var_mail() {
tar -c /var/mail/
}
tar_gz_srv_data_xxx_svc() {
tar --exclude="node_modules" --exclude=".nvm" --exclude=".node-gyp" --exclude=".npm" -cz /srv/data_xxx/svc/
}
dump_gz_mysql() {
mysqldump --user=root --protocol=socket -S /var/run/mysqld/mysqld.sock --default-character-set=utf8mb4 --all-databases | gzip
}
function backup_pipeline() {
# 1. Compress /etc/ to files
# 2. Compress /usr/local/bin/ to files
# 3. Compress /var/lib/prosody/ to files
# 4. Archive /var/mail/ to files
# 5. Compress /srv/data_xxx/svc/ to files
# 6. Export MySQL dump to databases
# 7. Remove older backups (if all previous steps were a success)
tar_gz_etc | upload_secure "etc.tar.gz" && \
tar_gz_usr_local_bin | upload_secure "usr_local_bin.tar.gz" && \
tar_gz_var_lib_prosody | upload_secure "var_lib_prosody.tar.gz" && \
tar_var_mail | upload_secure "var_mail.tar" && \
tar_gz_srv_data_xxx_svc | upload_secure "srv_data_xxx_svc.tar.gz" && \
dump_gz_mysql | upload_secure "mysql.sql.gz" && \
flush_older
}
echo "Starting backup ($BACKUP_DATE)..."
BACKUPLOG=`backup_pipeline 2>&1`
rc=$?
if [[ $rc -ne 0 ]]; then
echo "Backup failed, details will be sent over email ($BACKUP_DATE)."
echo "$BACKUPLOG" | /usr/bin/mail -s "Error: Could not perform server backup" $ADMIN_EMAIL
else
echo "Backup succeeded ($BACKUP_DATE)."
fi
exit $rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment