Last active
December 19, 2022 23:28
-
-
Save mnestor/a57ba06298ea7d25b6046395b3579cc7 to your computer and use it in GitHub Desktop.
Proxmox offsite backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Resides in /root | |
#!/bin/bash | |
bash /etc/pve/scripts/s3-backup.sh "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Resides in /etc/pve/scripts/ | |
#!/bin/bash | |
BUCKET="pve-backups-home" | |
RETAIN=100 #days to retain, I have to keep things for 90 days or I get charged extra | |
DEBUG=0 | |
# Phases | |
# job-start, end, abort | |
# backup-start, end, abort | |
# log-end | |
# pre-stop, restart | |
# post-restart | |
PHASE=$1 | |
# Modes stop, suspend, snapshot | |
MODE=$2 | |
# ID of vm being acted on | |
VMID=$3 | |
## Other env | |
# - Relevant to All Phases | |
# DUMPDIR | |
# STOREID | |
# - NOT Relevant to job- phases | |
# VMTYPE | |
# HOSTNAME | |
# TARGET | |
# LOGFILE | |
# Tricky tricky? with no /dev/tty how to get stdout | |
# in so many wierd configurations? | |
# setup a new pipe to redirect to stdout | |
exec 3>&1 | |
function log() { | |
echo "[S3 Backup - ${PHASE}]: $@" >&3 | |
} | |
function debug() { | |
if [ ${DEBUG:-0} -ne 0 ]; then | |
echo "[S3 Backup - ${PHASE} (DEBUG)]: $@" >&3 | |
fi | |
} | |
function s3run() { | |
action=$1 | |
file=$2 | |
baseFile=`basename ${file}` | |
# redirect this to &3 just like log() does | |
s3cmd ${action} ${file} s3://$BUCKET/ >&3 | |
retVal=$? | |
rm -rf ${file} >&3 | |
if [ $retVal -ne 0 ]; then | |
log "Failed to ${action} ${baseFile} Exit: ${?}" | |
log "Find the exit code details: https://github.com/s3tools/s3cmd/blob/master/S3/ExitCodes.py" | |
fi | |
} | |
case $PHASE in | |
job-end) | |
# job is done, let's cleanup old backups | |
log "Cleanup old backups" | |
s3cmd ls s3://${BUCKET}/${PREFIX} | grep ".zst" | while read -r line; do | |
# Extract date from filename | |
createDate=`echo $line|awk {'print $1" "$2'}` | |
# convert into a real date | |
createDate=$(date -d "$createDate" "+%s") | |
# create a date to compare to | |
olderThan=$(date -d "${RETAIN:-7} days ago" "+%s") | |
if [[ $createDate -le $olderThan ]]; then | |
files=`echo $line|awk '{gsub("tar.zst", ""); print $4}'` | |
s3cmd del "${files}" | |
fi | |
done | |
;; | |
backup-end) | |
# vm is backed up so we can copy the file to s3 | |
s3run put ${TARGET} | |
;; | |
log-end) | |
# log is done being written so copy it up | |
s3run put ${LOGFILE} | |
;; | |
*) | |
# Unknown something happened | |
debug "This phase (${PHASE}) is not setup" | |
;; | |
esac | |
# Clean exit | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't for get to
chmod +x ~/s3-backup.sh
S3 configuration
Install:
apt install s3cmd
Configure:
s3cmd --configure
Changes made to
~/.s3cfg
Move to
/etc/pve/.s3cfg
Link to
/root
on all hosts in cluster.cd; ln -s /etc/pve/.s3cfg
Encrypting the backups on S3
Stop the chunking
Stats are nice too