Backup single file


I want to backup a single file using restic. I know how to do this with folders and with the option --files-from.

Is there an option like restic -r /srv/restic-repo backup file.sql ?


Yes, it is possible to backup a single file like that. You’ll see the filepath+filename on the “Paths” section on snapshot listing.

Note that you can even use stdin as backup source, e.g.:

mysqldump -u <user> -p <password> <...> | restic -r <repo> backup --stdin --stdin-filename db_backup.sql

This is how I currently use it. However, it seems that sometimes it takes too long to connect to the restic repository on the backup server (Hetzner Storage Box) via SFTP and mysqldump aborts the backup.

To prevent this I would first create the MySQL dump and then backup the file to the backup server via restic.

Ah, I see. The command

mysqldump -u <user> -p <password> <...> | restic -r <repo> backup --stdin --stdin-filename db_backup.sql


mysqldump -u <user> -p <password> <...> > db_backup.sql
restic -r <repo> backup db_backup.sql

will result in identical repository changes (maybe except the saved metadata like permissions for that file).

I’m having a problem with this approach:

mysqldump -u <user> -p <password> <...> > db_backup.sql
restic -r <repo> backup db_backup.sql

If the backup directory contains any files other than db_backup.sql they seem to be getting backed up too. How do I instruct restic to only back up that single file and ignore all other files in the directory?

This command backs up only one thing, and that is the file named db_backup.sql (assuming you dont have a directory named db_backup.sql of ourse). It really should not back anything else up. If you think it does, please add -vv right before the backup command and show us all the output from that command when you run it. I could also ask, what makes you think that it is backing up anything else than that single file?

I could also ask, what makes you think that it is backing up anything else than that single file?

Because I deleted all snapshots for that directory, ran the single-file backup command, then ran restic ls <snapshot-id> and I saw it added all files in the directory. I’ll try investigating with -vv. It’s probably caused by user error but I don’t understand why yet.

Okay, so here is what I found…

If I run restic ls -r <repo> latest --path="c:/Program Files/PostgreSQL/14/backup" I get back:

snapshot af06e9fd of [c:\Program Files\PostgreSQL\14\backup c:\Program Files\PostgreSQL\14\backup\postgresql.rar] filtered by [] at 2021-11-16 11:39:59.6056439 -0500 EST):
<multiple unrelated files>
<multiple unrelated files>

If I run restic ls -r <repo> af06e9fd I get the same list with unrelated files.

If I run restic snapshots -r <repo> I get back:

af06e9fd  2021-11-16 11:39:59  <hostname>              c:\Program Files\PostgreSQL\14\backup
                                                       c:\Program Files\PostgreSQL\14\backup\postgresql.rar

My actual backup command is restic -r <repo> . db_backup.sql and it looks like restic interprets this as: Backup ./** and then backup db_backup.sql.

What I wanted restic to do is backup db_backup.sql, but show the parent path under the list of paths associated with the snapshot. Thinking about this further, it makes no sense as it implies the full directory was backed up. Omitting . from the backup command fixes the problem. Sorry for the false alarm.

You got it.