Skip to content

Web App: File and/or Directory Backup, Part 1

Sometimes, we find ourselves needing to backup files or directories from one server to another remotely. In this case, it’s not very practical to X- into the second server in order to get the data to it. Instead, we can use the cp copy command over ssh by simply typing scp (for “secure copy”) at the command line. To see specific usage for your case, you can type scp --help or man scp, but as an example consider the following:I need to copy all the files from /home/virtual/intarwebz.com/home/intarwebz/htdocs on the intarwebz.com server to backups.intarwebz.com which is on another server halfway across the world. To do this, I login to the backups server via ssh and at the command line I navigate to the destination directory where I want to put the copy.

For instance (localhost being my local workstation and iwebz being my username on both my remote servers):

[admin@localhost ~]# ssh -liwebz backups.intarwebz.com

iwebz@backups.intarwebz.com’s password: ********

[iwebz@backups ~]# cd /etc/back-up (of course make sure you have write access to this directory)

[iwebz@backups back-up]# scp iwebz@intarwebz.com:/full/path/to/folder/file.ext .

(or if you were elsewhere in the file system and wanted to use a full path)

[iwebz@backups ~]# scp iwebz@intarwebz.com:/full/path/to/folder/file.ext /etc/back-up

At this point, it will prompt me for the password and, assuming my users on both ends have appropriate permissions, download to the local folder, as specified by the single period. If you wish to securely copy entire folder contents from one server to another, you will need to use the -r command like so:

scp -r user@host:/full/path/to/folder .

Please Note! scp does not prompt before overwriting and will write over any files or folders with the same name in the directory to which you’re copying. Be sure you are putting the files you want where you want them before pressing enter on an scp command.

If you use Linux (or MacOS-X) on your workstation, you can run scp to copy files from remote servers to your local machine using the same syntax.

Furthermore, you can setup key authentication such that you can use scp without being prompted for a password (handy when working with wildcards). For details on doing that, see this tutorial on password-less ssh or watch this video:

In part 2 of Web App Backup, you will learn techniques for backing up MySQL databases and database tables.