User Tools

Site Tools


Backup Script

DokuWiki saves all content to flat text files under a single directory. This design choice was what initially brought me to using the software. I want to backup and restore all content easily. The task of automating backing up the wiki also might serve as a neat project.


If I didn't have a backup utility how would I manually create a backup of this wiki?

  • Login to the server
  • Change directory to the root of the website
  • Use the tar command to create a compressed file containing all of the content from the data directory
  • Upload the tar archive to a 3rd-party service for long-term storage
  • Remove the local tar archive


  • Cannot use 3rd-party Python libraries
  • Cannot store credentials
  • Cannot run directly on the server


  • Runs on Linux with access to the ssh command
  • Python 2.7.x


Focus on simplicity of interface and implementation over all other concerns. Making the program fast or adding a feature must not compromise its simplicity in any way.



backup [-h]

Running Remote Commands

The ssh command allows you to easily execute one-off remote commands. It also supports streaming a local file to the remote server for execution by using bash's ability to take script input on stdin.

# One-off command.
ssh 'echo $USER'
# Full script. The -s flag to bash causes commands
# to be read and executed from stdin. In this case
# stdin is the content of
ssh 'bash -s' <

The ssh command will exit with the same exit code as the remote command or script, making it largely transparent to the caller.

Running Local Commands

There are a few ways to invoke CLI commands from Python. The two deprecated functions os.system and os.popen will work, but the subprocess module is what the Python developers want us to use.

import subprocess['ssh', '', 'ls', '-lha'])


  • Use Popen constructor
  • Use PIPEs
  • Use full shell
  • Use strings instead of sequence for command structure
  • Run multiple commands over single SSH connection
  • Use shlex to split a command into its sequence
programming/python/backup.txt · Last modified: 2015/11/17 12:59 by jason