Created
July 22, 2017 18:39
-
-
Save Sebazzz/19da22d515cc40fb5ab988033530b87a to your computer and use it in GitHub Desktop.
Ubiquiti EdgeOS backup script - See: http://damsteen.nl
This file contains hidden or 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
#!/bin/bash | |
DATE=$(date +%F) | |
TARGET_FILENAME=edgeos-backup-$DATE.tar.gz | |
TARGET_DIR=/tmp | |
TARGET_PATH=$TARGET_DIR/$TARGET_FILENAME | |
SOURCE_DIR=/config | |
DATE_OFFSET_SCRIPT=/config/user-data/date-offset.pl | |
BACKUP_RETENTION=3 | |
FTP_USER=router | |
FTP_PASS=mypassword | |
FTP_HOST=backup.mydomain.local | |
function date-offset { | |
$DATE_OFFSET_SCRIPT $1 | |
} | |
function delete-older-remote-file { | |
OLDER_DATE=$(date-offset $1) | |
OLD_FILENAME=edgeos-backup-$OLDER_DATE.tar.gz | |
echo "... Going to delete older file $OLD_FILENAME" | |
ftp -n -z secure $FTP_HOST <<END_SCRIPT | |
quote USER $FTP_USER | |
quote PASS $FTP_PASS | |
quote PROT P | |
delete $OLD_FILENAME | |
quit | |
END_SCRIPT | |
} | |
echo "Backup /config..." | |
rm $TARGET_PATH 2> /dev/null | |
tar cfz $TARGET_PATH $SOURCE_DIR | |
echo "Uploading to FTP..." | |
ftp -n -z secure $FTP_HOST <<END_SCRIPT | |
quote USER $FTP_USER | |
quote PASS $FTP_PASS | |
quote PROT P | |
binary | |
put $TARGET_PATH $TARGET_FILENAME | |
quit | |
END_SCRIPT | |
echo "Clean up existing file..." | |
rm $TARGET_PATH | |
echo "Clean up older remote files..." | |
for i in $(seq $BACKUP_RETENTION $(($BACKUP_RETENTION + 3))); | |
do delete-older-remote-file $i | |
done | |
exit 0 |
This file contains hidden or 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
#!/usr/bin/perl -w | |
use Time::Piece; | |
use Time::Seconds; | |
my $offset = $ARGV[0]; | |
my $t = localtime() - (ONE_DAY * $offset); | |
print $t->ymd; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment