VM backup and download


The following code snippet uses our https://github.com/pbkwee/s2i project to let you create a backup image of a Linux server, encrypt it, and then make it available for download (or transfer it directly to a remote server).

s2i requires PHP, and can make use of PHP’s built in HTTP server. You will need sufficient disk space on your VM image to create the download. The file is compressed, so the backup will typically be smaller than the disk space used on the VM.

wget -q  -O server-to-image.sh 'https://raw.githubusercontent.com/pbkwee/s2i/main/server-to-image.sh'

bash server-to-image.sh  --help
usage: dirname string [...]
 
  server-to-image.sh Creates a backup of a Linux server.  It has options to let you download that via http (else you can scp it from the source).  It has options to encrypt the backup file (e.g. via openssl or zip).
  
  Usage: server-to-image.sh 
    --encrypt openssl (default if using --http) | zip (not so secure) | none (default if not using --http)
    --http (serve file on an http url)
    --files (default to / )
    --size output the size of the backup (without creating it or using any disk space)
    --outputdir output directory [ /root/backup.s2i ]
    --outputfile output file [ backup-2022-06-30-1656569558 ]
    --outputextn output file extension [ gz | zip | gz.enc depending on encryption ]
    --outputpath output file full path (overrides other output options)
    --password by default we will create a password for you.  And use the same password each time the same outputdir is used.  NA if encrypt==none.
  
  Put files/directories you wish to exclude in /exclude.log
  
  We recommend you stop database servers and other processes that may be updating files while you run this script.
  
  If you use the --http option we will put the file on a URL that should be secret.  However we still recommend you use one of the --encrypt options.
  
  Sample usage using pipes, which will save using space for the backup on the server being backed up:
  backupserver: 
  mkdir bu
  mkfifo bu/fifo
  echo /dont/backup/this/dir > bu/exclude.log
  bash ./server-to-image.sh --outputpath bu/fifo

The script will create the backup file and let you scp that to your remote host (encryption is optional here). It also provides an option to let you download that via a URL (in that case it would select an encryption option by default).

If you use the openssl encryption option you will need openssl. This is typically available on most Linux installs. There are also Windows binaries available, e.g. from https://curl.se/windows/

To ensure database consistency we recommend you stop the database server while the files are being copied. Alternatively you could create a database dump prior to creating the backup image.

The s2i project also has a restore.sh script that will overwrite a Linux server with the backup you had taken.