Skip to content

Instantly share code, notes, and snippets.

@elomagic
Last active May 1, 2020 18:14
Show Gist options
  • Save elomagic/1171063ba7f6d5006094fc97be1b02c0 to your computer and use it in GitHub Desktop.
Save elomagic/1171063ba7f6d5006094fc97be1b02c0 to your computer and use it in GitHub Desktop.
Script for installing a Time Machine server
#!/bin/bash
#title : timemachine-install.sh
#description : Script for installing a Time Machine server.
#more : http://www.elomagic.de
#author : Carsten Rambow
#date : 2016-12-02
#usage : /bin/bash timemachine-install.sh
#tested-distros : Ubuntu Server 16.10, Raspbian (Based on Debian)
TIMEMACHINE_USER="timemachine"
TIMEMACHINE_VOLUME_NAME_DEFAULT="TimeMachine"
TIMEMACHINE_DATA_FOLDER="/data/osx/timemachine"
NETWORK_INTERFACE=eth0
ECHO_COLOR_DEFAULT=\\e[0m
ECHO_COLOR_TITLE=\\033[33m
ECHO_COLOR_ERROR=\\033[31m
ECHO_COLOR_DONE=\\033[32m
if [[ $EUID -ne 0 ]]; then
echo -e "${ECHO_COLOR_ERROR}This script must be run as root.${ECHO_COLOR_DEFAULT}"
exit 1
fi
read -e -p "Time Machine Volume Name: " -i "$TIMEMACHINE_VOLUME_NAME_DEFAULT" TIMEMACHINE_VOLUME_NAME
# TODO Check if "/etc/avahi/services/afpd.service" already exists then abort
# TODO Check if "/etc/avahi/services/deviceinfo.service" already exists then abort
echo -e "${ECHO_COLOR_TITLE}Installing uuid-runtime package...${ECHO_COLOR_DEFAULT}"
sudo apt-get install uuid-runtime
echo -e "${ECHO_COLOR_TITLE}Installing required dependencies netatalk and avahi-daemon...${ECHO_COLOR_DEFAULT}"
apt-get install netatalk avahi-daemon
echo -e "${ECHO_COLOR_TITLE}Creating afpd.service file...${ECHO_COLOR_DEFAULT}"
cat > /etc/avahi/services/afpd.service << EOF
<?xml version="1.0" standalone="no"?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<!-- Created by timemachine-install.sh -->
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_afpovertcp._tcp</type>
<port>548</port>
</service>
<service>
<type>_device-info._tcp</type>
<port>0</port>
<txt-record>model=TimeCapsule</txt-record>
</service>
</service-group>
EOF
echo -e "${ECHO_COLOR_TITLE}Creating deviceinfo.service file...${ECHO_COLOR_DEFAULT}"
cat > /etc/avahi/services/deviceinfo.service << EOF
<?xml version="1.0" standalone="no"?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<!-- Created by timemachine-install.sh -->
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_device-info._tcp</type>
<port>548</port>
<txt-record>model=Xserve</txt-record>
</service>
</service-group>
EOF
echo -e "${ECHO_COLOR_TITLE}Restarting service netatalk...${ECHO_COLOR_DEFAULT}"
/etc/init.d/netatalk restart
echo -e "${ECHO_COLOR_TITLE}Restarting service avahi-daemon...${ECHO_COLOR_DEFAULT}"
/etc/init.d/avahi-daemon restart
echo -e "${ECHO_COLOR_TITLE}Creating time machine data folder $TIMEMACHINE_DATA_FOLDER...${ECHO_COLOR_DEFAULT}"
mkdir -p $TIMEMACHINE_DATA_FOLDER
# TODO Check if user timemachine already exists
echo -e "${ECHO_COLOR_TITLE}Creating user $TIMEMACHINE_USER...${ECHO_COLOR_DEFAULT}"
useradd -c "Time Machine User" -d $TIMEMACHINE_DATA_FOLDER -s /bin/false -g 10 $TIMEMACHINE_USER
# Password für time machine account must be entered by user
passwd $TIMEMACHINE_USER
# Mark volume as a supporting time machine drive
touch $TIMEMACHINE_DATA_FOLDER/.com.apple.timemachine.supported
# Some permissions stuff I do on most volumes to keep ACLs sane
chown -R $TIMEMACHINE_USER:users $TIMEMACHINE_DATA_FOLDER
# Configure netatalk for time machine and a couple other volumes
cat >> /etc/netatalk/AppleVolumes.default << EOF
$TIMEMACHINE_DATA_FOLDER $TIMEMACHINE_VOLUME_NAME allow:$TIMEMACHINE_USER options:tm
EOF
echo -e "${ECHO_COLOR_TITLE}Turning off default home shares...${ECHO_COLOR_DEFAULT}"
sed -i 's/^~/#~/' /etc/netatalk/AppleVolumes.default
# Change service
TM_MAC_ADDRESS="$(ifconfig $NETWORK_INTERFACE | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' -1)"
TM_UUID1=$(uuidgen)
TM_UUID2=$(uuidgen)
TM_UUID3=$(uuidgen)
cat > /etc/avahi/services/adisk.service << EOF
<?xml version="1.0" standalone="no"?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<!-- Created by timemachine-install.sh -->
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_adisk._tcp</type>
<port>9</port>
<txt-record>sys=waMA=$TM_MAC_ADDRESS,adVF=0×100</txt-record>
<txt-record>dk0=adVF=0xa1,adVN=$TIMEMACHINE_VOLUME_NAME,adVU=$TM_UUID1</txt-record>
<txt-record>dk1=adVN=media,adVU=$TM_UUID2</txt-record>
<txt-record>dk2=adVN=software,adVU=$TM_UUID3</txt-record>
</service>
</service-group>
EOF
echo -e "${ECHO_COLOR_TITLE}Restarting service netatalk...${ECHO_COLOR_DEFAULT}"
/etc/init.d/netatalk restart
echo -e "${ECHO_COLOR_TITLE}Restarting service avahi-daemon...${ECHO_COLOR_DEFAULT}"
/etc/init.d/avahi-daemon restart
echo -e "${ECHO_COLOR_DONE}Done${ECHO_COLOR_DEFAULT}."
@elomagic
Copy link
Author

elomagic commented Dec 2, 2016

Short bash script to setup easily a Time Machine Server.

How to run:

  • Create a file named timemachine-install.sh in your home folder and paste script from this page into it.
    ..or execute command curl https://gist.githubusercontent.com/elomagic/1171063ba7f6d5006094fc97be1b02c0/raw/d4cde11714b1f94da3f4645903efdcae76c378f4/timemachine-install.sh -o timemachine-install.sh to download directly.
  • Change permissions for the created file (Adding execute flag) chmod +x timemachine-install.sh.
  • Execute script in the bash shell with command ./timemachine-install.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment