User Tools

Site Tools


programming:python:backup

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.

Problem

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

Constraints

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

Assumptions

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

Approach

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.

Design

Interface

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 user@example.com '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 script.sh.
ssh user@example.com 'bash -s' < script.sh

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
 
subprocess.call(['ssh', 'user@example.com', 'ls', '-lha'])

Next

  • 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