Template:MySQLdump Linux

From Official Kodi Wiki
Jump to navigation Jump to search

Using MySQLdump in Linux is also possible, and is typically provided along with the mysql/mariadb-server installation. This is a script that can be used either manually or be hung into time-repetitive cron jobs. Log files are created with filenames that have a timestamp built-in, so older backups will not be overwritten. Copy the script below into your favorite text-editor and save it as a .sh (bash) file into the location+filename of your chosing, and make the file executable with chmod +x yourfilename.sh. You can export database contents for the Kodi video library, music library, and the database user for the Kodi application. You can do so separately or combined with all three parameters at the same time.

#!/usr/bin/env bash
#set -x  # enable in case of trouble

echo ' '
echo 'MYSQLDUMP databases Kodi v19+ video & music'
echo '-------------------------------------------'

# TESTED ON UBUNTU DESKTOP 21.10 with MYSQL SERVER 5.7.36 ON UBUNTU SERVER 18.04
# TESTED ON UBUNTU SERVER 22.04 with MYSQL SERVER 8.0.31 LOCALLY

timestamp=$(date +"%Y%m%d_%H%M");

# EXPORT PATH, ADJUST TO YOUR OWN CONVENIENCE
path="/home/user/Dropbox/MySQL/kodidtbs_v19_""$timestamp""_"

# FILENAMES
video_export="$path""video.sql"
music_export="$path""music.sql"
user_export="$path""user.sql"

# USER PARAMETERS TO EDIT ACCORDING TO YOUR DATABASE SERVER, CREDENTIALS AND PORT SITUATION
user=" --user=kodi --password=kodi"
srvr=" --host=srvr1 "
port=" --port=3306 "

# FIXED PARAMETERS FOR KODI DATABASES
params=" --add-drop-database --add-drop-table --add-drop-trigger --routines --triggers "

# VARIABLE PARAMETERS FOR KODI DATABASES
video=" --databases MyVideos119 "
music=" --databases MyMusic82 "

# ---------------------------------------------------------------------------------------

# CHECK IF USER-GIVEN PARAMETERS ARE PRESENT
if [ $# -gt 0 ]; then
	echo ' ';
	# LOOP THROUGH PARAMETERS GIVEN
	while test $# -gt 0
	do
		 case "$1" in
			  v|video|all)
				echo "Exporting video to : ""$video_export";
				mysqldump $user $srvr $params $video > $video_export
				;;
			  m|music|all)
				echo "Exporting music to : ""$music_export";
				mysqldump $user $srvr $params $music > $music_export
			  	;;
			  u|user|all)
				params="--databases mysql --tables user --skip-add-drop-table --no-create-info"
				echo "Exporting user to : ""$user_export";
				mysqldump $user $srvr $params --where="User='kodi'" > $user_export
				;;
		 esac
		 # NEXT VARIABLE
		 shift
	done
	echo ' ';
else
	echo 'EXAMPLE: ./dtbs-export video ';
	echo ' ';
	echo 'Possible user parameters:';
	echo '  v or video';
	echo '  m or music';
	echo '  u or user';
	echo '  all';
	echo ' ';
	echo 'Note: for importing full Kodi database exports, you need SQL database root user access.'
	echo ' '
	echo 'For a complete guide, type: "mysqldump --help" or "man mysqldump" ';
	echo ' ';
fi

# EXIT