initial commit
initial commit

file:b/mysql.backup (new)
--- /dev/null
+++ b/mysql.backup
@@ -1,1 +1,91 @@
-
+#!/bin/sh

+

+# mysql user/pass

+MYSQLUSER=""

+MYSQLPASS=""

+

+# local server name (machineX)

+LOCALSERVER=""

+

+# backup server (user@server)

+REMOTESERVER=""

+

+# backup folder on the backup server (/var/mysqlbackup)

+LOCALBACKUPPATH=""

+

+# remote backup path (/var/mysqlbackup)

+REMOTEBACKUPPATH=""

+

+# days to keep mysql backup locally

+LOCALBACKUPDAYS=7

+

+# days to keep mysql backup remotely

+REMOTEBACKUPDAYS=31

+

+# scp speed in Kbit/s

+TRANSFERSPEED=80000

+

+echo "$LOCALSERVER mySQL backup started at $(date)"

+

+# go to backup dir

+cd $LOCALBACKUPPATH

+

+# backup folder/file name

+BACKUPFILENAME=$(date +%Y%m%d)-all-databases-$LOCALSERVER

+

+if [ -f $BACKUPFILENAME.tar.gz ];

+then

+        echo "backup file $BACKUPFILENAME.tar.gz exists, proceding to upload";

+else

+        # creating temporary directory

+        mkdir $BACKUPFILENAME

+

+        # create backup

+        for DB in $( mysqlshow -u$MYSQLUSER -p$MYSQLPASS | awk '{print $2}' ); do

+                if [ $DB = "Databases" ]; then

+                        echo "skipping $DB"

+                else

+                        echo "dumping database $DB"

+                        mysqldump --add-drop-table -c -u$MYSQLUSER -p$MYSQLPASS --databases $DB > $BACKUPFILENAME/$DB.sql

+                fi

+        done

+

+        # create archive

+        echo "creating archive $BACKUPFILENAME.tar.gz at $(date)"

+        tar -czf $BACKUPFILENAME.tar.gz $BACKUPFILENAME

+        echo "archive created at $(date)"

+

+        # remove directory

+        if [ -d $BACKUPFILENAME ];

+        then

+                echo "deleting temporary folder $BACKUPFILENAME at $(date)"

+                rm $BACKUPFILENAME/*.*

+                rmdir $BACKUPFILENAME

+        fi

+fi

+

+# keep $LOCALBACKUPDAYS days local backup, remove older

+TOREMOVEFILENAME=$(date +%Y%m%d --date='$LOCALBACKUPDAYS days ago')-all-databases-$LOCALSERVER.tar.gz

+if [ -f $TOREMOVEFILENAME ]

+then

+    echo "deleting $LOCALBACKUPDAYS days old backup $TOREMOVEFILENAME"

+    rm $TOREMOVEFILENAME

+fi

+

+echo "$LOCALSERVER mySQL backup ended at $(date)"

+

+if [ $REMOTESERVER ]

+then

+    # keep $REMOTEBACKUPDAYS days on the backup server, remove older

+    TOREMOVEFILENAME=$(date +%Y%m%d --date='$REMOTEBACKUPDAYS days ago')-all-databases-$LOCALSERVER.tar.gz

+    

+    # first remove old backup

+    ssh -2 $REMOTESERVER "rm -f $REMOTEBACKUPPATH/$TOREMOVEFILENAME"

+    

+    echo "upload started at $(date)"

+    

+    # then send new backup file

+    scp -l $TRANSFERSPEED $BACKUPFILENAME.tar.gz $REMOTESERVER:$REMOTEBACKUPPATH

+    

+    echo "upload ended at $(date)"

+fi

comments