Backup a Site via FTP with wget
To quickly back up a website over FTP using wget, run wget -m ftp://username:password@example.com/ in a terminal, which mirrors the entire remote directory tree to your local machine. This command downloads all files and folders recursively, preserving timestamps and permissions, giving you a complete snapshot of your site’s files. Below is a detailed guide on how to use wget effectively for FTP backups, including options, common pitfalls, and safety considerations.
Why Use wget for FTP Backups?
wget is a free, command-line utility available on Linux, macOS, and Windows (via WSL or Cygwin) that supports HTTP, HTTPS, and FTP protocols. For FTP backups, it is particularly useful because it can recursively download an entire directory structure without requiring a GUI or paid software. Unlike a simple file-by-file download, wget’s mirror mode (-m) ensures that the local copy exactly matches the remote site, including file timestamps and directory structure. This makes it ideal for creating a quick, complete backup before making changes, migrating to a new host, or simply archiving a site.
Basic wget FTP Command Syntax
The core command structure is straightforward. The FTP URL can include credentials directly, or you can use the --ftp-user and --ftp-password options for better security (avoiding credentials in shell history). Here is the basic form:
wget -m ftp://username:password@example.com/public_html/
- -m (or
--mirror): Enables recursive download, timestamping, and infinite retries—essentially a full mirror. - ftp://username:password@example.com/path/: The FTP URL with credentials and the remote directory to back up.
If you prefer not to put the password in the command, use:
wget -m --ftp-user=username --ftp-password=yourpassword ftp://example.com/public_html/
This will download everything under public_html/ into a local folder named example.com/public_html/ in your current directory. To change the local destination, use the -P (directory prefix) option:
wget -m -P /path/to/local/backup ftp://username:password@example.com/public_html/
Key wget Options for FTP Backup
Several options are particularly relevant when backing up via FTP:
-r(recursive): Enables recursive download.-mimplies-rbut also adds timestamping and infinite retries.-np(no parent): Prevents wget from ascending to the parent directory. Without this, wget might try to download the entire FTP root if a link points upward. Always use-npwhen mirroring a subdirectory.-nH(no host directories): By default, wget creates a local folder named after the host (e.g.,example.com). This option skips that level, placing files directly in the destination folder.--cut-dirs=N: Removes the first N directories from the remote path when saving locally. For example, if you back up/public_html/images/,--cut-dirs=1saves files underimages/instead ofpublic_html/images/.--no-remove-listing: Keeps the temporary.listingfiles wget creates to parse directory contents. Useful for debugging, but not needed for a clean backup.--retry-connrefused: Retries even if the connection is refused, which can help with transient FTP server issues.
Common Problems and How to Avoid Them
While wget is reliable, FTP backups can fail or produce incomplete results. Here are the most frequent issues and solutions:
- Password with special characters: If your password contains
@,:,/, or?, it will break the URL. Use the--ftp-userand--ftp-passwordoptions instead of embedding them in the URL. - Passive vs. Active FTP: Many servers require passive mode. wget defaults to passive, but if you get connection timeouts, try
--no-passive-ftp(active mode) or vice versa. - Symlinks and permissions: wget does not follow symbolic links by default (which is good for avoiding loops), but it also does not preserve Unix permissions. After backup, you may need to reset file permissions manually if you restore to a new server.
- Incomplete downloads due to network drops: Use
--tries=0(infinite retries) or a high number like--tries=10. Combined with-c(continue), wget can resume interrupted downloads, though-cis not always reliable with FTP. - Accidentally downloading the whole server: If you omit the trailing slash on the remote path, wget might treat it as a file and fail, or if you use
-rwithout-np, it may crawl upward. Always specify the exact directory with a trailing slash and add-np.
Comparing wget to Other Backup Methods
wget is not the only way to back up a site via FTP. Alternatives include:
- lftp: A more FTP-savvy client that supports parallel downloads, resuming, and mirroring with better error handling. For large sites,
lftp -e 'mirror /public_html /local/backup'is often faster and more robust. - FileZilla: A GUI client that allows drag-and-drop downloads, but it is manual and not scriptable without additional tools.
- rsync over SSH: If your host offers SSH, rsync is superior—it only transfers changed files, preserves permissions, and is faster. But it requires SSH access, not just FTP.
- Control Panel backups: cPanel or Plesk often have one-click backup tools that include databases, but they may not be available on all hosts.
For a quick, one-off backup, wget is perfectly adequate. For recurring or large-scale backups, lftp or rsync are better choices.
Safety and Security Considerations
Backing up via FTP has inherent risks. FTP transmits credentials and data in plaintext, so anyone on the network can intercept them. For sensitive sites, use FTPS (FTP over SSL) or SFTP (SSH File Transfer Protocol). wget supports FTPS with --secure-protocol=ftps, but not SFTP (which requires a different tool like lftp or sftp). Additionally, be careful with credentials in shell history—use --ftp-password interactively or store them in a .wgetrc file with restricted permissions. Finally, always verify the backup by checking file counts and sizes, and test a restore on a staging server before relying on it.
Frequently Asked Questions
Does wget preserve file permissions and timestamps?
wget preserves timestamps by default when using -m (it sets the local file’s modification time to match the remote). However, it does not preserve Unix file permissions (like 755 or 644). After restoring, you may need to set permissions manually, especially for scripts or directories that require execute bits.
Can I use wget to back up a site that requires FTPS?
Yes. Use wget --secure-protocol=ftps to force FTPS. You may also need --no-check-certificate if the server uses a self-signed certificate, but that reduces security. For SFTP (SSH), wget does not support it; use lftp or sftp instead.
How do I resume an interrupted wget FTP download?
Add -c (continue) to the command. wget will attempt to resume partially downloaded files. However, FTP servers may not support resuming for all file types. If resuming fails, delete the partial file and re-download it.
Why did wget download the entire server instead of just my site?
This usually happens when you omit -np (no parent). If the FTP server’s root is accessible, wget might follow links upward. Always specify the full path to your site’s directory and include -np to prevent this.
Is there a way to back up only new or changed files with wget?
wget’s mirror mode (-m) uses timestamping to avoid re-downloading unchanged files. If you run the same command again, it will only fetch files that are newer on the server. However, it does not delete local files that were removed remotely unless you add --delete-after (which is not recommended for backups).
Can I use wget to back up a database via FTP?
No. FTP only handles files. Databases are stored as binary files or exported via tools like phpMyAdmin. You would need to export the database separately (e.g., mysqldump) and then download that file via FTP.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.