2019-10-05 19:58:08 +02:00
#!/bin/sh
2019-10-07 20:48:17 +02:00
# Purpose {{{
## Try to centralize all game's save in order to :
## easily backup all save
## easily restore it
## be able to access it from anywhere
## … all you can do with a Nextcloud (share, versionning,…)
##
2019-10-10 10:59:23 +02:00
## 1. Move save directories of a list of known games from Steam's userdata,
## common or compatdata directories to a remote directory (Nextcloud, remote
## mount,…).
## Then create a symlink in userdata, common or compatdata Steam directory to
## the remote game directory.
2019-10-07 20:48:17 +02:00
##
## 2. If a directory doesn't exist, check if a remote one is
## available and symlink it.
##
2019-10-10 10:59:23 +02:00
## 3. List userdata subdirectories without symlinks.
2019-10-07 20:48:17 +02:00
##
2019-10-10 10:59:23 +02:00
## KISS : Only manage save directories from Steam's directories. For other
2019-10-07 20:48:17 +02:00
## paths ($XDG_DATA_HOME,…) check other scripts.
# }}}
2019-10-05 19:58:08 +02:00
# Vars {{{
2019-10-23 11:04:02 +02:00
debug=1
2019-10-05 20:40:50 +02:00
2019-10-05 20:05:14 +02:00
## Steam {{{
steam_id="112595584"
steam_userdata=".steam/steam/userdata/${steam_id}"
2019-10-08 18:22:44 +02:00
steam_compatdata=".steam/steam/steamapps/compatdata"
2019-10-09 17:12:53 +02:00
steam_common=".steam/steam/steamapps/common"
2019-10-05 20:40:50 +02:00
2019-10-08 18:34:06 +02:00
## List of Steam saves in userdata to backup {{{
2019-10-07 22:56:53 +02:00
### 760 − Steam Screenshots − https://steamdb.info/app/760/
### 35700 − Trine Enchanted Edition − https://pcgamingwiki.com/wiki/Trine_Enchanted_Edition
### 35720 − Trine 2 Complete Story − https://pcgamingwiki.com/wiki/Trine_2
2019-10-08 18:22:44 +02:00
### 55230 − Saints Row: The Third − https://pcgamingwiki.com/wiki/Saints_Row:_The_Third
2019-10-05 20:40:50 +02:00
### 204360 − Castle Crashers − https://pcgamingwiki.com/wiki/Castle_Crashers
2019-10-07 22:56:53 +02:00
### 206420 − Saints Row IV − https://pcgamingwiki.com/wiki/Saints_Row_IV
### 218820 − Mercenary Kings − https://pcgamingwiki.com/wiki/Mercenary_Kings
2020-01-08 17:19:38 +01:00
### 220440 − DmC: Devil May Cry − https://www.pcgamingwiki.com/wiki/DmC:_Devil_May_Cry
2019-12-18 18:42:31 +01:00
### 242760 − The Forest − https://www.pcgamingwiki.com/wiki/The_Forest
2019-10-07 22:56:53 +02:00
### 247080 − Crypt of the Necrodancer − https://pcgamingwiki.com/wiki/Crypt_of_the_Necrodancer
### 255870 − PixelJunk Shooter − https://pcgamingwiki.com/wiki/PixelJunk_Shooter
### 312530 − Duck Game − https://pcgamingwiki.com/wiki/Duck_Game
### 359840 − Shift Happens − https://pcgamingwiki.com/wiki/Shift_Happens
2019-12-18 18:42:15 +01:00
### 534550 − Guacamelee! 2 − https://www.pcgamingwiki.com/wiki/Guacamelee!_2
2020-01-08 17:19:38 +01:00
steam_userdata_games="760 35700 35720 55230 204360 206420 218820 220440 247080 242760 255870 312530 359840 534550"
2019-10-07 22:56:53 +02:00
## }}}
2019-10-09 16:50:18 +02:00
## Pattern of Steam saves in common to backup {{{
2019-10-09 17:12:53 +02:00
### Add the directory name of the game and the directory|file name to backup
### separated by a slash.
2019-10-09 17:44:58 +02:00
### To be able to manage white space in directory name, the field separator is %.
### eg. GAME NAME/data.save%other game/*.sav
### 620 − Portal 2 − https://pcgamingwiki.com/wiki/Portal_2 (solo only, multiplayer is on Steam cloud)
2019-10-09 18:10:46 +02:00
### 251470 − TowerFall Ascension − https://pcgamingwiki.com/wiki/TowerFall_Ascension (not managed cause no substree, check $XDG_DATA_HOME)
2019-10-09 16:50:18 +02:00
### 274190 − Broforce − https://pcgamingwiki.com/wiki/Broforce
2019-10-09 17:44:58 +02:00
steam_common_games_pattern="Portal 2/*.sav%Broforce/*.sav"
2019-10-09 16:50:18 +02:00
# }}}
2019-10-09 00:31:18 +02:00
## Pattern of Steam saves in compatdata to backup {{{
2019-10-09 16:40:18 +02:00
### Compatdata contains directories for games using Steam play so it's too big
### to be fully moved to a remote storage.
2019-11-01 13:44:18 +01:00
### Add the game id and the directory|file name to backup separated by a slash.
### And, to be able to manage white space in pattern name, the field separator is %.
### eg. GAME_ID/savedata.xml%GAME_ID42/user.bin
2019-10-08 18:22:44 +02:00
### 213670 − South Park: The Stick of Truth − https://pcgamingwiki.com/wiki/South_Park:_The_Stick_of_Truth
2019-11-01 13:41:16 +01:00
### 242760 − The Forest − https://www.pcgamingwiki.com/wiki/The_Forest
2019-10-09 00:45:41 +02:00
### 312610 − Metal Slug X − https://pcgamingwiki.com/wiki/Metal_Slug_X
2019-10-09 16:40:18 +02:00
### 359840 − Shift Happens − https://pcgamingwiki.com/wiki/Shift_Happens (don't work yet)
2019-11-13 17:55:54 +01:00
### 379720 − Doom (2016) − https://www.pcgamingwiki.com/wiki/Doom_(2016)
2019-12-18 18:28:37 +01:00
### 387290 − Ori and the Blind Forest: Definitive Edition − https://www.pcgamingwiki.com/wiki/Ori_and_the_Blind_Forest:_Definitive_Edition
2019-10-09 16:40:18 +02:00
### 480490 − Prey (2017) − https://pcgamingwiki.com/wiki/Prey_(2017) (don't work yet)
2019-10-08 18:22:44 +02:00
### 686200 − Door Kickers: Action Squad − https://pcgamingwiki.com/wiki/Door_Kickers:_Action_Squad
2019-12-18 18:28:37 +01:00
steam_compatdata_games_pattern="213670/save%242760/TheForest%312610/UserDefault.xml%379720/GAME-AUTOSAVE0%387290/saveFile0.sav%686200/userdata.bin"
2019-10-08 18:22:44 +02:00
# }}}
2019-10-07 22:56:53 +02:00
## Ids without backups in userdata {{{
2019-10-08 00:08:15 +02:00
### 7 − Unknown
2019-12-18 18:47:22 +01:00
### 49520 − Borderlands 2 − https://pcgamingwiki.com/wiki/Borderlands_2
2019-10-07 22:56:53 +02:00
### 219150 − Hotline Miami − https://pcgamingwiki.com/wiki/Hotline_Miami
2019-12-18 18:47:22 +01:00
### 236090 − Dust: An Elysian Tail − https://www.pcgamingwiki.com/wiki/Dust:_An_Elysian_Tail
2019-10-07 22:56:53 +02:00
### 241100 − Steam Controller Configs − https://steamdb.info/app/241100/
### 242680 − Nuclear Throne − https://pcgamingwiki.com/wiki/Nuclear_Throne
### 251470 − TowerFall Ascension − https://pcgamingwiki.com/wiki/TowerFall_Ascension
### 255870 − PixelJunk Shooter − https://pcgamingwiki.com/wiki/PixelJunk_Shooter
### 268990 − The Dishwasher: Vampire Smile − https://pcgamingwiki.com/wiki/The_Dishwasher:_Vampire_Smile
### 295790 − Never Alone − https://pcgamingwiki.com/wiki/Never_Alone
2019-12-18 18:47:22 +01:00
### 379720 − Doom (2016) − https://www.pcgamingwiki.com/wiki/Doom_(2016)
### 387290 − Ori and the Blind Forest: Definitive Edition − https://www.pcgamingwiki.com/wiki/Ori_and_the_Blind_Forest:_Definitive_Edition
2019-10-07 22:56:53 +02:00
### 416600 − Full Metal Furies − https://pcgamingwiki.com/wiki/Full_Metal_Furies
### 474210 − Butcher − https://pcgamingwiki.com/wiki/Butcher
### 697660 − Jump Gunners − https://pcgamingwiki.com/wiki/Jump_Gunners
### 728880 − Overcooked! 2 − https://pcgamingwiki.com/wiki/Overcooked!_2
2019-12-18 18:47:22 +01:00
ignore_pattern_steam_id="(7|620|49520|55230|213670|219150|236090|241100|242680|251470|255870|268990|274190|295790|379720|387290|416600|474210|480490|686200|697660|728880|config|ugc|ugcmsgcache|\.)$"
2019-10-07 22:56:53 +02:00
## }}}
2019-10-05 20:05:14 +02:00
## }}}
2019-10-05 19:58:08 +02:00
remote_dir="${HOME}/Nextcloud/games/linux.sgl.script"
2019-10-08 00:08:15 +02:00
local_unmanaged_games_list="/tmp/unmanaged_games_list"
2019-10-05 19:58:08 +02:00
# }}}
2019-10-05 20:54:04 +02:00
# Functions {{{
# Move one Steam save game dir {{{
move_steam_game_dir() {
2019-10-05 21:07:13 +02:00
_game_id="${1}"
2019-10-08 18:34:06 +02:00
_steam_dir="${2}"
_local_game_path="${HOME}/${_steam_dir}/${_game_id}"
_remote_game_path="${remote_dir}/${_steam_dir}/${_game_id}"
2019-10-05 20:54:04 +02:00
## If a remote directory doesn't already exists for this game
2019-10-05 21:07:13 +02:00
if [ ! -d "${_remote_game_path}" ]; then
2019-10-09 00:32:03 +02:00
### Ensure to create tree directories on remote storage
mkdir -p -- "$(dirname "${_remote_game_path}")"
2019-10-08 19:10:37 +02:00
### Move data to remote storage
2019-10-05 21:07:13 +02:00
mv -- "${_local_game_path}" "${_remote_game_path}"
2019-10-08 19:10:37 +02:00
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Move Steam game − The data of ${_game_id} − ${_local_game_path} moved to remote storage."
### Then ask to create a symbolic link to local storage
symlink_steam_game_dir "${_game_id}" "${_steam_dir}"
2019-10-05 20:54:04 +02:00
else
2019-10-08 19:10:37 +02:00
printf '\e[1;35m%-6s\e[m\n' "Move Steam game − ${_game_id} already have data on remote storage : ${_remote_game_path}. Abort to avoid to override data."
2019-10-05 20:54:04 +02:00
exit 5
fi
}
# }}}
2019-10-05 21:06:33 +02:00
# Symlink one Steam save game dir from remote to local {{{
symlink_steam_game_dir() {
_game_id="${1}"
2019-10-08 18:34:06 +02:00
_steam_dir="${2}"
_local_game_path="${HOME}/${_steam_dir}/${_game_id}"
_remote_game_path="${remote_dir}/${_steam_dir}/${_game_id}"
2019-10-05 21:06:33 +02:00
if [ -d "${_remote_game_path}" ]; then
ln -s -- "${_remote_game_path}" "${_local_game_path}"
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink Steam game — Symlink remote data of ${_game_id} to local storage."
else
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Symlink Steam game — ${_game_id} doesn't have remote data."
fi
}
# }}}
2019-10-05 20:54:04 +02:00
# }}}
2019-10-05 19:58:08 +02:00
# Tests {{{
## Ensure remote dir exist {{{
if [ ! -d "${remote_dir}" ]; then
printf '\e[1;35m%-6s\e[m\n' "The directory for save game doesn't exists : ${remote_dir}"
exit 1
fi
## }}}
2019-10-08 22:51:22 +02:00
## Ensure Steam directories exist {{{
2019-10-09 17:12:53 +02:00
for steam_dir in "${steam_userdata}" "${steam_common}" "${steam_compatdata}"; do
2019-10-08 22:51:22 +02:00
local_steam_path="${HOME}/${steam_dir}"
if [ ! -d "${local_steam_path}" ]; then
printf '\e[1;35m%-6s\e[m\n' "The Steam directory − ${steam_dir} for your ID (${steam_id}) doesn't exists yet… Should it must be create (for restoration,…) [Y/n] ?"
read -r create_local_steam_userdata
if [ "${create_local_steam_userdata}" = "" ] || [ "${create_local_steam_userdata}" = "Y" ] || [ "${create_local_steam_userdata}" = "y" ]; then
mkdir -p -- "${local_steam_path}"
else
printf '\e[1;35m%-6s\e[m\n' "Steam directory (${steam_dir}) doesn't exists, abort script."
exit 2
fi
2019-10-05 19:58:08 +02:00
fi
2019-10-08 22:51:22 +02:00
done
2019-10-05 19:58:08 +02:00
## }}}
# }}}
2019-10-05 20:40:50 +02:00
2019-10-13 20:32:20 +02:00
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Run save game script for Steam."
2019-10-08 18:22:44 +02:00
# Manage Steam userdata save game {{{
2019-10-08 18:34:06 +02:00
for game_id in ${steam_userdata_games}; do
local_game_path="${HOME}/${steam_userdata}/${game_id}"
2019-10-09 18:10:46 +02:00
local_game_path_type="$(file "${local_game_path}" | cut -d' ' -f2)"
2019-10-05 20:40:50 +02:00
case ${local_game_path_type} in
## Data is already a symlink
"symbolic")
2019-10-09 18:10:46 +02:00
link_name="$(file "${local_game_path}" | sed 's;.* symbolic link to \(.*\);\1;')"
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam userdata for loop — The data of ${game_id} are already symlinked to ${link_name} . Skip."
2019-10-08 00:08:15 +02:00
;;
2019-10-05 20:40:50 +02:00
## Data is still a directory
"directory")
2019-10-08 18:34:06 +02:00
move_steam_game_dir "${game_id}" "${steam_userdata}"
2019-10-05 20:40:50 +02:00
;;
2019-10-08 18:34:06 +02:00
## Data doesn't exist
2019-10-05 20:40:50 +02:00
"cannot")
2019-10-09 17:12:53 +02:00
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam userdata for loop — The data of ${game_id} − ${local_game_path} doesn't exist. Skip."
2019-10-08 18:34:06 +02:00
symlink_steam_game_dir "${game_id}" "${steam_userdata}"
2019-10-05 20:40:50 +02:00
;;
2019-10-08 18:34:06 +02:00
## Data can't be managed
2019-10-05 20:40:50 +02:00
*)
2019-10-08 18:22:44 +02:00
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_id} (userdata) − ${local_game_path} are not managed. Type: ${local_game_path_type}. Abort"
2019-10-08 18:25:02 +02:00
exit 3
2019-10-05 20:40:50 +02:00
;;
esac
done
2019-10-07 20:48:17 +02:00
# }}}
2019-10-09 17:12:53 +02:00
# Manage Steam common save game {{{
2019-10-09 17:44:58 +02:00
IFS="%"
2019-10-09 17:12:53 +02:00
for game_pattern in ${steam_common_games_pattern}; do
## Separate the game_name and the directory|file to backup|symlink
2019-10-09 18:10:46 +02:00
game_name="$(echo "${game_pattern}" | cut -d"/" -f1)"
save_pattern="$(echo "${game_pattern}" | cut -d"/" -f2)"
2019-10-09 17:12:53 +02:00
## If the game is installed
if [ -d "${HOME}/${steam_common}/${game_name}" ]; then
### Follow symbolic links but avoid links to dosdevices and keep only one result
temp_local_save_path="$(find -L "${HOME}/${steam_common}/${game_name}" -iname "${save_pattern}" -print -quit)"
local_save_path="$(dirname "${temp_local_save_path}")"
local_save_path_type="$(ls -ld "${local_save_path}" | sed 's/\(^.\).*/\1/')"
## Path independent from local or remote base directory
steam_dir="$(printf "%s" "${local_save_path}" | sed -e "s;${HOME}/;;")"
## Print vars {{{
#if [ "${debug}" -eq "0" ]; then
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : game name : ${game_name}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : save pattern : ${save_pattern}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : temp local save path : ${temp_local_save_path}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : local save path : ${local_save_path}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : local save type : ${local_save_path_type}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : steam dir : ${steam_dir}"
#fi
## }}}
case ${local_save_path_type} in
## Data is already a symlink
"symbolic"|"symboliclink"|"l")
2019-10-09 18:10:46 +02:00
link_name="$(file "${local_save_path}" | sed 's;.* symbolic link to \(.*\);\1;')"
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam common for loop — The data of ${game_name} are already symlinked to ${link_name} . Skip."
2019-10-09 17:12:53 +02:00
;;
## Data is still a directory, try to move it
"directory"|"d")
move_steam_game_dir "$(basename "${steam_dir}")" "$(dirname "${steam_dir}")"
;;
## Data doesn't exist
"cannot")
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam common for loop — The data of ${game_name} − ${local_save_path} doesn't exist. Skip."
### TODO : Try to symlink
;;
## Data can't be managed
*)
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_name} (common) − ${local_save_path} are not managed. Type: ${local_save_path_type}. Abort."
exit 3
;;
esac
else ## The game is not present on the system
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam common for loop — ${game_name} doesn't seems to be installed on the system, check the path : ${HOME}/${steam_common}/${game_name} . Skip."
fi
done
# }}}
# Manage Steam compatdata save game {{{
2019-11-01 13:44:18 +01:00
IFS="%"
2019-10-09 00:31:18 +02:00
for game_pattern in ${steam_compatdata_games_pattern}; do
## Separate the game_id and the directory|file to backup|symlink
2019-10-09 18:10:46 +02:00
game_id="$(echo "${game_pattern}" | cut -d"/" -f1)"
save_pattern="$(echo "${game_pattern}" | cut -d"/" -f2)"
2019-10-09 00:31:18 +02:00
2019-10-09 16:50:18 +02:00
## If the game is installed
if [ -d "${HOME}/${steam_compatdata}/${game_id}" ]; then
### Follow symbolic links but avoid links to dosdevices and keep only one result
temp_local_save_path="$(find -L "${HOME}/${steam_compatdata}/${game_id}" -ipath "*dosdevices*" -prune -o -iname "${save_pattern}" | grep -v "dosdevices" | head -n 1)"
2019-11-01 14:00:23 +01:00
### Verify if the previous command successfully return an existing path
if [ ! -e "${temp_local_save_path}" ]; then
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_id} (compatdata) − Can't find the path to ${save_pattern} pattern. Result of find : ${temp_local_save_path} . Abort."
exit 3
fi
2019-10-09 16:50:18 +02:00
local_save_path="$(dirname "${temp_local_save_path}")"
local_save_path_type="$(ls -ld "${local_save_path}" | sed 's/\(^.\).*/\1/')"
## Path independent from local or remote base directory
steam_dir="$(printf "%s" "${local_save_path}" | sed -e "s;${HOME}/;;")"
## Print vars {{{
#if [ "${debug}" -eq "0" ]; then
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : game ID : ${game_id}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : save pattern : ${save_pattern}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : temp local save path : ${temp_local_save_path}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : local save path : ${local_save_path}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : local save type : ${local_save_path_type}"
#printf '\e[1;35m%-6s\e[m\n' "DEBUG : steam dir : ${steam_dir}"
#fi
## }}}
case ${local_save_path_type} in
## Data is already a symlink
"symbolic"|"symboliclink"|"l")
2019-10-09 18:10:46 +02:00
link_name="$(file "${local_save_path}" | sed 's;.* symbolic link to \(.*\);\1;')"
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam compatdata for loop — The data of ${game_id} are already symlinked to ${link_name} . Skip."
2019-10-09 16:50:18 +02:00
;;
## Data is still a directory, try to move it
"directory"|"d")
2019-11-01 14:00:23 +01:00
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Data of ${game_id} (compatdata) − Try to move ${local_save_path} to remote share."
2019-10-09 16:50:18 +02:00
move_steam_game_dir "$(basename "${steam_dir}")" "$(dirname "${steam_dir}")"
;;
## Data doesn't exist
"cannot")
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam compatdata for loop — The data of ${game_id} − ${local_save_path} doesn't exist. Skip."
### TODO : Try to symlink
;;
## Data can't be managed
*)
printf '\e[1;35m%-6s\e[m\n' "Data of ${game_id} (compatdata) − ${local_save_path} are not managed. Type: ${local_save_path_type}. Abort."
exit 3
;;
esac
else ## The game is not present on the system
[ "${debug}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Steam compatdata for loop — ${game_id} doesn't seems to be installed on the system. Skip."
fi
2019-10-08 18:22:44 +02:00
done
# }}}
# List userdata unmanage game id {{{
2019-10-08 22:51:22 +02:00
cd -- "${HOME}/${steam_userdata}" || exit 1
2019-10-08 00:08:15 +02:00
rm -f -- "${local_unmanaged_games_list}"
find . -maxdepth 1 -type d | grep -vE "${ignore_pattern_steam_id}" > "${local_unmanaged_games_list}"
if [ -s "${local_unmanaged_games_list}" ]; then
printf '\e[1;35m%-6s\e[m\n' "List of unmanaged directories :"
cat "${local_unmanaged_games_list}"
fi
rm -f -- "${local_unmanaged_games_list}"
cd -- - > /dev/null || exit 1
# }}}
exit 0