Commit bfe481f2 authored by Fjen Undso's avatar Fjen Undso
Browse files

delete old backups on ftp and check directory creation

parent a45b1c74
......@@ -5,7 +5,7 @@
#
# sftp note: ssh private/public key with no password is required
# if not using pwauth
# TODO: NUMBACKUPS for ftp server and gpg key encryption
# TODO: gpg key encryption
##### BEGIN SETTINGS
# system settings
......@@ -24,32 +24,40 @@ declare -r EXLUDEDBS='^(?!(mysql|phpmyadmin|information_schema|performance_schem
declare -r FTPHOST="example.com"
declare -r FTPUSER=""
declare -r FTPPASS=""
declare -r FTPDIR="/path/on/ftp"
declare -r FTPDIR="path/on/ftp"
declare -r FTPTYPE="sftp" # ftp or sftp - leave empty if not desired
declare -r FTPNUMBACKUPS=90
# gpg settings
declare -r GPGPASS="" # optional gpg symmetrical passphrase
##### END SETTINGS
# Check prerequisites
execftp() {
lftp -u $FTPUSER,$FTPPASS $FTPTYPE://$FTPHOST -e "$1"
}
### Check prerequisites
for i in mydumper lftp mail tar $COMPRESS; do
if [ ! "$(which $i)" ]; then
echo "Error: prerequisite $i not found in PATH" >&2
echo "Error: prerequisite $i not found in PATH" >&2
exit 1
fi
done
### Check local directory
if [ ! -d "$LOCALDIR" ]; then
mkdir -p "$LOCALDIR" \
|| echo "Failed to create $LOCALDIR" >&2 \
&& exit 1
fi
echo "### Delete old backups"
### Delete local old backups
echo "### Delete local old backups"
for dir in $(ls -t1 "$LOCALDIR" | tail -n +$NUMBACKUPS); do
echo "delete: $dir"
rm -r "$LOCALDIR/$dir"
done
### MySQL backup
echo "### MySQL Backup..."
date=$(date +"%Y%m%d-%H%M%S")
backupdir="$LOCALDIR/$date"
......@@ -59,6 +67,7 @@ mkdir "$backupdir" \
mydumper -u "$MYUSER" -p "$MYPASS" -h "$MYHOST" --regex "$EXLUDEDBS" -o "$backupdir"
faildump=$?
### Compression
echo "### Compression..."
cd "$backupdir"
dbs=$(ls *.sql | cut -d "." -f 1 | cut -d "-" -f 1 | sort | uniq)
......@@ -73,6 +82,7 @@ for db in $dbs; do
done
cd -
### Encryption
if [ -n "$GPGPASS" ]; then
echo "### Encryption..."
cd "$backupdir"
......@@ -82,17 +92,40 @@ if [ -n "$GPGPASS" ]; then
cd -
fi
echo "### Upload to FTP..."
if [ "$FTPTYPE" == "ftp" ] || [ "$FTPTYPE" == "sftp" ]; then
lftp -u $FTPUSER,$FTPPASS $FTPTYPE://$FTPHOST \
-e "mkdir $FTPDIR; cd $FTPDIR; mirror -R $backupdir"
failftp=$?
else
if [ "$FTPTYPE" != "ftp" ] && [ "$FTPTYPE" != "sftp" ]; then
echo "NO FTP UPLOAD CONFIGURED"
failftp=0
else
### FTP: directory permission check
ftppwd=$(execftp "mkdir -p $FTPDIR/$date && cd $FTPDIR/$date; pwd; exit")
FTPDIRnoslash=${FTPDIR%\/} # remove trailing /
if [[ "$ftppwd" != *"$FTPDIRnoslash/$date" ]]; then
echo "Error: failed to create $FTPDIRnoslash/$date on ftp" >&2
failftp=1
fi
### FTP: delete old backups
ftpfiles=$(execftp "cd $FTPDIR; nlist; exit")
# filter files starting with a dot
$ftpfiles=( $(echo $ftpfiles | sed 's,\.[A-Za-z0-9_\/\.]* , ,g') )
# find old backups
ftpfilesdelete=()
while [ ${#ftpfiles[@]} -ge $FTPNUMBACKUPS]; do
ftpfilesdelete+=(${ftpfiles[0]})
unset ftpfiles[0]
done
# delete on ftp
execftp "cd $FTPDIR; rm -r ${ftpfilesdelete[@]}; exit"
### FTP: upload
if [ -z $failftp ]; then
echo "### Upload to FTP..."
execftp "cd $FTPDIR; mirror -R $backupdir; exit"
failftp=$?
fi
fi
# Mail admin on error
### Mail admin on error
if [ "$faildump" -ge 1 ] || [ "$failftp" -ge 1 ]; then
tmplogfile=`mktemp`
echo "Date: $(date)" >> $tmplogfile
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment