title: How use systemd to activate a script to backup files on shutdown
body: <p>Can someone who knows systemd please help me figure out what I am doing wrong?</p>
  
  <p>I am trying to use systemd to trigger a script that will sync (backup) files from the home directory of a user (mardi) to a second drive when the user shuts down her computer.  I would prefer to have it only happen on shutdown but if that gets too complicated, then it can happen on shutdown or reboot; I am OK with that.  (I know this is not a particularly good back-up strategy but it is step one in an overall process so please do not provide feedback on the inadvisability of this ; I want to do this and I want to learn how to properly use systemd so please provide feedback that helps)</p>
  
  <p>I am running Linux Mint 18 - Ubuntu 16.04 based - if that makes a difference to the answers.</p>
  
  <p>I have included what I have done below, which is s result of reading the man pages for systemd and the multiple (often conflicting) forum pages here and elsewhere, but I am not getting it to work.</p>
  
  <p>I have tested the bash script and when it is run, it does what I want.</p>
  
  <p>I have manually stopped the mardibackup.service by running systemctl stop mardibackup.service and it does what I want.</p>
  
  <p>So I expect I am not defining the unit properly or calling the service properly, but I cannot figure out what to do not.</p>
  
  <p>Specific questions:</p>
  
  <p>1) Why use multi-user.target and call the service when the ExecStop is triggered stops?  Why not just use the shutdown.target and call the service when the ExecStart is triggered?</p>
  
  <p>2) What is ExecStart=/bin/true supposed to do?  It does not seem to do anything but it is in many recommended answers.</p>
  
  <p>Help??  And thank you.</p>
  
  <p>So, here is what I have done:</p>
  
  <p>1) Create the following script, using Lucky Backup to generate the rsync command and put it into /home/mardi/Templates/backupmardi.sh 
  (this is working when I execute it manually from the command line)</p>
  
  <pre><code>#!/bin/bash
  
  # This program synchronizes the contents of mardi home directory
  # to the archives disk in /backups/mardi
  #
  # It checks if a current version of a file is already copied and if so, does not change anything
  # If a file is deleted in the home directory, it is NOT deleted from the archives
  
  rsync -h --progress --stats -r -tgo -p -l -D --update --delete-after /home/mardi /mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff/backups/
  </code></pre>
  
  <p>2) Change the permissions of the script file to make it executable</p>
  
  <pre><code>chmod +x /home/mardi/Templates/backupmardi.sh
  </code></pre>
  
  <p>3) Create the following shutdown/restart service to call the backup script and put into /etc/systemd/system/mardibackup.service
  (this does what I want if I manually "Stop" it via systemctl stop mardibackup.service)</p>
  
  <pre><code>[Unit]
  Description=Backup Mardi home dir to archives
  
  [Service]
  Type=oneshot
  RemainAfterExit=true
  ExecStart=/bin/true
  ExecStop=/bin/bash /home/mardi/Templates/backupmardi.sh
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>4) create the symlink that will call the shutdown/restart script when the system is shutdown/restarted</p>
  
  <pre><code>systemctl enable /etc/systemd/system/mardibackup.service
      (Note if you ever want to remove the symlink, enter: systemctl disable mardibackup.service)
  systemctl start mardibackup.service
  </code></pre>
  
  <p>5) Shutdown the system 
  (I tried multiple shutdowns but it does not call the backup script backupmardi.sh)</p>
  
  <p>Anyone with some ideas?</p>